Skip to content

Commit

Permalink
benchmark: introduce benchmark combination filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
mscdex committed Dec 4, 2022
1 parent 7811d2d commit 096af8f
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 096af8f

Please sign in to comment.