From 96d8e9284ef6983f8e357d66da5d4c3c7086ab0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?0hm=E2=98=98=EF=B8=8F?= Date: Sat, 19 Jul 2025 22:23:47 +0530 Subject: [PATCH 1/2] 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. --- benchmark/common.js | 7 +++++++ .../writing-and-running-benchmarks.md | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/benchmark/common.js b/benchmark/common.js index 0d3dbef24d07ad..a9c5e58183dafe 100644 --- a/benchmark/common.js +++ b/benchmark/common.js @@ -9,6 +9,8 @@ function allow() { class Benchmark { constructor(fn, configs, options = {}) { + this.setup = options.setup; + this.teardown = options.teardown; // Used to make sure a benchmark only start a timer once this._started = false; @@ -62,6 +64,9 @@ class Benchmark { if (process.env.NODE_RUN_BENCHMARK_FN !== undefined) { fn(this.config); } else { + if (this.setup) { + this.setup(); + } // _run will use fork() to create a new process for each configuration // combination. this._run(); @@ -234,6 +239,8 @@ class Benchmark { if (queueIndex + 1 < this.queue.length) { recursive(queueIndex + 1); + } else if (this.teardown) { + this.teardown(); } }); }; diff --git a/doc/contributing/writing-and-running-benchmarks.md b/doc/contributing/writing-and-running-benchmarks.md index 2664116aefc4b3..1b32d869147c73 100644 --- a/doc/contributing/writing-and-running-benchmarks.md +++ b/doc/contributing/writing-and-running-benchmarks.md @@ -523,6 +523,14 @@ The arguments of `createBenchmark` are: 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. Supported options: + * `setup` {Function} A function to be run once by the main "controller" + process before any benchmark child processes are spawned. Ideal for + preparing the environment for the entire benchmark suite. + + * `teardown` {Function} A function to be run once by the main "controller" + process after all benchmark child processes have completed. Ideal for + cleaning up the environment. + * `flags` {Array} Contains node-specific command line flags to pass to the child process. @@ -585,6 +593,13 @@ const configs = { const options = { // Add --expose-internals in order to require internal modules in main flags: ['--zero-fill-buffers'], + // Setup and teardown functions are run in the parent process, once. + setup() { + console.log('Running setup.'); + }, + teardown() { + console.log('Running teardown.'); + }, }; // `main` and `configs` are required, `options` is optional. From 0bd0afc2ed864e54b72e02c5488499bdbcc586b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?0hm=E2=98=98=EF=B8=8F?= Date: Sat, 19 Jul 2025 22:25:53 +0530 Subject: [PATCH 2/2] benchmark: updates to use the new setup and teardown options --- benchmark/module/module-require.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/benchmark/module/module-require.js b/benchmark/module/module-require.js index 5346366dc8e8b7..62dee9c8b7f954 100644 --- a/benchmark/module/module-require.js +++ b/benchmark/module/module-require.js @@ -5,15 +5,22 @@ const common = require('../common.js'); const tmpdir = require('../../test/common/tmpdir'); const benchmarkDirectory = tmpdir.resolve('nodejs-benchmark-module'); +const t = 1e4; + const bench = common.createBenchmark(main, { type: ['.js', '.json', 'dir'], - n: [1e4], + n: [t], +}, { + setup() { + tmpdir.refresh(); + createEntryPoint(t); + }, + teardown() { + tmpdir.refresh(); + }, }); function main({ type, n }) { - tmpdir.refresh(); - createEntryPoint(n); - switch (type) { case '.js': measureJSFile(n); @@ -24,8 +31,6 @@ function main({ type, n }) { case 'dir': measureDir(n); } - - tmpdir.refresh(); } function measureJSFile(n) {