eth-gas-reporter
Advanced tools
Comparing version 0.0.4 to 0.0.5
154
index.js
@@ -1,92 +0,106 @@ | ||
var mocha = require('mocha'); | ||
var inherits = require('util').inherits; | ||
var Base = mocha.reporters.Base; | ||
var color = Base.color; | ||
const mocha = require('mocha') | ||
const inherits = require('util').inherits | ||
const stats = require('./gasStats.js') | ||
const Base = mocha.reporters.Base | ||
const color = Base.color | ||
const log = console.log | ||
module.exports = Gas | ||
module.exports = Gas; | ||
// Based on 'Spec'. Please smoke outside while running your suite. For safety. | ||
// Based on the 'Spec' reporter | ||
function Gas (runner) { | ||
Base.call(this, runner); | ||
Base.call(this, runner) | ||
var self = this; | ||
var indents = 0; | ||
var n = 0; | ||
var startBlock; | ||
const self = this | ||
let indents = 0 | ||
let n = 0 | ||
let startBlock | ||
let methodMap | ||
function indent () { | ||
return Array(indents).join(' '); | ||
} | ||
// ------------------------------------ Helpers ------------------------------------------------- | ||
const indent = () => Array(indents).join(' ') | ||
function calculateGasUsed(){ | ||
let gasUsed = 0; | ||
const endBlock = web3.eth.blockNumber; | ||
while(startBlock <= endBlock){ | ||
let block = web3.eth.getBlock(startBlock); | ||
if (block) | ||
gasUsed += block.gasUsed; | ||
startBlock++; | ||
const gasAnalytics = (methodMap) => { | ||
let gasUsed = 0 | ||
const endBlock = web3.eth.blockNumber | ||
while (startBlock <= endBlock) { | ||
let block = web3.eth.getBlock(startBlock) | ||
if (block) { | ||
// Add to running tally for this test | ||
gasUsed += block.gasUsed | ||
// Compile per method stats | ||
methodMap && block.transactions.forEach(tx => { | ||
const input = web3.eth.getTransaction(tx).input; | ||
const receipt = web3.eth.getTransactionReceipt(tx); | ||
const id = stats.getMethodID( input ); | ||
if (methodMap[id]){ | ||
methodMap[id].gasData.push(receipt.gasUsed); | ||
} | ||
}) | ||
} | ||
startBlock++ | ||
} | ||
return gasUsed; | ||
}; | ||
return gasUsed | ||
} | ||
runner.on('start', function () { | ||
console.log(); | ||
}); | ||
// ------------------------------------ Runners ------------------------------------------------- | ||
runner.on('start', () => { | ||
methodMap = stats.mapMethodsToContracts(artifacts) | ||
log() | ||
}) | ||
runner.on('suite', function (suite) { | ||
++indents; | ||
console.log(color('suite', '%s%s'), indent(), suite.title); | ||
}); | ||
runner.on('suite', suite => { | ||
++indents | ||
log(color('suite', '%s%s'), indent(), suite.title) | ||
}) | ||
runner.on('suite end', function () { | ||
--indents; | ||
runner.on('suite end', () => { | ||
--indents | ||
if (indents === 1) { | ||
console.log(); | ||
log() | ||
} | ||
}); | ||
}) | ||
runner.on('hook end', function () { | ||
startBlock = web3.eth.blockNumber + 1; | ||
runner.on('pending', test => { | ||
let fmt = indent() + color('pending', ' - %s') | ||
log(fmt, test.title) | ||
}) | ||
runner.on('test', function() {}); | ||
runner.on('hook end', () => { startBlock = web3.eth.blockNumber + 1 }) | ||
runner.on('pending', function (test) { | ||
var fmt = indent() + color('pending', ' - %s'); | ||
console.log(fmt, test.title); | ||
}); | ||
runner.on('pass', test => { | ||
let fmt | ||
let limitString | ||
let gasUsed = gasAnalytics(methodMap) | ||
//let percent = 0 | ||
let percent = stats.gasToPercentOfLimit(gasUsed); | ||
runner.on('pass', function (test) { | ||
let fmt; | ||
let gasUsed = calculateGasUsed(); | ||
if (test.speed === 'fast') { | ||
fmt = indent() + | ||
color('checkmark', ' ' + Base.symbols.ok) + | ||
color('pass', ' %s')+ | ||
color('checkmark', ' (%d gas)'); | ||
console.log(fmt, test.title, gasUsed); | ||
if (percent >= 100){ | ||
limitString = color('fail', ' (%d% of limit) ') | ||
} else { | ||
fmt = indent() + | ||
color('checkmark', ' ' + Base.symbols.ok) + | ||
color('pass', ' %s') + | ||
color(test.speed, ' (%dms)') + | ||
color('checkmark', ' (%d gas)'); | ||
console.log(fmt, test.title, test.duration, gasUsed); | ||
limitString = color('pass', ' (%d% of limit) ') | ||
} | ||
}); | ||
runner.on('fail', function (test) { | ||
let gasUsed = calculateGasUsed(); | ||
fmt = indent() + | ||
color('checkmark', ' ' + Base.symbols.ok) + | ||
color('pass', ' %s') + | ||
color('checkmark', ' (%d gas)') + | ||
limitString | ||
log(fmt, test.title, gasUsed, percent) | ||
}) | ||
runner.on('fail', test => { | ||
let gasUsed = gasAnalytics() | ||
let fmt = indent() + | ||
color('fail', ' %d) %s') + | ||
color('pass', ' (%d gas)'); | ||
console.log() | ||
color('pass', ' (%d gas)') | ||
log() | ||
log(fmt, ++n, test.title, gasUsed) | ||
}) | ||
console.log(fmt, ++n, test.title, gasUsed); | ||
}); | ||
runner.on('end', self.epilogue.bind(self)); | ||
runner.on('end', () => { | ||
self.epilogue(); | ||
stats.generateGasStatsReport (methodMap) | ||
}) | ||
} | ||
@@ -97,2 +111,2 @@ | ||
*/ | ||
inherits(Gas, Base); | ||
inherits(Gas, Base) |
@@ -1,5 +0,5 @@ | ||
var Migrations = artifacts.require("./Migrations.sol"); | ||
var Migrations = artifacts.require('./Migrations.sol') | ||
module.exports = function(deployer) { | ||
deployer.deploy(Migrations); | ||
}; | ||
module.exports = function (deployer) { | ||
deployer.deploy(Migrations) | ||
} |
@@ -1,8 +0,12 @@ | ||
var ConvertLib = artifacts.require("./ConvertLib.sol"); | ||
var MetaCoin = artifacts.require("./MetaCoin.sol"); | ||
var ConvertLib = artifacts.require('./ConvertLib.sol') | ||
var MetaCoin = artifacts.require('./MetaCoin.sol') | ||
var Wallet = artifacts.require('./Wallets/Wallet.sol') | ||
var VariableCosts = artifacts.require('./VariableCosts.sol') | ||
module.exports = function(deployer) { | ||
module.exports = function (deployer) { | ||
deployer.deploy(ConvertLib); | ||
deployer.link(ConvertLib, MetaCoin); | ||
deployer.deploy(MetaCoin); | ||
}; | ||
deployer.deploy(Wallet); | ||
deployer.deploy(VariableCosts); | ||
} |
{ | ||
"name": "mock", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "truffle.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"name": "eth-gas-reporter", | ||
"version": "0.0.4", | ||
"description": "Mocha reporter which shows gas used per unit test.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "./mock/test.sh" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/cgewecke/eth-gas-reporter.git" | ||
}, | ||
"keywords": [ | ||
"Ethereum", | ||
"solidity", | ||
"unit-testing", | ||
"truffle", | ||
"gas." | ||
], | ||
"author": "cgewecke <github.com/cgewecke>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/cgewecke/eth-gas-reporter/issues" | ||
}, | ||
"homepage": "https://github.com/cgewecke/eth-gas-reporter#readme", | ||
"dependencies": { | ||
"abi-decoder": "^1.0.8", | ||
"cli-table2": "^0.2.0", | ||
"colors": "^1.1.2", | ||
"lodash": "^4.17.4", | ||
"req-cwd": "^2.0.0", | ||
"request": "^2.83.0", | ||
"request-promise-native": "^1.0.5", | ||
"shelljs": "^0.7.8" | ||
}, | ||
"devDependencies": { | ||
"eth-gas-reporter": "0.0.3", | ||
"ethereumjs-testrpc": "^4.1.3", | ||
@@ -17,0 +38,0 @@ "truffle": "^3.4.11" |
@@ -1,73 +0,72 @@ | ||
var MetaCoin = artifacts.require("./MetaCoin.sol"); | ||
var MetaCoin = artifacts.require('./MetaCoin.sol') | ||
contract('MetaCoin', function(accounts) { | ||
beforeEach(async function(){ | ||
await MetaCoin.new(); | ||
await MetaCoin.new(); | ||
}); | ||
afterEach(async function(){ | ||
await MetaCoin.new(); | ||
await MetaCoin.new(); | ||
}); | ||
contract('MetaCoin', function (accounts) { | ||
beforeEach(async function () { | ||
await MetaCoin.new() | ||
await MetaCoin.new() | ||
}) | ||
afterEach(async function () { | ||
await MetaCoin.new() | ||
await MetaCoin.new() | ||
}) | ||
it("should put 10000 MetaCoin in the first account", function() { | ||
return MetaCoin.deployed().then(function(instance) { | ||
return instance.getBalance.call(accounts[0]); | ||
}).then(function(balance) { | ||
assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account"); | ||
}); | ||
}); | ||
it("should call a function that depends on a linked library", function() { | ||
var meta; | ||
var metaCoinBalance; | ||
var metaCoinEthBalance; | ||
it('should put 10000 MetaCoin in the first account', function () { | ||
return MetaCoin.deployed().then(function (instance) { | ||
return instance.getBalance.call(accounts[0]) | ||
}).then(function (balance) { | ||
assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account") | ||
}) | ||
}) | ||
it('should call a function that depends on a linked library', function () { | ||
var meta | ||
var metaCoinBalance | ||
var metaCoinEthBalance | ||
return MetaCoin.deployed().then(function(instance) { | ||
meta = instance; | ||
return meta.getBalance.call(accounts[0]); | ||
}).then(function(outCoinBalance) { | ||
metaCoinBalance = outCoinBalance.toNumber(); | ||
return meta.getBalanceInEth.call(accounts[0]); | ||
}).then(function(outCoinBalanceEth) { | ||
metaCoinEthBalance = outCoinBalanceEth.toNumber(); | ||
}).then(function() { | ||
assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, "Library function returned unexpected function, linkage may be broken"); | ||
}); | ||
}); | ||
it("should send coin correctly", function() { | ||
var meta; | ||
return MetaCoin.deployed().then(function (instance) { | ||
meta = instance | ||
return meta.getBalance.call(accounts[0]) | ||
}).then(function (outCoinBalance) { | ||
metaCoinBalance = outCoinBalance.toNumber() | ||
return meta.getBalanceInEth.call(accounts[0]) | ||
}).then(function (outCoinBalanceEth) { | ||
metaCoinEthBalance = outCoinBalanceEth.toNumber() | ||
}).then(function () { | ||
assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, 'Library function returned unexpected function, linkage may be broken') | ||
}) | ||
}) | ||
it('should send coin correctly', function () { | ||
var meta | ||
// Get initial balances of first and second account. | ||
var account_one = accounts[0]; | ||
var account_two = accounts[1]; | ||
var account_one = accounts[0] | ||
var account_two = accounts[1] | ||
var account_one_starting_balance; | ||
var account_two_starting_balance; | ||
var account_one_ending_balance; | ||
var account_two_ending_balance; | ||
var account_one_starting_balance | ||
var account_two_starting_balance | ||
var account_one_ending_balance | ||
var account_two_ending_balance | ||
var amount = 10; | ||
var amount = 10 | ||
return MetaCoin.deployed().then(function(instance) { | ||
meta = instance; | ||
return meta.getBalance.call(account_one); | ||
}).then(function(balance) { | ||
account_one_starting_balance = balance.toNumber(); | ||
return meta.getBalance.call(account_two); | ||
}).then(function(balance) { | ||
account_two_starting_balance = balance.toNumber(); | ||
return meta.sendCoin(account_two, amount, {from: account_one}); | ||
}).then(function() { | ||
return meta.getBalance.call(account_one); | ||
}).then(function(balance) { | ||
account_one_ending_balance = balance.toNumber(); | ||
return meta.getBalance.call(account_two); | ||
}).then(function(balance) { | ||
account_two_ending_balance = balance.toNumber(); | ||
return MetaCoin.deployed().then(function (instance) { | ||
meta = instance | ||
return meta.getBalance.call(account_one) | ||
}).then(function (balance) { | ||
account_one_starting_balance = balance.toNumber() | ||
return meta.getBalance.call(account_two) | ||
}).then(function (balance) { | ||
account_two_starting_balance = balance.toNumber() | ||
return meta.sendCoin(account_two, amount, {from: account_one}) | ||
}).then(function () { | ||
return meta.getBalance.call(account_one) | ||
}).then(function (balance) { | ||
account_one_ending_balance = balance.toNumber() | ||
return meta.getBalance.call(account_two) | ||
}).then(function (balance) { | ||
account_two_ending_balance = balance.toNumber() | ||
assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender"); | ||
assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver"); | ||
}); | ||
}); | ||
}); | ||
assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender") | ||
assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver") | ||
}) | ||
}) | ||
}) |
module.exports = { | ||
networks: { | ||
development: { | ||
host: "localhost", | ||
host: 'localhost', | ||
port: 8545, | ||
network_id: "*", // Match any network id | ||
network_id: '*' // Match any network id | ||
} | ||
@@ -12,2 +12,2 @@ }, | ||
} | ||
}; | ||
} |
{ | ||
"name": "eth-gas-reporter", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Mocha reporter which shows gas used per unit test.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "./mock/test.sh", | ||
"postinstall": "cd mock && npm install" | ||
"test": "./mock/test.sh" | ||
}, | ||
@@ -26,3 +25,17 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/cgewecke/eth-gas-reporter#readme" | ||
"homepage": "https://github.com/cgewecke/eth-gas-reporter#readme", | ||
"dependencies": { | ||
"abi-decoder": "^1.0.8", | ||
"cli-table2": "^0.2.0", | ||
"colors": "^1.1.2", | ||
"lodash": "^4.17.4", | ||
"req-cwd": "^2.0.0", | ||
"request": "^2.83.0", | ||
"request-promise-native": "^1.0.5", | ||
"shelljs": "^0.7.8" | ||
}, | ||
"devDependencies": { | ||
"ethereumjs-testrpc": "^4.1.3", | ||
"truffle": "^3.4.11" | ||
} | ||
} |
# eth-gas-reporter | ||
Gas usage per unit test. Average gas usage per method. A mocha reporter. | ||
![metacoingas2](https://user-images.githubusercontent.com/7332026/30790579-874eccc6-a161-11e7-90f9-3a08de72b1c7.png) | ||
![screen shot 2017-10-12 at 8 02 57 pm](https://user-images.githubusercontent.com/7332026/31528529-b0d52178-af88-11e7-9c58-004ba4a7b360.png) | ||
@@ -9,6 +10,6 @@ | ||
```javascript | ||
// Mocha installed globally | ||
// Truffle/mocha installed globally | ||
npm install -g eth-gas-reporter | ||
// Mocha installed locally | ||
// Truffle/mocha installed locally | ||
npm install --save-dev eth-gas-reporter | ||
@@ -32,7 +33,7 @@ ``` | ||
``` | ||
### Thanks | ||
### Run without truffle | ||
``` | ||
mocha --reporter eth-gas-reporter | ||
``` | ||
Everything here has been cannibalized from elsewhere and dropped into mocha's reporter facility. Many thanks to: | ||
+ [@maurelian](https://github.com/maurelian) - once posted a slack picture of mocha reporting gas output instead time. Idea from him. | ||
+ [@cag](https://github.com/cag) - wrote a really nice gas stats utility for the Gnosis suites. The table borrows from / is based his work. | ||
+ [Neufund](https://github.com/Neufund/ico-contracts) - expressing gas usage as a ratio of the block limit in their ico suite. That comes from them. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
23986
21
0
438
38
0
8
2
1
+ Addedabi-decoder@^1.0.8
+ Addedcli-table2@^0.2.0
+ Addedcolors@^1.1.2
+ Addedlodash@^4.17.4
+ Addedreq-cwd@^2.0.0
+ Addedrequest@^2.83.0
+ Addedshelljs@^0.7.8
+ Addedabi-decoder@1.2.0(transitive)
+ Addedajv@6.12.6(transitive)
+ Addedansi-regex@2.1.1(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcli-table2@0.2.0(transitive)
+ Addedcode-point-at@1.1.0(transitive)
+ Addedcolors@1.4.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addedcrypto-js@3.3.0(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedinterpret@1.4.0(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedis-fullwidth-code-point@1.0.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedlodash@3.10.14.17.21(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.10.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrechoir@0.6.2(transitive)
+ Addedreq-cwd@2.0.0(transitive)
+ Addedreq-from@2.0.0(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedrequest-promise-core@1.1.4(transitive)
+ Addedrequest-promise-native@1.0.9(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedresolve-from@3.0.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedshelljs@0.7.8(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedstealthy-require@1.1.1(transitive)
+ Addedstring-width@1.0.2(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addedutf8@2.1.2(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)
+ Addedweb3@0.18.4(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedxhr2@0.2.1(transitive)
+ Addedxmlhttprequest@1.8.0(transitive)