display gas cost of each test

This commit is contained in:
Jonathan Rainville 2018-06-29 10:29:07 -04:00 committed by Iuri Matias
parent d0851d70ba
commit 5ccd8f6333
3 changed files with 45 additions and 21 deletions

View File

@ -14,7 +14,7 @@ function log(eventType, eventName) {
//console.log(eventType, eventName); //console.log(eventType, eventName);
} }
EventEmitter.prototype._maxListeners = 300; EventEmitter.prototype._maxListeners = 350;
const _on = EventEmitter.prototype.on; const _on = EventEmitter.prototype.on;
const _setHandler = EventEmitter.prototype.setHandler; const _setHandler = EventEmitter.prototype.setHandler;

View File

@ -1,4 +1,5 @@
const Base = require('mocha/lib/reporters/base'); const Base = require('mocha/lib/reporters/base');
const ms = require('mocha/lib/ms');
const color = Base.color; const color = Base.color;
class EmbarkSpec extends Base { class EmbarkSpec extends Base {
@ -11,10 +12,12 @@ class EmbarkSpec extends Base {
self.gasLimit = options.reporterOptions.gasLimit; self.gasLimit = options.reporterOptions.gasLimit;
let indents = 0; let indents = 0;
let n = 0; let n = 0;
self.stats.gasCost = 0; self.stats.totalGasCost = 0;
self.stats.test = {};
self.stats.test.gasUsed = 0;
function onContractReceipt(receipt) { function onContractReceipt(receipt) {
self.stats.gasCost += receipt.gasUsed; self.stats.totalGasCost += receipt.gasUsed;
if (self.gasDetails) { if (self.gasDetails) {
const fmt = color('bright pass', ' ') + const fmt = color('bright pass', ' ') +
color('green', ' %s') + color('green', ' %s') +
@ -25,8 +28,13 @@ class EmbarkSpec extends Base {
console.log(fmt, receipt.className, receipt.gasUsed); console.log(fmt, receipt.className, receipt.gasUsed);
} }
} }
function onBlockHeader(blockHeader) {
self.stats.totalGasCost += blockHeader.gasUsed;
self.stats.test.gasUsed += blockHeader.gasUsed;
}
self.embarkEvents.on("deploy:contract:receipt", onContractReceipt); self.embarkEvents.on("deploy:contract:receipt", onContractReceipt);
self.embarkEvents.on("block:header", onBlockHeader);
function indent() { function indent() {
return Array(indents).join(' '); return Array(indents).join(' ');
@ -56,31 +64,31 @@ class EmbarkSpec extends Base {
console.log(fmt, test.title); console.log(fmt, test.title);
}); });
runner.on('test', function () {
self.stats.test.gasUsed = 0;
});
runner.on('pass', function (test) { runner.on('pass', function (test) {
let fmt; let fmt =
if (test.speed === 'fast') { indent() +
fmt = color('checkmark', ' ' + Base.symbols.ok) +
indent() + color('pass', ' %s') +
color('checkmark', ' ' + Base.symbols.ok) + color(test.speed, ' (%dms)') +
color('pass', ' %s'); ' - ' +
console.log(fmt, test.title); color(self.getGasColor(self.stats.test.gasUsed), '[%d gas]');
} else { console.log(fmt, test.title, test.duration, self.stats.test.gasUsed);
fmt =
indent() +
color('checkmark', ' ' + Base.symbols.ok) +
color('pass', ' %s') +
color(test.speed, ' (%dms)');
console.log(fmt, test.title, test.duration);
}
}); });
runner.on('fail', function (test) { runner.on('fail', function (test) {
console.log(indent() + color('fail', ' %d) %s'), ++n, test.title); console.log(indent() + color('fail', ' %d) %s') + ' - ' + color(self.getGasColor(self.stats.test.gasUsed), '[%d gas]'),
++n, test.title, self.stats.test.gasUsed);
}); });
runner.once('end', function () { runner.once('end', function () {
runner.removeAllListeners(); runner.removeAllListeners();
self.embarkEvents.removeListener("deploy:contract:receipt", onContractReceipt); self.embarkEvents.removeListener("deploy:contract:receipt", onContractReceipt);
self.embarkEvents.removeListener("block:header", onBlockHeader);
self.epilogue(); self.epilogue();
}); });
} }
@ -104,11 +112,13 @@ class EmbarkSpec extends Base {
// passes // passes
fmt = color('bright pass', ' ') + fmt = color('bright pass', ' ') +
color('green', ' %d passing') + color('green', ' %d passing') +
color(this.getGasColor(stats.gasCost), ' (%s gas)'); color('light', ' (%s)') +
color('light', ' - [Total: %s gas]');
console.log(fmt, console.log(fmt,
stats.passes || 0, stats.passes || 0,
stats.gasCost); ms(stats.duration),
stats.totalGasCost);
// pending // pending
if (stats.pending) { if (stats.pending) {

View File

@ -39,6 +39,7 @@ class Test {
this.error = false; this.error = false;
this.builtContracts = {}; this.builtContracts = {};
this.compiledContracts = {}; this.compiledContracts = {};
this.logsSubscription = null;
this.web3 = new Web3(); this.web3 = new Web3();
} }
@ -87,9 +88,22 @@ class Test {
this.sim = getSimulator(); this.sim = getSimulator();
} }
this.web3.setProvider(this.sim.provider(this.simOptions)); this.web3.setProvider(this.sim.provider(this.simOptions));
this.subscribeToPendingTransactions();
callback(); callback();
} }
subscribeToPendingTransactions() {
const self = this;
if (self.logsSubscription) {
self.logsSubscription.unsubscribe();
}
self.logsSubscription = self.web3.eth
.subscribe('newBlockHeaders')
.on("data", function (blockHeader) {
self.engine.events.emit('block:header', blockHeader);
});
}
initDeployServices() { initDeployServices() {
this.engine.startService("web3", { this.engine.startService("web3", {
web3: this.web3 web3: this.web3