This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
/
index.js
135 lines (114 loc) · 3.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const mocha = require("mocha");
const inherits = require("util").inherits;
const Base = mocha.reporters.Base;
const color = Base.color;
const log = console.log;
const utils = require("./lib/utils");
const Config = require("./lib/config");
const TransactionWatcher = require("./lib/transactionWatcher");
const GasTable = require("./lib/gasTable");
const SyncRequest = require("./lib/syncRequest");
/**
* Based on the Mocha 'Spec' reporter. Watches an Ethereum test suite run
* and collects data about method & deployments gas usage. Mocha executes the hooks
* in this reporter synchronously so any client calls here should be executed
* via low-level RPC interface using sync-request. (see /lib/syncRequest)
* An exception is made for fetching gas & currency price data from coinmarketcap and
* ethgasstation (we hope that single call will complete by the time the tests finish running)
*
* @param {Object} runner mocha's runner
* @param {Object} options reporter.options (see README example usage)
*/
function Gas(runner, options) {
// Spec reporter
Base.call(this, runner);
const self = this;
let indents = 0;
let n = 0;
let failed = false;
let indent = () => Array(indents).join(" ");
// Gas reporter setup
const config = new Config(options.reporterOptions);
const sync = new SyncRequest(config.url);
const watch = new TransactionWatcher(config);
const table = new GasTable(config);
// These call the cloud, start running them.
utils.setGasAndPriceRates(config);
// ------------------------------------ Runners -------------------------------------------------
runner.on("start", () => {
watch.data.initialize(config);
});
runner.on("suite", suite => {
++indents;
log(color("suite", "%s%s"), indent(), suite.title);
});
runner.on("suite end", () => {
--indents;
if (indents === 1) {
log();
}
});
runner.on("pending", test => {
let fmt = indent() + color("pending", " - %s");
log(fmt, test.title);
});
runner.on("test", () => {
watch.beforeStartBlock = sync.blockNumber();
watch.data.resetAddressCache();
});
runner.on("hook end", () => {
watch.itStartBlock = sync.blockNumber() + 1;
});
runner.on("pass", test => {
let fmt;
let fmtArgs;
let gasUsedString;
let consumptionString;
let timeSpentString = color(test.speed, "%dms");
const gasUsed = watch.blocks();
if (gasUsed) {
gasUsedString = color("checkmark", "%d gas");
if (config.showTimeSpent) {
consumptionString = " (" + timeSpentString + ", " + gasUsedString + ")";
fmtArgs = [test.title, test.duration, gasUsed];
} else {
consumptionString = " (" + gasUsedString + ")";
fmtArgs = [test.title, gasUsed];
}
fmt =
indent() +
color("checkmark", " " + Base.symbols.ok) +
color("pass", " %s") +
consumptionString;
} else {
if (config.showTimeSpent) {
consumptionString = " (" + timeSpentString + ")";
fmtArgs = [test.title, test.duration];
} else {
consumptionString = "";
fmtArgs = [test.title];
}
fmt =
indent() +
color("checkmark", " " + Base.symbols.ok) +
color("pass", " %s") +
consumptionString;
}
log.apply(null, [fmt, ...fmtArgs]);
});
runner.on("fail", test => {
failed = true;
let fmt = indent() + color("fail", " %d) %s");
log();
log(fmt, ++n, test.title);
});
runner.on("end", () => {
table.generate(watch.data);
self.epilogue();
});
}
/**
* Inherit from `Base.prototype`.
*/
inherits(Gas, Base);
module.exports = Gas;