| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ea71e96 commit b6114ae
3 files changed
| 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 | ||
| 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) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -587,6 +587,32 @@ The arguments of `createBenchmark` are: | |||
| 587 | 587 | containing a combination of benchmark parameters. It should return `true` | |
| 588 | 588 | or `false` to indicate whether the combination should be included or not. | |
| 589 | 589 | ||
| 590 | + * `setup` {Function} A function that will be run once in the root process | ||
| 591 | + before the benchmark combinations are executed in child processes. | ||
| 592 | + It can be used to setup any global state required by the benchmark. Note | ||
| 593 | + that the JavaScript heap state will not be shared with the benchmark processes, | ||
| 594 | + so don't try to access any variables created in the `setup` function from | ||
| 595 | + the `main` function, for example. | ||
| 596 | + The argument passed into it is an array of all the combinations of | ||
| 597 | + configurations that will be executed. | ||
| 598 | + If tear down is necessary, register a listener for the `exit` event on | ||
| 599 | + `process` inside the `setup` function. In the example below, that's done | ||
| 600 | + by `tmpdir.refresh()`. | ||
| 601 | + | ||
| 602 | + ```js | ||
| 603 | + const tmpdir = require('../../test/common/tmpdir'); | ||
| 604 | + const bench = common.createBenchmark(main, { | ||
| 605 | + type: ['fast', 'slow'], | ||
| 606 | + n: [1e4], | ||
| 607 | + }, { | ||
| 608 | + setup(configs) { | ||
| 609 | + tmpdir.refresh(); | ||
| 610 | + const maxN = configs.reduce((max, c) => Math.max(max, c.n), 0); | ||
| 611 | + setupFixturesReusedForAllBenchmarks(maxN); | ||
| 612 | + }, | ||
| 613 | + }); | ||
| 614 | + ``` | ||
| 615 | + | ||
| 590 | 616 | `createBenchmark` returns a `bench` object, which is used for timing | |
| 591 | 617 | the runtime of the benchmark. Run `bench.start()` after the initialization | |
| 592 | 618 | and `bench.end(n)` when the benchmark is done. `n` is the number of operations | |
| Back | FazBrowse Home | New Git URL |
0 commit comments