GeezerVison is showing github.com — lib: add gc options to node:bench · nodejs/node@426c758
File treeExpand file treeCollapse file tree Open diff view settings lib/internal/bench_runner
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/bench.md +44-5Lines changed: 44 additions & 5 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -90,6 +90,32 @@ system load can all affect results. Keep raw samples when comparing results and | 90 | 90 | investigate noisy or skewed distributions rather than treating a confidence | 91 | 91 | interval as a pass/fail threshold. | 92 | 92 |
| | 93 | +### Garbage collection | | 94 | + | | 95 | +Warmup invocations can leave garbage on the heap that is collected during the | | 96 | +first measured samples, charging warmup allocations to measured work. The `gc` | | 97 | +option forces a full garbage collection outside the measured region: | | 98 | + | | 99 | +* `'none'`: No garbage collection is forced. This is the default. | | 100 | +* `'once'`: A full garbage collection runs after warmup, before the first | | 101 | + measured sample. Garbage produced by measured samples is still collected | | 102 | + during measurement, so its cost remains part of the results. | | 103 | +* `'sample'`: A full garbage collection runs before every measured sample. This | | 104 | + reduces variance between samples, but samples that are too short to trigger a | | 105 | + collection on their own exclude the cost of collecting their garbage, which | | 106 | + can make allocation-heavy code appear faster than it is in practice. | | 107 | + | | 108 | +Forced collection runs after `beforeEach` hooks and any event loop turn between | | 109 | +samples, and is excluded from benchmark timeout accounting. The effective mode | | 110 | +is reported as `gc` in `'bench:plan'` events. The mode can be overridden for | | 111 | +every benchmark through `createRunner()`, [`run()`][], or [`--bench-gc`][]. | | 112 | + | | 113 | +Forcing garbage collection requires the `--expose-gc` flag, which also makes | | 114 | +`globalThis.gc()` available to benchmark code. A benchmark whose effective mode | | 115 | +is `'once'` or `'sample'` fails when the flag is not set. When `--bench-gc` | | 116 | +selects one of these modes, the command-line runner adds `--expose-gc` to | | 117 | +benchmark child processes automatically. | | 118 | + | 93 | 119 | ### Measurement integrity | 94 | 120 |
| 95 | 121 | A statistically consistent result does not prove that a benchmark measured the | @@ -221,9 +247,10 @@ arbitrary functions or closures to move them between isolates, because closures | 221 | 247 | cannot be reconstructed with their original lexical environment. | 222 | 248 |
| 223 | 249 | Benchmark files passed to `--bench` should declare benchmarks but must not call | 224 | | -`run()`. The CLI supports `--bench-name-pattern`, `--bench-samples`, | 225 | | -`--bench-warmup`, `--bench-reporter`, and `--bench-reporter-destination`. See | 226 | | -the [command-line options documentation][] for details. | | 250 | +`run()`. The CLI supports `--bench-gc`, `--bench-name-pattern`, | | 251 | +`--bench-samples`, `--bench-warmup`, `--bench-reporter`, and | | 252 | +`--bench-reporter-destination`. See the [command-line options documentation][] | | 253 | +for details. | 227 | 254 |
| 228 | 255 | Preload modules passed through `--require` or `--import` should not declare | 229 | 256 | benchmarks. Such declarations are not associated with an entry file and have | @@ -321,6 +348,9 @@ added: v26.9.0 | 321 | 348 | --> | 322 | 349 |
| 323 | 350 | * `options` {Object} | | 351 | + * `gc` {string} Overrides the [garbage collection][] mode for every | | 352 | + benchmark declared through this runner. One of `'none'`, `'once'`, or | | 353 | + `'sample'`. | 324 | 354 | * `yieldBetweenSamples` {boolean} Schedule an event loop turn between sample | 325 | 355 | callbacks. Disabling this also prevents timer-based abort signals from | 326 | 356 | firing between synchronous callbacks. Benchmark timeouts continue to be | @@ -334,8 +364,8 @@ the module-level functions. Call the returned `run()` function to start the | 334 | 364 | runner and obtain its {BenchmarksStream}. | 335 | 365 |
| 336 | 366 | Each runner can be started once. Its `run()` function accepts the same options | 337 | | -as the module-level [`run()`][]. `run({ yieldBetweenSamples })` overrides the | 338 | | -value passed to `createRunner()`. Unlike the module-level runner, a failed | | 367 | +as the module-level [`run()`][]. `run({ gc })` and `run({ yieldBetweenSamples })` | | 368 | +override the values passed to `createRunner()`. Unlike the module-level runner, a failed | 339 | 369 | benchmark in an explicit runner does not change the process exit code. | 340 | 370 |
| 341 | 371 | ## `bench([name][, options], fn)` | @@ -350,6 +380,8 @@ added: v26.9.0 | 350 | 380 | * `diagnosticChannels` {Array} String diagnostics channel names, deduplicated | 351 | 381 | and inherited from containing suites by union. Symbol values in the array | 352 | 382 | are silently ignored. **Default:** `[]`. | | 383 | + * `gc` {string} The [garbage collection][] mode. One of `'none'`, `'once'`, | | 384 | + or `'sample'`. **Default:** `'none'`. | 353 | 385 | * `only` {boolean} When any benchmark or containing suite has `only` set, | 354 | 386 | benchmarks without `only` in their hierarchy are skipped. **Default:** | 355 | 387 | `false`. | @@ -511,6 +543,9 @@ added: v26.9.0 | 511 | 543 | --> | 512 | 544 |
| 513 | 545 | * `options` {Object} | | 546 | + * `gc` {string} Overrides the [garbage collection][] mode for every | | 547 | + benchmark. One of `'none'`, `'once'`, or `'sample'`. **Default:** the value | | 548 | + passed to `createRunner()` for an explicit runner. | 514 | 549 | * `namePattern` {string|RegExp} Only runs benchmarks whose full hierarchical | 515 | 550 | name matches the pattern. String values are interpreted as JavaScript | 516 | 551 | regular expressions. | @@ -803,6 +838,8 @@ isolation, all files share one runner and their plans are emitted before any | 803 | 838 | benchmark executes. Plan data contains the benchmark-scoped identity, location, | 804 | 839 | tags, and parameters described in [benchmark result][], together with: | 805 | 840 |
| | 841 | +* `gc` {string} The effective [garbage collection][] mode after run-level | | 842 | + overrides. | 806 | 843 | * `diagnosticChannels` {string\[]} The inherited string channel names | 807 | 844 | subscribed to during each callback. | 808 | 845 | * `samples` {number} The effective maximum number of measured callback | @@ -878,10 +915,12 @@ A completed benchmark result contains: | 878 | 915 | interval for the median rate, with `lower` and `upper` properties. | 879 | 916 | * `skewness` {number} The skewness of the scaled rate histogram. | 880 | 917 |
| | 918 | +[`--bench-gc`]: cli.md#--bench-gcmode | 881 | 919 | [`context.record()`]: #contextrecordsample | 882 | 920 | [`createRunner()`]: #createrunneroptions | 883 | 921 | [`os`]: os.md | 884 | 922 | [`run()`]: #runoptions | 885 | 923 | [benchmark reporters]: #benchmark-reporters | 886 | 924 | [benchmark result]: #benchmark-result | 887 | 925 | [command-line options documentation]: cli.md#--bench | | 926 | +[garbage collection]: #garbage-collection |
Collapse file Copy file name to clipboardExpand all lines: doc/api/cli.md +20Lines changed: 20 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -554,6 +554,25 @@ details. | 554 | 554 | This flag cannot be combined with `--test`, `--watch`, `--watch-path`, | 555 | 555 | `--check`, `--eval`, or `--interactive`. | 556 | 556 |
| | 557 | +### `--bench-gc=mode` | | 558 | + | | 559 | +<!-- YAML | | 560 | +added: REPLACEME | | 561 | +--> | | 562 | + | | 563 | +> Stability: 1 - Experimental | | 564 | +
| | 565 | +Overrides the garbage collection mode for every selected benchmark. When `mode` | | 566 | +is `'once'`, a full garbage collection runs before the first measured sample. | | 567 | +When `mode` is `'sample'`, a full garbage collection runs before every measured | | 568 | +sample. When `mode` is `'none'`, no garbage collection is forced. Without this | | 569 | +option, each benchmark uses its own `gc` value, which defaults to `'none'`. | | 570 | + | | 571 | +Forcing garbage collection requires the `--expose-gc` flag. With process | | 572 | +isolation, `--expose-gc` is added to benchmark child processes automatically | | 573 | +when `mode` is `'once'` or `'sample'`. With `--bench-isolation=none`, | | 574 | +`--expose-gc` must be passed explicitly. | | 575 | + | 557 | 576 | ### `--bench-isolation=mode` | 558 | 577 |
| 559 | 578 | <!-- YAML | @@ -4283,6 +4302,7 @@ one is included in the list below. | 4283 | 4302 | * `--allow-openssl-store` | 4284 | 4303 | * `--allow-wasi` | 4285 | 4304 | * `--allow-worker` | | 4305 | +* `--bench-gc` | 4286 | 4306 | * `--bench-isolation` | 4287 | 4307 | * `--bench-name-pattern` | 4288 | 4308 | * `--bench-reporter-destination` | |
Collapse file Copy file name to clipboardExpand all lines: doc/node-config-schema.json +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 54 | 54 | "type": "boolean", | 55 | 55 | "description": "launch benchmark runner on startup" | 56 | 56 | }, | | 57 | + "bench-gc": { | | 58 | + "type": "string", | | 59 | + "description": "run garbage collection before measured benchmark samples" | | 60 | + }, | 57 | 61 | "bench-isolation": { | 58 | 62 | "type": "string", | 59 | 63 | "description": "configures the type of benchmark isolation used in the benchmark runner" | | 191 | 195 | "type": "boolean", | 192 | 196 | "description": "allow worker threads when any permissions are set" | 193 | 197 | }, | | 198 | + "bench-gc": { | | 199 | + "type": "string", | | 200 | + "description": "run garbage collection before measured benchmark samples" | | 201 | + }, | 194 | 202 | "bench-isolation": { | 195 | 203 | "type": "string", | 196 | 204 | "description": "configures the type of benchmark isolation used in the benchmark runner" | |
Collapse file Copy file name to clipboardExpand all lines: doc/node.1 +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -353,6 +353,17 @@ details. | 353 | 353 | This flag cannot be combined with \fB--test\fR, \fB--watch\fR, \fB--watch-path\fR, | 354 | 354 | \fB--check\fR, \fB--eval\fR, or \fB--interactive\fR. | 355 | 355 | . | | 356 | +.It Fl -bench-gc Ns = Ns Ar mode | | 357 | +Overrides the garbage collection mode for every selected benchmark. When \fBmode\fR | | 358 | +is \fB'once'\fR, a full garbage collection runs before the first measured sample. | | 359 | +When \fBmode\fR is \fB'sample'\fR, a full garbage collection runs before every measured | | 360 | +sample. When \fBmode\fR is \fB'none'\fR, no garbage collection is forced. Without this | | 361 | +option, each benchmark uses its own \fBgc\fR value, which defaults to \fB'none'\fR. | | 362 | +Forcing garbage collection requires the \fB--expose-gc\fR flag. With process | | 363 | +isolation, \fB--expose-gc\fR is added to benchmark child processes automatically | | 364 | +when \fBmode\fR is \fB'once'\fR or \fB'sample'\fR. With \fB--bench-isolation=none\fR, | | 365 | +\fB--expose-gc\fR must be passed explicitly. | | 366 | +. | 356 | 367 | .It Fl -bench-isolation Ns = Ns Ar mode | 357 | 368 | Configures benchmark file isolation. When \fBmode\fR is \fB'process'\fR, each matching | 358 | 369 | file runs in a separate child process. This is the default. Files are still run | @@ -2215,6 +2226,8 @@ one is included in the list below. | 2215 | 2226 | .It | 2216 | 2227 | \fB--allow-worker\fR | 2217 | 2228 | .It | | 2229 | +\fB--bench-gc\fR | | 2230 | +.It | 2218 | 2231 | \fB--bench-isolation\fR | 2219 | 2232 | .It | 2220 | 2233 | \fB--bench-name-pattern\fR | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bench_runner/benchmark.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,6 +42,7 @@ const { | 42 | 42 | validateInteger, | 43 | 43 | validateNumber, | 44 | 44 | validateObject, | | 45 | + validateOneOf, | 45 | 46 | validateString, | 46 | 47 | validateUint32, | 47 | 48 | } = require('internal/validators'); | @@ -50,6 +51,7 @@ const { structuredClone } = require('internal/worker/js_transferable'); | 50 | 51 | const { bigint: hrtime } = process.hrtime; | 51 | 52 | const kDefaultSamples = 30; | 52 | 53 | const kDefaultWarmup = 10; | | 54 | +const kGcModes = ObjectFreeze(['none', 'once', 'sample']); | 53 | 55 | const kEmptyDiagnosticChannels = ObjectFreeze([]); | 54 | 56 | const kEmptyNamePath = ObjectFreeze([]); | 55 | 57 | const kEmptyParams = ObjectFreeze({ __proto__: null }); | @@ -234,13 +236,15 @@ class Bench extends AsyncResource { | 234 | 236 | const validated = validateNodeOptions( | 235 | 237 | options, parent.tags, parent.diagnosticChannels); | 236 | 238 | const { | | 239 | + gc = 'none', | 237 | 240 | params, | 238 | 241 | samples = kDefaultSamples, | 239 | 242 | signal, | 240 | 243 | timeout = Infinity, | 241 | 244 | warmup = kDefaultWarmup, | 242 | 245 | } = options; | 243 | 246 |
| | 247 | + validateOneOf(gc, 'options.gc', kGcModes); | 244 | 248 | validateUint32(samples, 'options.samples', true); | 245 | 249 | validateUint32(warmup, 'options.warmup'); | 246 | 250 | validateAbortSignal(signal, 'options.signal'); | @@ -258,6 +262,7 @@ class Bench extends AsyncResource { | 258 | 262 | this.skip = validated.skip; | 259 | 263 | this.tags = validated.tags; | 260 | 264 | this.params = canonicalizeParams(params); | | 265 | + this.gc = gc; | 261 | 266 | this.samples = samples; | 262 | 267 | this.warmup = warmup; | 263 | 268 | this.timeout = timeout; | @@ -573,6 +578,7 @@ module.exports = { | 573 | 578 | Bench, | 574 | 579 | BenchContext, | 575 | 580 | Suite, | | 581 | + kGcModes, | 576 | 582 | normalizeArgs, | 577 | 583 | summarizeSamples, | 578 | 584 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bench_runner/cli.js +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,6 +44,9 @@ const { spawn } = require('child_process'); | 44 | 44 | const { createWriteStream, statSync } = require('fs'); | 45 | 45 | const { Glob } = require('internal/fs/glob'); | 46 | 46 | const { getValidatedPath } = require('internal/fs/utils'); | | 47 | +const { | | 48 | + kGcModes, | | 49 | +} = require('internal/bench_runner/benchmark'); | 47 | 50 | const { | 48 | 51 | BenchmarksStream, | 49 | 52 | } = require('internal/bench_runner/benchmarks_stream'); | @@ -112,6 +115,7 @@ const kFilterArgs = [ | 112 | 115 | '--experimental-default-config-file', | 113 | 116 | ]; | 114 | 117 | const kFilterArgValues = [ | | 118 | + '--bench-gc', | 115 | 119 | '--bench-isolation', | 116 | 120 | '--bench-name-pattern', | 117 | 121 | '--bench-reporter', | @@ -265,6 +269,7 @@ function parseCommandLine() { | 265 | 269 | destinations, | 266 | 270 | fileRunId: isChild && process.env.NODE_BENCH_FILE_RUN_ID ? | 267 | 271 | process.env.NODE_BENCH_FILE_RUN_ID : undefined, | | 272 | + gc: getOptionValue('--bench-gc') || undefined, | 268 | 273 | isChild, | 269 | 274 | isolation: getOptionValue('--bench-isolation'), | 270 | 275 | namePattern: parseNamePattern(getOptionValue('--bench-name-pattern')), | @@ -438,6 +443,7 @@ function validateRecord(record) { | 438 | 443 | } | 439 | 444 | if (record.type === 'bench:plan') { | 440 | 445 | const { | | 446 | + gc, | 441 | 447 | samples, | 442 | 448 | selected, | 443 | 449 | skip, | @@ -458,6 +464,7 @@ function validateRecord(record) { | 458 | 464 | ArrayPrototypeSome(ObjectValues(record.data.params), (value) => | 459 | 465 | typeof value !== 'string' && typeof value !== 'boolean' && | 460 | 466 | (typeof value !== 'number' || !NumberIsFinite(value))) || | | 467 | + !ArrayPrototypeIncludes(kGcModes, gc) || | 461 | 468 | !NumberIsSafeInteger(samples) || samples <= 0 || | 462 | 469 | samples > 0xFFFFFFFF || | 463 | 470 | !NumberIsSafeInteger(warmup) || warmup < 0 || warmup > 0xFFFFFFFF || | @@ -604,6 +611,7 @@ async function loadBenchmarkFiles(files, options, modules, onRecord) { | 604 | 611 |
| 605 | 612 | const stream = runBenchmarks({ | 606 | 613 | __proto__: null, | | 614 | + gc: options.gc, | 607 | 615 | namePattern: options.namePattern, | 608 | 616 | samples: options.samples, | 609 | 617 | warmup: options.warmup, | @@ -749,6 +757,10 @@ function getChildArgs(path, options) { | 749 | 757 | getInheritedChildArgs() : ArrayPrototypeSlice(options.execArgv); | 750 | 758 | ArrayPrototypePush( | 751 | 759 | args, '--experimental-bench', '--bench', '--bench-isolation=none'); | | 760 | + if (options.gc !== undefined) { | | 761 | + ArrayPrototypePush(args, `--bench-gc=${options.gc}`); | | 762 | + if (options.gc !== 'none') ArrayPrototypePush(args, '--expose-gc'); | | 763 | + } | 752 | 764 | if (options.namePatternSource.length > 0) { | 753 | 765 | ArrayPrototypePush( | 754 | 766 | args, `--bench-name-pattern=${options.namePatternSource}`); | |
|