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);
}
EventEmitter.prototype._maxListeners = 300;
EventEmitter.prototype._maxListeners = 350;
const _on = EventEmitter.prototype.on;
const _setHandler = EventEmitter.prototype.setHandler;

View File

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

View File

@ -39,6 +39,7 @@ class Test {
this.error = false;
this.builtContracts = {};
this.compiledContracts = {};
this.logsSubscription = null;
this.web3 = new Web3();
}
@ -87,9 +88,22 @@ class Test {
this.sim = getSimulator();
}
this.web3.setProvider(this.sim.provider(this.simOptions));
this.subscribeToPendingTransactions();
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() {
this.engine.startService("web3", {
web3: this.web3