File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,8 @@ function allow() {
99
1010class 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 . globalTeardown ) {
243+ this . teardown ( ) ;
237244 }
238245 } ) ;
239246 } ;
Original file line number Diff line number Diff 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 = {
585593const 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 global setup.' );
599+ },
600+ teardown () {
601+ console .log (' Running global teardown.' );
602+ },
588603};
589604
590605// `main` and `configs` are required, `options` is optional.
You can’t perform that action at this time.
0 commit comments