GeezerVison is showing github.com — benchmark: add per-suite setup option · nodejs/node@26a5305
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/common.js +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,7 @@ class Benchmark { | 28 | 28 | const argv = process.argv.slice(2); | 29 | 29 | const parsed_args = this._parseArgs(argv, configs, options); | 30 | 30 |
| | 31 | + this.originalOptions = options; | 31 | 32 | this.options = parsed_args.cli; | 32 | 33 | this.extra_options = parsed_args.extra; | 33 | 34 | this.combinationFilter = typeof options.combinationFilter === 'function' ? options.combinationFilter : allow; | @@ -207,6 +208,12 @@ class Benchmark { | 207 | 208 | }); | 208 | 209 | } | 209 | 210 |
| | 211 | + if (this.originalOptions.setup) { | | 212 | + // Only do this from the root process. _run() is only ever called from the root, | | 213 | + // in child processes main is run directly. | | 214 | + this.originalOptions.setup(this.queue); | | 215 | + } | | 216 | + | 210 | 217 | const recursive = (queueIndex) => { | 211 | 218 | const config = this.queue[queueIndex]; | 212 | 219 |
| |
Collapse file Copy file name to clipboardExpand all lines: benchmark/module/module-require.js +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,12 +8,15 @@ const benchmarkDirectory = tmpdir.resolve('nodejs-benchmark-module'); | 8 | 8 | const bench = common.createBenchmark(main, { | 9 | 9 | type: ['.js', '.json', 'dir'], | 10 | 10 | n: [1e4], | | 11 | +}, { | | 12 | + setup(configs) { | | 13 | + tmpdir.refresh(); | | 14 | + const maxN = configs.reduce((max, c) => Math.max(max, c.n), 0); | | 15 | + createEntryPoint(maxN); | | 16 | + }, | 11 | 17 | }); | 12 | 18 |
| 13 | 19 | function main({ type, n }) { | 14 | | - tmpdir.refresh(); | 15 | | - createEntryPoint(n); | 16 | | - | 17 | 20 | switch (type) { | 18 | 21 | case '.js': | 19 | 22 | measureJSFile(n); | @@ -24,8 +27,6 @@ function main({ type, n }) { | 24 | 27 | case 'dir': | 25 | 28 | measureDir(n); | 26 | 29 | } | 27 | | - | 28 | | - tmpdir.refresh(); | 29 | 30 | } | 30 | 31 |
| 31 | 32 | function measureJSFile(n) { | |
Collapse file Copy file name to clipboardExpand all lines: doc/contributing/writing-and-running-benchmarks.md +26Lines changed: 26 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -591,6 +591,32 @@ The arguments of `createBenchmark` are: | 591 | 591 | containing a combination of benchmark parameters. It should return `true` | 592 | 592 | or `false` to indicate whether the combination should be included or not. | 593 | 593 |
| | 594 | + * `setup` {Function} A function that will be run once in the root process | | 595 | + before the benchmark combinations are executed in child processes. | | 596 | + It can be used to setup any global state required by the benchmark. Note | | 597 | + that the JavaScript heap state will not be shared with the benchmark processes, | | 598 | + so don't try to access any variables created in the `setup` function from | | 599 | + the `main` function, for example. | | 600 | + The argument passed into it is an array of all the combinations of | | 601 | + configurations that will be executed. | | 602 | + If tear down is necessary, register a listener for the `exit` event on | | 603 | + `process` inside the `setup` function. In the example below, that's done | | 604 | + by `tmpdir.refresh()`. | | 605 | + | | 606 | + ```js | | 607 | + const tmpdir = require('../../test/common/tmpdir'); | | 608 | + const bench = common.createBenchmark(main, { | | 609 | + type: ['fast', 'slow'], | | 610 | + n: [1e4], | | 611 | + }, { | | 612 | + setup(configs) { | | 613 | + tmpdir.refresh(); | | 614 | + const maxN = configs.reduce((max, c) => Math.max(max, c.n), 0); | | 615 | + setupFixturesReusedForAllBenchmarks(maxN); | | 616 | + }, | | 617 | + }); | | 618 | + ``` | | 619 | + | 594 | 620 | `createBenchmark` returns a `bench` object, which is used for timing | 595 | 621 | the runtime of the benchmark. Run `bench.start()` after the initialization | 596 | 622 | and `bench.end(n)` when the benchmark is done. `n` is the number of operations | |
|