zbench-js 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,157 @@
1
+ # GNU LESSER GENERAL PUBLIC LICENSE
2
+
3
+ Version 3, 29 June 2007
4
+
5
+ Copyright (C) 2007 Free Software Foundation, Inc.
6
+ <https://fsf.org/>
7
+
8
+ Everyone is permitted to copy and distribute verbatim copies of this
9
+ license document, but changing it is not allowed.
10
+
11
+ This version of the GNU Lesser General Public License incorporates the
12
+ terms and conditions of version 3 of the GNU General Public License,
13
+ supplemented by the additional permissions listed below.
14
+
15
+ ## 0. Additional Definitions.
16
+
17
+ As used herein, "this License" refers to version 3 of the GNU Lesser
18
+ General Public License, and the "GNU GPL" refers to version 3 of the
19
+ GNU General Public License.
20
+
21
+ "The Library" refers to a covered work governed by this License, other
22
+ than an Application or a Combined Work as defined below.
23
+
24
+ An "Application" is any work that makes use of an interface provided
25
+ by the Library, but which is not otherwise based on the Library.
26
+ Defining a subclass of a class defined by the Library is deemed a mode
27
+ of using an interface provided by the Library.
28
+
29
+ A "Combined Work" is a work produced by combining or linking an
30
+ Application with the Library. The particular version of the Library
31
+ with which the Combined Work was made is also called the "Linked
32
+ Version".
33
+
34
+ The "Minimal Corresponding Source" for a Combined Work means the
35
+ Corresponding Source for the Combined Work, excluding any source code
36
+ for portions of the Combined Work that, considered in isolation, are
37
+ based on the Application, and not on the Linked Version.
38
+
39
+ The "Corresponding Application Code" for a Combined Work means the
40
+ object code and/or source code for the Application, including any data
41
+ and utility programs needed for reproducing the Combined Work from the
42
+ Application, but excluding the System Libraries of the Combined Work.
43
+
44
+ ## 1. Exception to Section 3 of the GNU GPL.
45
+
46
+ You may convey a covered work under sections 3 and 4 of this License
47
+ without being bound by section 3 of the GNU GPL.
48
+
49
+ ## 2. Conveying Modified Versions.
50
+
51
+ If you modify a copy of the Library, and, in your modifications, a
52
+ facility refers to a function or data to be supplied by an Application
53
+ that uses the facility (other than as an argument passed when the
54
+ facility is invoked), then you may convey a copy of the modified
55
+ version:
56
+
57
+ - a) under this License, provided that you make a good faith effort
58
+ to ensure that, in the event an Application does not supply the
59
+ function or data, the facility still operates, and performs
60
+ whatever part of its purpose remains meaningful, or
61
+ - b) under the GNU GPL, with none of the additional permissions of
62
+ this License applicable to that copy.
63
+
64
+ ## 3. Object Code Incorporating Material from Library Header Files.
65
+
66
+ The object code form of an Application may incorporate material from a
67
+ header file that is part of the Library. You may convey such object
68
+ code under terms of your choice, provided that, if the incorporated
69
+ material is not limited to numerical parameters, data structure
70
+ layouts and accessors, or small macros, inline functions and templates
71
+ (ten or fewer lines in length), you do both of the following:
72
+
73
+ - a) Give prominent notice with each copy of the object code that
74
+ the Library is used in it and that the Library and its use are
75
+ covered by this License.
76
+ - b) Accompany the object code with a copy of the GNU GPL and this
77
+ license document.
78
+
79
+ ## 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that, taken
82
+ together, effectively do not restrict modification of the portions of
83
+ the Library contained in the Combined Work and reverse engineering for
84
+ debugging such modifications, if you also do each of the following:
85
+
86
+ - a) Give prominent notice with each copy of the Combined Work that
87
+ the Library is used in it and that the Library and its use are
88
+ covered by this License.
89
+ - b) Accompany the Combined Work with a copy of the GNU GPL and this
90
+ license document.
91
+ - c) For a Combined Work that displays copyright notices during
92
+ execution, include the copyright notice for the Library among
93
+ these notices, as well as a reference directing the user to the
94
+ copies of the GNU GPL and this license document.
95
+ - d) Do one of the following:
96
+ - 0) Convey the Minimal Corresponding Source under the terms of
97
+ this License, and the Corresponding Application Code in a form
98
+ suitable for, and under terms that permit, the user to
99
+ recombine or relink the Application with a modified version of
100
+ the Linked Version to produce a modified Combined Work, in the
101
+ manner specified by section 6 of the GNU GPL for conveying
102
+ Corresponding Source.
103
+ - 1) Use a suitable shared library mechanism for linking with
104
+ the Library. A suitable mechanism is one that (a) uses at run
105
+ time a copy of the Library already present on the user's
106
+ computer system, and (b) will operate properly with a modified
107
+ version of the Library that is interface-compatible with the
108
+ Linked Version.
109
+ - e) Provide Installation Information, but only if you would
110
+ otherwise be required to provide such information under section 6
111
+ of the GNU GPL, and only to the extent that such information is
112
+ necessary to install and execute a modified version of the
113
+ Combined Work produced by recombining or relinking the Application
114
+ with a modified version of the Linked Version. (If you use option
115
+ 4d0, the Installation Information must accompany the Minimal
116
+ Corresponding Source and Corresponding Application Code. If you
117
+ use option 4d1, you must provide the Installation Information in
118
+ the manner specified by section 6 of the GNU GPL for conveying
119
+ Corresponding Source.)
120
+
121
+ ## 5. Combined Libraries.
122
+
123
+ You may place library facilities that are a work based on the Library
124
+ side by side in a single library together with other library
125
+ facilities that are not Applications and are not covered by this
126
+ License, and convey such a combined library under terms of your
127
+ choice, if you do both of the following:
128
+
129
+ - a) Accompany the combined library with a copy of the same work
130
+ based on the Library, uncombined with any other library
131
+ facilities, conveyed under the terms of this License.
132
+ - b) Give prominent notice with the combined library that part of it
133
+ is a work based on the Library, and explaining where to find the
134
+ accompanying uncombined form of the same work.
135
+
136
+ ## 6. Revised Versions of the GNU Lesser General Public License.
137
+
138
+ The Free Software Foundation may publish revised and/or new versions
139
+ of the GNU Lesser General Public License from time to time. Such new
140
+ versions will be similar in spirit to the present version, but may
141
+ differ in detail to address new problems or concerns.
142
+
143
+ Each version is given a distinguishing version number. If the Library
144
+ as you received it specifies that a certain numbered version of the
145
+ GNU Lesser General Public License "or any later version" applies to
146
+ it, you have the option of following the terms and conditions either
147
+ of that published version or of any later version published by the
148
+ Free Software Foundation. If the Library as you received it does not
149
+ specify a version number of the GNU Lesser General Public License, you
150
+ may choose any version of the GNU Lesser General Public License ever
151
+ published by the Free Software Foundation.
152
+
153
+ If the Library as you received it specifies that a proxy can decide
154
+ whether future versions of the GNU Lesser General Public License shall
155
+ apply, that proxy's public statement of acceptance of any version is
156
+ permanent authorization for you to choose that version for the
157
+ Library.
package/README.md ADDED
@@ -0,0 +1,195 @@
1
+ # zbench
2
+
3
+ Reproducible, data-driven performance testing.
4
+
5
+ `node:test` is the wrong tool for benchmarks: it reports pass/fail, not throughput, and it has no
6
+ notion of a matrix, a baseline, or noise. zbench is the other tool. You describe the matrix in JSON,
7
+ write a `test` function, and it handles iteration, measurement, machine gating, process isolation,
8
+ and comparison across git references.
9
+
10
+ ```
11
+ npm i -D zbench-js
12
+ ```
13
+
14
+ ## The shape of a suite
15
+
16
+ A config file lists tests. Each test names a module and the configurations to run it under.
17
+
18
+ `tests/perf/config.json`:
19
+
20
+ ```json
21
+ {
22
+ "tests": [
23
+ {
24
+ "path": "unpack.ts",
25
+ "name": "Unpack a zip of random data",
26
+ "measure": {
27
+ "bytes": { "throughput": "s", "unit": "MB" },
28
+ "entries": { "cost": true, "aggregate_cost": true, "unit": "entry" }
29
+ },
30
+ "configurations": [
31
+ { "name": "1000x 128KB", "value": { "size": 131072, "entries": 1000 } },
32
+ { "value": { "size": 131072, "entries": 2000 } },
33
+ { "cpu": 1, "value": { "size": 524288, "entries": 1000 } },
34
+ { "mem": 1, "value": { "size": 131072, "entries": 16000 } }
35
+ ]
36
+ }
37
+ ]
38
+ }
39
+ ```
40
+
41
+ `tests/perf/unpack.ts`:
42
+
43
+ ```ts
44
+ interface Config {
45
+ size: number;
46
+ entries: number;
47
+ }
48
+
49
+ export function setup(config: Config) {
50
+ return buildArchive(config.entries, config.size);
51
+ }
52
+
53
+ export async function test(config: Config, archive: Archive) {
54
+ const unpacked = await unpack(archive);
55
+ return { bytes: unpacked.byteLength, entries: unpacked.count };
56
+ }
57
+ ```
58
+
59
+ Then `npx zbench`.
60
+
61
+ The config file is discovered at `tests/perf/config.json`, `tests/perf.json`,
62
+ `tests/perf.config.json`, or `zbench.json`, searching upward from the working directory. Test paths
63
+ resolve relative to it.
64
+
65
+ ## What a test module exports
66
+
67
+ Only `test` is required.
68
+
69
+ | Export | When it runs | Timed |
70
+ | ---------- | ---------------------- | ------------------------------- |
71
+ | `setup` | once per configuration | separately, reported as `setup` |
72
+ | `before` | before every iteration | no |
73
+ | `test` | the operation | **yes** |
74
+ | `after` | after every iteration | no |
75
+ | `teardown` | once per configuration | no |
76
+
77
+ `setup` returns state, which every other hook receives as its second argument. `before` is where
78
+ you restore anything the test consumes, so each iteration starts from the same place.
79
+
80
+ `test` receives the configuration's `value` merged with the active flags. It may return an object of
81
+ amounts — how much work it actually did — which overrides the amounts taken from the configuration.
82
+ Return them whenever the real amount is not known until the test runs.
83
+
84
+ Node strips types rather than resolving them, so a test that imports a sibling `.ts` file has to
85
+ name the real extension (`./fixtures.ts`). Set `allowImportingTsExtensions` in the tsconfig that
86
+ covers your tests.
87
+
88
+ ## Measurements
89
+
90
+ Every test reports `setup`, `total`, `avg`, `ops/s`, and `±` (relative standard deviation — the
91
+ noise floor) without asking. `measure` adds columns derived from the quantities the test works in.
92
+
93
+ | Key | Reports | Example |
94
+ | ---------------------- | ------------------------------------------- | ---------- |
95
+ | `throughput` | units per timespan, per configuration | `MB/s` |
96
+ | `cost` | timespan per unit, per configuration | `ms/entry` |
97
+ | `aggregate_throughput` | units per timespan, across the whole matrix | `MB/s` |
98
+ | `aggregate_cost` | timespan per unit, across the whole matrix | `ms/entry` |
99
+
100
+ Each takes `"ns"`, `"us"`, `"ms"`, `"s"`, or `true` to pick whichever timespan keeps the number
101
+ readable. The timespan is settled once per matrix, so a column has one heading.
102
+
103
+ `unit` names the quantity in the column heading and defaults to the key. Byte units (`B`, `KB`,
104
+ `MB`, `GB`, `TB`, `KiB`, `MiB`, ...) also scale the amount, so a test that counts bytes and declares
105
+ `"unit": "MB"` reports MB/s with no further arithmetic. `scale` sets the multiplier explicitly and
106
+ overrides that.
107
+
108
+ ## Flags
109
+
110
+ Flags are alternative code paths rather than points in the matrix: the whole matrix runs once per
111
+ combination, and each combination gets its own table. Declare them once and let tests draw from the
112
+ pool by name:
113
+
114
+ ```json
115
+ {
116
+ "flags": { "lazy": [true, false] },
117
+ "tests": [{ "path": "mount.ts", "flags": ["lazy"], "configurations": [] }]
118
+ }
119
+ ```
120
+
121
+ A test can also declare its own inline: `"flags": { "lazy": [true, false] }`. Flag values are merged
122
+ into the configuration the test receives. `-f lazy=true` restricts a run to one value.
123
+
124
+ ## Machine requirements
125
+
126
+ A configuration can say what it needs:
127
+
128
+ ```json
129
+ { "cpu": 2, "mem": 1, "value": { "entries": 32000 } }
130
+ ```
131
+
132
+ Both are logarithmic — level `n + 1` is about twice level `n`. zbench calibrates the machine at
133
+ startup and reports rows it cannot afford as `N/A`, so the same config file runs everywhere without
134
+ timing out a laptop. Override with `--cpu` / `--mem` (or `ZBENCH_CPU` / `ZBENCH_MEM`), or ignore the
135
+ gating entirely with `-a`.
136
+
137
+ ## Comparing states
138
+
139
+ ```
140
+ zbench -R v3.2.1 -R main -R HEAD
141
+ ```
142
+
143
+ Each reference is checked out into a cached worktree under `.zbench/`, built, and run. The first is
144
+ the baseline; every other column shows its value and a speedup factor that is `> 1` when better,
145
+ whichever direction the column improves in. A change is only colored when it is larger than both
146
+ `--threshold` (default 1%) and the two runs' combined noise — so a green number means the change
147
+ outran the variance, and a gray one means it did not.
148
+
149
+ Use `.` for the working tree as it is, without a checkout or a build.
150
+
151
+ The tests themselves always come from the working tree and are copied into each worktree, so an old
152
+ reference is benchmarked with today's tests. `--build` sets the command that makes a worktree
153
+ runnable (default `npm install --no-audit --no-fund && npm run build`), `--rebuild` forces it, and
154
+ `--clean` removes the cached worktrees.
155
+
156
+ ## Isolation and concurrency
157
+
158
+ Each matrix runs in a fresh process by default, so one configuration cannot warm up or poison the
159
+ next, and a reference's own build is what gets loaded. `--no-isolate` runs everything in-process for
160
+ debugging or profiling.
161
+
162
+ `-J` sets how many matrices are timed at once, defaulting to half the number of hardware threads. Concurrent
163
+ matrices contend for the machine, which inflates absolute numbers and widens `±`; the comparison
164
+ factors hold up because every reference runs under the same contention, but pass `-J 1` when the
165
+ absolute numbers are the point.
166
+
167
+ A matrix that crashes or outruns `--timeout` (default 300s) is reported as `N/A` rather than
168
+ bringing down the run, so the references that did finish are still compared. This matters when the
169
+ thing you are looking for is a quadratic: the configuration that takes 30 ms on the fix can take ten
170
+ minutes on the commit before it.
171
+
172
+ ## Options
173
+
174
+ ```
175
+ -c, --config <path> Config file. Discovered by default.
176
+ -n, --iterations <n> Timed runs per configuration.
177
+ -w, --warmup <n> Untimed runs before the timed ones.
178
+ -R, --ref <ref> Benchmark a git reference. Repeatable; the first one is the baseline.
179
+ -f, --flag <name=json> Restrict a flag to one value. Repeatable.
180
+ -t, --threshold <pct> Smallest change worth coloring. [1]
181
+ -T, --timeout <s> Seconds a matrix may take before it is killed and reported N/A. [300]
182
+ -a, --all Run every configuration, ignoring cpu/mem requirements.
183
+ -J, --jobs <n> Matrices to time at once.
184
+ -l, --list List what would run, then exit.
185
+ -j, --json <path> Write the raw results as JSON.
186
+ --cpu <n> Override the detected CPU level.
187
+ --mem <n> Override the detected memory level.
188
+ --no-isolate Run in this process instead of one child per matrix.
189
+ --build <cmd> Command that makes a reference's worktree runnable.
190
+ --rebuild Rebuild reference worktrees even when they are up to date.
191
+ --clean Remove cached reference worktrees, then exit.
192
+ -q, --quiet Only print results.
193
+ ```
194
+
195
+ Positional arguments filter tests by name or path.
@@ -0,0 +1,15 @@
1
+ /**
2
+ * What the current machine can be asked to run.
3
+ * A configuration is skipped when it asks for more than these.
4
+ */
5
+ export interface Capabilities {
6
+ cpu: number;
7
+ mem: number;
8
+ }
9
+ /**
10
+ * Measure what this machine can handle. Levels are logarithmic, so `n + 1` is about twice `n`.
11
+ * The result is cached, since calibration costs real time.
12
+ */
13
+ export declare function capabilities(): Capabilities;
14
+ /** Resolve the budget from overrides, the environment, and finally calibration. */
15
+ export declare function resolveCapabilities(overrides?: Partial<Capabilities>): Capabilities;
@@ -0,0 +1,47 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import { totalmem } from 'node:os';
3
+ import { performance } from 'node:perf_hooks';
4
+ /** Milliseconds the calibration loop takes on the machine level 0 is defined against. */
5
+ const cpuReference = 40;
6
+ /** Memory a level 0 machine is assumed to have. */
7
+ const memReference = 4 * 2 ** 30;
8
+ function calibrate() {
9
+ const run = () => {
10
+ let x = 0;
11
+ for (let i = 0; i < 5e6; i++)
12
+ x = (x + Math.imul(i, 2654435761)) >>> 0;
13
+ return x;
14
+ };
15
+ run(); // let the JIT settle before the timed pass
16
+ const start = performance.now();
17
+ run();
18
+ return performance.now() - start;
19
+ }
20
+ let detected;
21
+ /**
22
+ * Measure what this machine can handle. Levels are logarithmic, so `n + 1` is about twice `n`.
23
+ * The result is cached, since calibration costs real time.
24
+ */
25
+ export function capabilities() {
26
+ detected ??= {
27
+ cpu: Math.max(0, Math.round(Math.log2(cpuReference / calibrate()))),
28
+ mem: Math.max(0, Math.floor(Math.log2(totalmem() / memReference))),
29
+ };
30
+ return detected;
31
+ }
32
+ /** Resolve the budget from overrides, the environment, and finally calibration. */
33
+ export function resolveCapabilities(overrides = {}) {
34
+ const env = (name) => {
35
+ const value = process.env[name];
36
+ if (value === undefined)
37
+ return undefined;
38
+ const parsed = Number(value);
39
+ return Number.isFinite(parsed) ? parsed : undefined;
40
+ };
41
+ const cpu = overrides.cpu ?? env('ZBENCH_CPU');
42
+ const mem = overrides.mem ?? env('ZBENCH_MEM');
43
+ if (cpu !== undefined && mem !== undefined)
44
+ return { cpu, mem };
45
+ const auto = capabilities();
46
+ return { cpu: cpu ?? auto.cpu, mem: mem ?? auto.mem };
47
+ }
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};