Skip to content

Commit 6186b3e

Browse files
mscdexruyadorno
authored andcommitted
benchmark: introduce benchmark combination filtering
PR-URL: #45735 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Daeyeon Jeong <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent e2698c0 commit 6186b3e

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

benchmark/common.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
const child_process = require('child_process');
44
const http_benchmarkers = require('./_http-benchmarkers.js');
55

6+
function allow() {
7+
return true;
8+
}
9+
610
class Benchmark {
711
constructor(fn, configs, options = {}) {
812
// Used to make sure a benchmark only start a timer once
@@ -31,9 +35,17 @@ class Benchmark {
3135
this.flags = this.flags.concat(options.flags);
3236
}
3337

38+
if (typeof options.combinationFilter === 'function')
39+
this.combinationFilter = options.combinationFilter;
40+
else
41+
this.combinationFilter = allow;
42+
3443
// The configuration list as a queue of jobs
3544
this.queue = this._queue(this.options);
3645

46+
if (this.queue.length === 0)
47+
return;
48+
3749
// The configuration of the current job, head of the queue
3850
this.config = this.queue[0];
3951

@@ -108,6 +120,7 @@ class Benchmark {
108120
_queue(options) {
109121
const queue = [];
110122
const keys = Object.keys(options);
123+
const { combinationFilter } = this;
111124

112125
// Perform a depth-first walk through all options to generate a
113126
// configuration list that contains all combinations.
@@ -131,7 +144,15 @@ class Benchmark {
131144
if (keyIndex + 1 < keys.length) {
132145
recursive(keyIndex + 1, currConfig);
133146
} else {
134-
queue.push(currConfig);
147+
// Check if we should allow the current combination
148+
const allowed = combinationFilter({ ...currConfig });
149+
if (typeof allowed !== 'boolean') {
150+
throw new TypeError(
151+
'Combination filter must always return a boolean'
152+
);
153+
}
154+
if (allowed)
155+
queue.push(currConfig);
135156
}
136157
}
137158
}

doc/contributing/writing-and-running-benchmarks.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,12 @@ The arguments of `createBenchmark` are:
450450
possible combinations of these parameters, unless specified otherwise.
451451
Each configuration is a property with an array of possible values.
452452
The configuration values can only be strings or numbers.
453-
* `options` {Object} The benchmark options. At the moment only the `flags`
454-
option for specifying command line flags is supported.
453+
* `options` {Object} The benchmark options. Supported options:
454+
* `flags` {Array} Contains node-specific command line flags to pass to
455+
the child process.
456+
* `combinationFilter` {Function} Has a single parameter which is an object
457+
containing a combination of benchmark parameters. It should return `true`
458+
or `false` to indicate whether the combination should be included or not.
455459

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

0 commit comments

Comments
 (0)