Skip to content

Commit 057b39e

Browse files
committed
function gas usage logger in test | TODO: log also contract creation
1 parent 27dfcd1 commit 057b39e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

test/TestEscrow.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { duration, latestTime } = require('./helpers/latestTime.js');
2+
const { logGasUsed } = require('./helpers/gas.js')
23

34
const Escrow = artifacts.require('market/Escrow.sol')
45
const Token = artifacts.require('tokens/SingularityNetToken.sol')
@@ -22,9 +23,11 @@ contract('Escrow', function ([payee, payer, validator]) {
2223
const jobDescriptor = "0x01"
2324
//Complete jobs
2425

25-
await this.token.approve(this.escrow.address, amount, { from: payer })
26+
const approveResult = await this.token.approve(this.escrow.address, amount, { from: payer })
27+
logGasUsed('token.approve',approveResult)
28+
2629
const result = await this.escrow.deposit(amount, jobDescriptor, { from: payer })
27-
30+
logGasUsed('escrow.deposit',result)
2831

2932
const found2 = result.logs.find(e => e.event === 'Deposited')
3033
assert.strictEqual(found2.event, 'Deposited', 'Deposited event not fired')
@@ -76,6 +79,7 @@ contract('Escrow', function ([payee, payer, validator]) {
7679
await this.escrow.deposit(amount, jobDescriptor, { from: payer })
7780
await this.escrow.setResult(jobResult, { from: payee })
7881
const result = await this.escrow.accept({from: payer})
82+
logGasUsed('escrow.setResult',result)
7983

8084
const found = result.logs.find(e => e.event === 'Accepted')
8185
assert.strictEqual(found.event, 'Accepted', 'Accepted event not fired')
@@ -92,6 +96,7 @@ contract('Escrow', function ([payee, payer, validator]) {
9296
const time = (await this.escrow.end.call()) + duration.minutes(10)
9397
await increaseTimeTo(time)
9498
let result = await this.escrow.withdraw({ from: payee })
99+
logGasUsed('escrow.withdraw',result)
95100
const found = result.logs.find(e => e.event === 'Withdrew')
96101
assert.strictEqual(found.event, 'Withdrew', 'Withdrew event not fired')
97102

test/helpers/gas.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
exports.logGasUsed = (msg, tx) => {
2+
if (tx.receipt) {
3+
console.log(`Function: ${msg}\nCumulative gas used ${tx.receipt.cumulativeGasUsed}\nGas used ${tx.receipt.gasUsed}`)
4+
}
5+
}

0 commit comments

Comments
 (0)