Skip to content

Commit 96d8e92

Browse files
committed
benchmark: new options setup and teardown
This commit introduces two new options to the Benchmark tool: setup and teardown. The setup option runs before any forks are created, and teardown executes after all forks have completed.
1 parent c8d5b39 commit 96d8e92

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

benchmark/common.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ function allow() {
99

1010
class Benchmark {
1111
constructor(fn, configs, options = {}) {
12+
this.setup = options.setup;
13+
this.teardown = options.teardown;
1214
// Used to make sure a benchmark only start a timer once
1315
this._started = false;
1416

@@ -62,6 +64,9 @@ class Benchmark {
6264
if (process.env.NODE_RUN_BENCHMARK_FN !== undefined) {
6365
fn(this.config);
6466
} else {
67+
if (this.setup) {
68+
this.setup();
69+
}
6570
// _run will use fork() to create a new process for each configuration
6671
// combination.
6772
this._run();
@@ -234,6 +239,8 @@ class Benchmark {
234239

235240
if (queueIndex + 1 < this.queue.length) {
236241
recursive(queueIndex + 1);
242+
} else if (this.teardown) {
243+
this.teardown();
237244
}
238245
});
239246
};

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,14 @@ The arguments of `createBenchmark` are:
523523
Each configuration is a property with an array of possible values.
524524
The configuration values can only be strings or numbers.
525525
* `options` {Object} The benchmark options. Supported options:
526+
* `setup` {Function} A function to be run once by the main "controller"
527+
process before any benchmark child processes are spawned. Ideal for
528+
preparing the environment for the entire benchmark suite.
529+
530+
* `teardown` {Function} A function to be run once by the main "controller"
531+
process after all benchmark child processes have completed. Ideal for
532+
cleaning up the environment.
533+
526534
* `flags` {Array} Contains node-specific command line flags to pass to
527535
the child process.
528536

@@ -585,6 +593,13 @@ const configs = {
585593
const options = {
586594
// Add --expose-internals in order to require internal modules in main
587595
flags: ['--zero-fill-buffers'],
596+
// Setup and teardown functions are run in the parent process, once.
597+
setup() {
598+
console.log('Running setup.');
599+
},
600+
teardown() {
601+
console.log('Running teardown.');
602+
},
588603
};
589604

590605
// `main` and `configs` are required, `options` is optional.

0 commit comments

Comments
 (0)