Skip to content

Commit

Permalink
benchmark: introduce benchmark combination filtering
Browse files Browse the repository at this point in the history
PR-URL: #45735
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
  • Loading branch information
mscdex authored and juanarbol committed Mar 5, 2023
1 parent dbf082d commit 6468f30
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
23 changes: 22 additions & 1 deletion benchmark/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
const child_process = require('child_process');
const http_benchmarkers = require('./_http-benchmarkers.js');

function allow() {
return true;
}

class Benchmark {
constructor(fn, configs, options = {}) {
// Used to make sure a benchmark only start a timer once
Expand Down Expand Up @@ -31,9 +35,17 @@ class Benchmark {
this.flags = this.flags.concat(options.flags);
}

if (typeof options.combinationFilter === 'function')
this.combinationFilter = options.combinationFilter;
else
this.combinationFilter = allow;

// The configuration list as a queue of jobs
this.queue = this._queue(this.options);

if (this.queue.length === 0)
return;

// The configuration of the current job, head of the queue
this.config = this.queue[0];

Expand Down Expand Up @@ -108,6 +120,7 @@ class Benchmark {
_queue(options) {
const queue = [];
const keys = Object.keys(options);
const { combinationFilter } = this;

// Perform a depth-first walk through all options to generate a
// configuration list that contains all combinations.
Expand All @@ -131,7 +144,15 @@ class Benchmark {
if (keyIndex + 1 < keys.length) {
recursive(keyIndex + 1, currConfig);
} else {
queue.push(currConfig);
// Check if we should allow the current combination
const allowed = combinationFilter({ ...currConfig });
if (typeof allowed !== 'boolean') {
throw new TypeError(
'Combination filter must always return a boolean'
);
}
if (allowed)
queue.push(currConfig);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions doc/contributing/writing-and-running-benchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,12 @@ The arguments of `createBenchmark` are:
possible combinations of these parameters, unless specified otherwise.
Each configuration is a property with an array of possible values.
The configuration values can only be strings or numbers.
* `options` {Object} The benchmark options. At the moment only the `flags`
option for specifying command line flags is supported.
* `options` {Object} The benchmark options. Supported options:
* `flags` {Array} Contains node-specific command line flags to pass to
the child process.
* `combinationFilter` {Function} Has a single parameter which is an object
containing a combination of benchmark parameters. It should return `true`
or `false` to indicate whether the combination should be included or not.

`createBenchmark` returns a `bench` object, which is used for timing
the runtime of the benchmark. Run `bench.start()` after the initialization
Expand Down

0 comments on commit 6468f30

Please sign in to comment.