zbench-js 0.1.0 → 0.2.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/README.md +31 -9
- package/dist/cli.js +110 -101
- package/dist/compare.d.ts +29 -9
- package/dist/compare.js +59 -39
- package/dist/config.d.ts +80 -55
- package/dist/config.js +161 -117
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/isolate.d.ts +8 -12
- package/dist/isolate.js +31 -62
- package/dist/measure.d.ts +3 -1
- package/dist/measure.js +3 -0
- package/dist/refs.d.ts +1 -8
- package/dist/refs.js +37 -44
- package/dist/report.d.ts +8 -2
- package/dist/report.js +62 -29
- package/dist/types.d.ts +18 -0
- package/dist/units.d.ts +3 -3
- package/dist/units.js +2 -2
- package/dist/worker.d.ts +3 -0
- package/dist/worker.js +114 -0
- package/package.json +4 -3
- package/dist/pool.d.ts +0 -5
- package/dist/pool.js +0 -15
- package/dist/runner.d.ts +0 -40
- package/dist/runner.js +0 -121
package/dist/runner.js
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
import { pathToFileURL } from 'node:url';
|
|
4
|
-
import { flagCombinations } from './config.js';
|
|
5
|
-
/** `import()` the test module and check that it is one. */
|
|
6
|
-
export async function loadTest(test) {
|
|
7
|
-
const module = (await import(pathToFileURL(test.file).href));
|
|
8
|
-
if (typeof module.test != 'function')
|
|
9
|
-
throw new Error(`${test.path} does not export a \`test\` function`);
|
|
10
|
-
return module;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Time one configuration.
|
|
14
|
-
* `setup`/`teardown` run once, `before`/`after` run around each iteration, and only `test` is timed.
|
|
15
|
-
*/
|
|
16
|
-
async function runCase(module, test, flags, configuration, options) {
|
|
17
|
-
const config = { ...flags, ...configuration.value };
|
|
18
|
-
const iterations = options.iterations ?? test.iterations;
|
|
19
|
-
const warmup = options.warmup ?? test.warmup;
|
|
20
|
-
const result = {
|
|
21
|
-
test: test.id,
|
|
22
|
-
flags,
|
|
23
|
-
configuration: configuration.name,
|
|
24
|
-
value: configuration.value,
|
|
25
|
-
setup: 0,
|
|
26
|
-
samples: [],
|
|
27
|
-
amounts: {},
|
|
28
|
-
};
|
|
29
|
-
// By key, not by metric: a key with both a per-configuration and an aggregate metric is one quantity
|
|
30
|
-
const quantities = [...new Set(test.metrics.map(metric => metric.key))];
|
|
31
|
-
const setupStart = performance.now();
|
|
32
|
-
let state;
|
|
33
|
-
try {
|
|
34
|
-
state = await module.setup?.(config);
|
|
35
|
-
}
|
|
36
|
-
catch (e) {
|
|
37
|
-
result.error = `setup: ${e?.stack ?? e}`;
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
result.setup = performance.now() - setupStart;
|
|
41
|
-
try {
|
|
42
|
-
for (let i = 0; i < warmup + iterations; i++) {
|
|
43
|
-
const timed = i >= warmup;
|
|
44
|
-
await module.before?.(config, state);
|
|
45
|
-
globalThis.gc?.();
|
|
46
|
-
const start = performance.now();
|
|
47
|
-
const amounts = await module.test(config, state);
|
|
48
|
-
const elapsed = performance.now() - start;
|
|
49
|
-
await module.after?.(config, state);
|
|
50
|
-
if (!timed)
|
|
51
|
-
continue;
|
|
52
|
-
result.samples.push(elapsed);
|
|
53
|
-
for (const key of quantities) {
|
|
54
|
-
const amount = amounts?.[key] ?? configuration.value[key];
|
|
55
|
-
if (amount === undefined)
|
|
56
|
-
continue;
|
|
57
|
-
result.amounts[key] = (result.amounts[key] ?? 0) + amount;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
catch (e) {
|
|
62
|
-
result.error = String(e?.stack ?? e);
|
|
63
|
-
}
|
|
64
|
-
finally {
|
|
65
|
-
try {
|
|
66
|
-
await module.teardown?.(config, state);
|
|
67
|
-
}
|
|
68
|
-
catch (e) {
|
|
69
|
-
result.error ??= `teardown: ${e?.stack ?? e}`;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
/** Whether a configuration fits in the budget, and why not when it doesn't. */
|
|
75
|
-
export function skipReason(configuration, options) {
|
|
76
|
-
if (options.all)
|
|
77
|
-
return undefined;
|
|
78
|
-
if (options.cpu !== undefined && configuration.cpu > options.cpu)
|
|
79
|
-
return `needs cpu ${configuration.cpu}`;
|
|
80
|
-
if (options.mem !== undefined && configuration.mem > options.mem)
|
|
81
|
-
return `needs mem ${configuration.mem}`;
|
|
82
|
-
return undefined;
|
|
83
|
-
}
|
|
84
|
-
/** Narrow a test's flags to the values the caller asked for, dropping the rest. */
|
|
85
|
-
function selectFlags(test, options) {
|
|
86
|
-
const flags = {};
|
|
87
|
-
for (const [name, values] of Object.entries(test.flags)) {
|
|
88
|
-
const wanted = options.flags?.[name];
|
|
89
|
-
flags[name] = wanted ? values.filter(v => wanted.some(w => Object.is(w, v))) : values;
|
|
90
|
-
}
|
|
91
|
-
return flagCombinations(flags);
|
|
92
|
-
}
|
|
93
|
-
/** Run every configuration of a test, once per flag combination. */
|
|
94
|
-
export async function runTest(test, options = {}) {
|
|
95
|
-
const module = await loadTest(test);
|
|
96
|
-
const combinations = selectFlags(test, options);
|
|
97
|
-
const results = [];
|
|
98
|
-
const total = combinations.length * test.configurations.length;
|
|
99
|
-
let index = 0;
|
|
100
|
-
for (const flags of combinations) {
|
|
101
|
-
for (const configuration of test.configurations) {
|
|
102
|
-
options.onCase?.(test, flags, configuration.name, index++, total);
|
|
103
|
-
const skipped = skipReason(configuration, options);
|
|
104
|
-
if (skipped) {
|
|
105
|
-
results.push({
|
|
106
|
-
test: test.id,
|
|
107
|
-
flags,
|
|
108
|
-
configuration: configuration.name,
|
|
109
|
-
value: configuration.value,
|
|
110
|
-
setup: 0,
|
|
111
|
-
samples: [],
|
|
112
|
-
amounts: {},
|
|
113
|
-
skipped,
|
|
114
|
-
});
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
results.push(await runCase(module, test, flags, configuration, options));
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return results;
|
|
121
|
-
}
|