Skip to content

Commit

Permalink
benchmark: check for and fix multiple end()
Browse files Browse the repository at this point in the history
PR-URL: #31624
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
mscdex authored and codebytere committed Mar 30, 2020
1 parent 708aff9 commit a305ae2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions benchmark/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function Benchmark(fn, configs, options) {
this._time = [0, 0];
// Used to make sure a benchmark only start a timer once
this._started = false;
this._ended = false;

// this._run will use fork() to create a new process for each configuration
// combination.
Expand Down Expand Up @@ -197,6 +198,9 @@ Benchmark.prototype.end = function(operations) {
if (!this._started) {
throw new Error('called end without start');
}
if (this._ended) {
throw new Error('called end multiple times');
}
if (typeof operations !== 'number') {
throw new Error('called end() without specifying operation count');
}
Expand All @@ -210,6 +214,7 @@ Benchmark.prototype.end = function(operations) {
elapsed[1] = 1;
}

this._ended = true;
const time = elapsed[0] + elapsed[1] / 1e9;
const rate = operations / time;
this.report(rate, elapsed);
Expand Down
4 changes: 3 additions & 1 deletion benchmark/streams/writable-manywrites.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ function main({ n, sync, writev, callback }) {
let k = 0;
function run() {
while (k++ < n && s.write(b, cb));
if (k >= n)
if (k >= n) {
bench.end(n);
s.removeListener('drain', run);
}
}
s.on('drain', run);
run();
Expand Down

0 comments on commit a305ae2

Please sign in to comment.