| 9 | |
| 10 | class Benchmark { |
| 11 | constructor(fn, configs, options = {}) { |
| 12 | // Used to make sure a benchmark only start a timer once |
| 13 | this._started = false; |
| 14 | |
| 15 | // Indicate that the benchmark ended |
| 16 | this._ended = false; |
| 17 | |
| 18 | // Holds process.hrtime value |
| 19 | this._time = 0n; |
| 20 | |
| 21 | // Use the file name as the name of the benchmark |
| 22 | this.name = require.main.filename.slice(__dirname.length + 1); |
| 23 | |
| 24 | // Execution arguments i.e. flags used to run the jobs |
| 25 | this.flags = process.env.NODE_BENCHMARK_FLAGS?.split(/\s+/) ?? []; |
| 26 | |
| 27 | // Parse job-specific configuration from the command line arguments |
| 28 | const argv = process.argv.slice(2); |
| 29 | const parsed_args = this._parseArgs(argv, configs, options); |
| 30 | |
| 31 | this.originalOptions = options; |
| 32 | this.options = parsed_args.cli; |
| 33 | this.extra_options = parsed_args.extra; |
| 34 | this.combinationFilter = typeof options.combinationFilter === 'function' ? options.combinationFilter : allow; |
| 35 | |
| 36 | if (options.byGroups) { |
| 37 | this.queue = []; |
| 38 | const groupNames = process.env.NODE_RUN_BENCHMARK_GROUPS?.split(',') ?? Object.keys(configs); |
| 39 | |
| 40 | for (const groupName of groupNames) { |
| 41 | const config = { ...configs[groupName][0], group: groupName }; |
| 42 | const parsed_args = this._parseArgs(argv, config, options); |
| 43 | |
| 44 | this.options = parsed_args.cli; |
| 45 | this.extra_options = parsed_args.extra; |
| 46 | this.queue = this.queue.concat(this._queue(this.options)); |
| 47 | } |
| 48 | } else { |
| 49 | this.queue = this._queue(this.options); |
| 50 | } |
| 51 | |
| 52 | if (options.flags) { |
| 53 | this.flags = this.flags.concat(options.flags); |
| 54 | } |
| 55 | |
| 56 | if (this.queue.length === 0) |
| 57 | return; |
| 58 | |
| 59 | // The configuration of the current job, head of the queue |
| 60 | this.config = this.queue[0]; |
| 61 | |
| 62 | process.nextTick(() => { |
| 63 | if (process.env.NODE_RUN_BENCHMARK_FN !== undefined) { |
| 64 | fn(this.config); |
| 65 | } else { |
| 66 | // _run will use fork() to create a new process for each configuration |
| 67 | // combination. |
| 68 | this._run(); |