overtake 2.0.0 → 2.0.2
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/bin/overtake.js +1 -1
- package/build/cli.js +157 -0
- package/build/executor.d.ts +2 -2
- package/build/executor.js +139 -0
- package/build/gc-watcher.js +16 -0
- package/build/index.d.ts +2 -2
- package/build/index.js +375 -0
- package/build/register-hook.d.ts +1 -0
- package/build/register-hook.js +15 -0
- package/build/reporter.d.ts +1 -1
- package/build/reporter.js +255 -0
- package/build/runner.d.ts +1 -1
- package/build/runner.js +528 -0
- package/build/types.js +28 -0
- package/build/utils.d.ts +1 -0
- package/build/utils.js +157 -0
- package/build/worker.js +111 -0
- package/package.json +5 -5
- package/src/__tests__/assert-no-closure.ts +1 -1
- package/src/__tests__/benchmark-execute.ts +2 -2
- package/src/cli.ts +33 -11
- package/src/executor.ts +21 -10
- package/src/index.ts +2 -2
- package/src/register-hook.ts +15 -0
- package/src/reporter.ts +2 -2
- package/src/runner.ts +3 -5
- package/src/utils.ts +60 -7
- package/src/worker.ts +3 -3
- package/tsconfig.json +0 -1
package/build/runner.js
ADDED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
import { performance, PerformanceObserver } from 'node:perf_hooks';
|
|
2
|
+
import { Control, DURATION_SCALE, COMPLETE_VALUE } from './types.js';
|
|
3
|
+
import { GCWatcher } from './gc-watcher.js';
|
|
4
|
+
const hr = process.hrtime.bigint.bind(process.hrtime);
|
|
5
|
+
const sink = new Int32Array(1);
|
|
6
|
+
const consume = (value) => {
|
|
7
|
+
let payload = 0;
|
|
8
|
+
switch (typeof value) {
|
|
9
|
+
case 'number':
|
|
10
|
+
payload = Number.isFinite(value) ? Math.trunc(value) : 0;
|
|
11
|
+
break;
|
|
12
|
+
case 'bigint':
|
|
13
|
+
payload = Number(value & 0xffffffffn);
|
|
14
|
+
break;
|
|
15
|
+
case 'string':
|
|
16
|
+
payload = value.length;
|
|
17
|
+
break;
|
|
18
|
+
case 'boolean':
|
|
19
|
+
payload = value ? 1 : 0;
|
|
20
|
+
break;
|
|
21
|
+
case 'object':
|
|
22
|
+
payload = value === null ? 0 : 1;
|
|
23
|
+
break;
|
|
24
|
+
case 'function':
|
|
25
|
+
payload = 1;
|
|
26
|
+
break;
|
|
27
|
+
default:
|
|
28
|
+
payload = -1;
|
|
29
|
+
}
|
|
30
|
+
sink[0] ^= payload;
|
|
31
|
+
};
|
|
32
|
+
const runSync = (run, overhead) => {
|
|
33
|
+
return (...args) => {
|
|
34
|
+
const start = hr();
|
|
35
|
+
const result = run(...args);
|
|
36
|
+
consume(result);
|
|
37
|
+
const duration = hr() - start;
|
|
38
|
+
return duration > overhead ? duration - overhead : 0n;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const runAsync = (run) => {
|
|
42
|
+
return async (...args) => {
|
|
43
|
+
const start = hr();
|
|
44
|
+
const result = await run(...args);
|
|
45
|
+
consume(result);
|
|
46
|
+
return hr() - start;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
const isThenable = (value) => {
|
|
50
|
+
return value !== null && (typeof value === 'object' || typeof value === 'function') && typeof value.then === 'function';
|
|
51
|
+
};
|
|
52
|
+
const TARGET_SAMPLE_NS = 1000000n; // aim for ~1ms per measured sample
|
|
53
|
+
const MAX_BATCH = 1_048_576;
|
|
54
|
+
const PROGRESS_STRIDE = 16;
|
|
55
|
+
const GC_STRIDE = 32;
|
|
56
|
+
const OUTLIER_MULTIPLIER = 4;
|
|
57
|
+
const OUTLIER_IQR_MULTIPLIER = 3;
|
|
58
|
+
const OUTLIER_WINDOW = 64;
|
|
59
|
+
const OUTLIER_ABS_THRESHOLD = 10_000_000;
|
|
60
|
+
const BASELINE_SAMPLES = 16;
|
|
61
|
+
const OUTLIER_SCRATCH = new Float64Array(OUTLIER_WINDOW);
|
|
62
|
+
const measureTimerOverhead = () => {
|
|
63
|
+
let total = 0n;
|
|
64
|
+
for (let i = 0; i < BASELINE_SAMPLES; i++) {
|
|
65
|
+
const start = hr();
|
|
66
|
+
consume(0);
|
|
67
|
+
total += hr() - start;
|
|
68
|
+
}
|
|
69
|
+
return total / BigInt(BASELINE_SAMPLES);
|
|
70
|
+
};
|
|
71
|
+
const collectSample = async ({ batchSize, run, runRaw, runIsAsync, pre, preIsAsync, post, postIsAsync, context, data, nextNonce, }) => {
|
|
72
|
+
const canBatchTime = !runIsAsync && !pre && !post;
|
|
73
|
+
if (canBatchTime) {
|
|
74
|
+
const batchStart = hr();
|
|
75
|
+
if (nextNonce) {
|
|
76
|
+
for (let b = 0; b < batchSize; b++) {
|
|
77
|
+
consume(runRaw(context, data, nextNonce()));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
for (let b = 0; b < batchSize; b++) {
|
|
82
|
+
consume(runRaw(context, data));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return ((hr() - batchStart) * DURATION_SCALE) / BigInt(batchSize);
|
|
86
|
+
}
|
|
87
|
+
let sampleDuration = 0n;
|
|
88
|
+
for (let b = 0; b < batchSize; b++) {
|
|
89
|
+
if (pre) {
|
|
90
|
+
if (preIsAsync) {
|
|
91
|
+
await pre(context, data);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
pre(context, data);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (runIsAsync) {
|
|
98
|
+
const runAsyncFn = run;
|
|
99
|
+
const duration = nextNonce ? await runAsyncFn(context, data, nextNonce()) : await runAsyncFn(context, data);
|
|
100
|
+
sampleDuration += duration;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
const runSyncFn = run;
|
|
104
|
+
const duration = nextNonce ? runSyncFn(context, data, nextNonce()) : runSyncFn(context, data);
|
|
105
|
+
sampleDuration += duration;
|
|
106
|
+
}
|
|
107
|
+
if (post) {
|
|
108
|
+
if (postIsAsync) {
|
|
109
|
+
await post(context, data);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
post(context, data);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return (sampleDuration * DURATION_SCALE) / BigInt(batchSize);
|
|
117
|
+
};
|
|
118
|
+
const tuneParameters = async ({ initialBatch, run, runRaw, runIsAsync, pre, preIsAsync, post, postIsAsync, context, data, minCycles, relThreshold, maxCycles, nextNonce, }) => {
|
|
119
|
+
let batchSize = initialBatch;
|
|
120
|
+
let bestCv = Number.POSITIVE_INFINITY;
|
|
121
|
+
let bestBatch = batchSize;
|
|
122
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
123
|
+
const samples = [];
|
|
124
|
+
const sampleCount = Math.min(8, maxCycles);
|
|
125
|
+
for (let s = 0; s < sampleCount; s++) {
|
|
126
|
+
const duration = await collectSample({
|
|
127
|
+
batchSize,
|
|
128
|
+
run,
|
|
129
|
+
runRaw,
|
|
130
|
+
runIsAsync,
|
|
131
|
+
pre,
|
|
132
|
+
preIsAsync,
|
|
133
|
+
post,
|
|
134
|
+
postIsAsync,
|
|
135
|
+
context,
|
|
136
|
+
data,
|
|
137
|
+
nextNonce,
|
|
138
|
+
});
|
|
139
|
+
samples.push(Number(duration));
|
|
140
|
+
}
|
|
141
|
+
const mean = samples.reduce((acc, v) => acc + v, 0) / samples.length;
|
|
142
|
+
const variance = samples.reduce((acc, v) => acc + (v - mean) * (v - mean), 0) / Math.max(1, samples.length - 1);
|
|
143
|
+
const stddev = Math.sqrt(variance);
|
|
144
|
+
const cv = mean === 0 ? Number.POSITIVE_INFINITY : stddev / mean;
|
|
145
|
+
if (cv < bestCv) {
|
|
146
|
+
bestCv = cv;
|
|
147
|
+
bestBatch = batchSize;
|
|
148
|
+
}
|
|
149
|
+
if (cv <= relThreshold || batchSize >= MAX_BATCH) {
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
batchSize = Math.min(MAX_BATCH, batchSize * 2);
|
|
153
|
+
}
|
|
154
|
+
const tunedRel = bestCv < relThreshold ? Math.max(bestCv * 1.5, relThreshold * 0.5) : relThreshold;
|
|
155
|
+
const tunedMin = Math.min(maxCycles, Math.max(minCycles, Math.ceil(minCycles * Math.max(1, bestCv / (relThreshold || 1e-6)))));
|
|
156
|
+
return { batchSize: bestBatch, relThreshold: tunedRel, minCycles: tunedMin };
|
|
157
|
+
};
|
|
158
|
+
const createGCTracker = () => {
|
|
159
|
+
if (process.env.OVERTAKE_GC_OBSERVER !== '1') {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
if (typeof PerformanceObserver === 'undefined') {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
const events = [];
|
|
166
|
+
const observer = new PerformanceObserver((list) => {
|
|
167
|
+
for (const entry of list.getEntries()) {
|
|
168
|
+
events.push({ start: entry.startTime, end: entry.startTime + entry.duration });
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
try {
|
|
172
|
+
observer.observe({ entryTypes: ['gc'] });
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
const overlaps = (start, end) => {
|
|
178
|
+
let noisy = false;
|
|
179
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
180
|
+
const event = events[i];
|
|
181
|
+
if (event.end < start - 5_000) {
|
|
182
|
+
events.splice(i, 1);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (event.start <= end && event.end >= start) {
|
|
186
|
+
noisy = true;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return noisy;
|
|
190
|
+
};
|
|
191
|
+
const dispose = () => observer.disconnect();
|
|
192
|
+
return { overlaps, dispose };
|
|
193
|
+
};
|
|
194
|
+
const pushWindow = (arr, value, cap) => {
|
|
195
|
+
if (arr.length === cap) {
|
|
196
|
+
arr.shift();
|
|
197
|
+
}
|
|
198
|
+
arr.push(value);
|
|
199
|
+
};
|
|
200
|
+
const medianAndIqr = (arr) => {
|
|
201
|
+
if (arr.length === 0)
|
|
202
|
+
return { median: 0, iqr: 0 };
|
|
203
|
+
for (let i = 0; i < arr.length; i++) {
|
|
204
|
+
OUTLIER_SCRATCH[i] = arr[i];
|
|
205
|
+
}
|
|
206
|
+
const view = OUTLIER_SCRATCH.subarray(0, arr.length);
|
|
207
|
+
view.sort();
|
|
208
|
+
const mid = Math.floor(view.length / 2);
|
|
209
|
+
const median = view.length % 2 === 0 ? (view[mid - 1] + view[mid]) / 2 : view[mid];
|
|
210
|
+
const q1Idx = Math.floor(view.length * 0.25);
|
|
211
|
+
const q3Idx = Math.floor(view.length * 0.75);
|
|
212
|
+
const q1 = view[q1Idx];
|
|
213
|
+
const q3 = view[q3Idx];
|
|
214
|
+
return { median, iqr: q3 - q1 };
|
|
215
|
+
};
|
|
216
|
+
const windowCv = (arr) => {
|
|
217
|
+
if (arr.length < 2)
|
|
218
|
+
return Number.POSITIVE_INFINITY;
|
|
219
|
+
const mean = arr.reduce((a, v) => a + v, 0) / arr.length;
|
|
220
|
+
const variance = arr.reduce((a, v) => a + (v - mean) * (v - mean), 0) / (arr.length - 1);
|
|
221
|
+
const stddev = Math.sqrt(variance);
|
|
222
|
+
return mean === 0 ? Number.POSITIVE_INFINITY : stddev / mean;
|
|
223
|
+
};
|
|
224
|
+
export const benchmark = async ({ setup, teardown, pre, run: runRaw, post, data, warmupCycles, minCycles, absThreshold, relThreshold, gcObserver = false, durationsSAB, controlSAB, }) => {
|
|
225
|
+
const durations = new BigUint64Array(durationsSAB);
|
|
226
|
+
const control = new Int32Array(controlSAB);
|
|
227
|
+
control[Control.INDEX] = 0;
|
|
228
|
+
control[Control.PROGRESS] = 0;
|
|
229
|
+
control[Control.COMPLETE] = 255;
|
|
230
|
+
control[Control.HEAP_USED] = 0;
|
|
231
|
+
const context = (await setup?.());
|
|
232
|
+
const heapBefore = process.memoryUsage().heapUsed;
|
|
233
|
+
const input = data;
|
|
234
|
+
const maxCycles = durations.length;
|
|
235
|
+
const gcWatcher = gcObserver ? new GCWatcher() : null;
|
|
236
|
+
const gcTracker = gcObserver ? createGCTracker() : null;
|
|
237
|
+
try {
|
|
238
|
+
// classify sync/async and capture initial duration
|
|
239
|
+
let preIsAsync = false;
|
|
240
|
+
if (pre) {
|
|
241
|
+
const preResult = pre(context, input);
|
|
242
|
+
preIsAsync = isThenable(preResult);
|
|
243
|
+
if (preIsAsync) {
|
|
244
|
+
await preResult;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const probeStart = hr();
|
|
248
|
+
const probeResult = runRaw(context, input);
|
|
249
|
+
const runIsAsync = isThenable(probeResult);
|
|
250
|
+
if (runIsAsync) {
|
|
251
|
+
const resolved = await probeResult;
|
|
252
|
+
consume(resolved);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
consume(probeResult);
|
|
256
|
+
}
|
|
257
|
+
const durationProbeRaw = hr() - probeStart;
|
|
258
|
+
let postIsAsync = false;
|
|
259
|
+
if (post) {
|
|
260
|
+
const postResult = post(context, input);
|
|
261
|
+
postIsAsync = isThenable(postResult);
|
|
262
|
+
if (postIsAsync) {
|
|
263
|
+
await postResult;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const timerOverhead = runIsAsync ? 0n : measureTimerOverhead();
|
|
267
|
+
let durationProbe = runIsAsync ? durationProbeRaw : durationProbeRaw > timerOverhead ? durationProbeRaw - timerOverhead : 0n;
|
|
268
|
+
const shouldPerturbInput = process.env.OVERTAKE_PERTURB_INPUT === '1';
|
|
269
|
+
let nonce = 0;
|
|
270
|
+
const nextNonce = shouldPerturbInput
|
|
271
|
+
? () => {
|
|
272
|
+
nonce = (nonce + 1) | 0;
|
|
273
|
+
return nonce;
|
|
274
|
+
}
|
|
275
|
+
: null;
|
|
276
|
+
if (!runIsAsync && !pre && !post) {
|
|
277
|
+
const PROBE_TIME_LIMIT_NS = 1000000000n;
|
|
278
|
+
const INITIAL_PROBE_SIZE = 10;
|
|
279
|
+
const MAX_PROBE_SIZE = 10_000;
|
|
280
|
+
const initialStart = hr();
|
|
281
|
+
if (nextNonce) {
|
|
282
|
+
for (let i = 0; i < INITIAL_PROBE_SIZE; i++) {
|
|
283
|
+
consume(runRaw(context, input, nextNonce()));
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
for (let i = 0; i < INITIAL_PROBE_SIZE; i++) {
|
|
288
|
+
consume(runRaw(context, input));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const initialDuration = hr() - initialStart;
|
|
292
|
+
const estimatedPerOp = initialDuration / BigInt(INITIAL_PROBE_SIZE);
|
|
293
|
+
const remainingBudget = PROBE_TIME_LIMIT_NS - initialDuration;
|
|
294
|
+
const additionalIterations = estimatedPerOp > 0n ? Number(remainingBudget / estimatedPerOp) : MAX_PROBE_SIZE - INITIAL_PROBE_SIZE;
|
|
295
|
+
const cappedAdditional = Math.min(Math.max(0, additionalIterations), MAX_PROBE_SIZE - INITIAL_PROBE_SIZE);
|
|
296
|
+
let totalIterations = INITIAL_PROBE_SIZE;
|
|
297
|
+
if (cappedAdditional > 0) {
|
|
298
|
+
if (nextNonce) {
|
|
299
|
+
for (let i = 0; i < cappedAdditional; i++) {
|
|
300
|
+
consume(runRaw(context, input, nextNonce()));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
for (let i = 0; i < cappedAdditional; i++) {
|
|
305
|
+
consume(runRaw(context, input));
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
totalIterations += cappedAdditional;
|
|
309
|
+
}
|
|
310
|
+
durationProbe = (hr() - initialStart) / BigInt(totalIterations);
|
|
311
|
+
}
|
|
312
|
+
const runTimedSync = runIsAsync ? null : runSync(runRaw, timerOverhead);
|
|
313
|
+
const runTimedAsync = runIsAsync ? runAsync(runRaw) : null;
|
|
314
|
+
const run = runIsAsync ? runTimedAsync : runTimedSync;
|
|
315
|
+
const runOnceSync = runIsAsync ? null : nextNonce ? (ctx, dataValue) => runTimedSync(ctx, dataValue, nextNonce()) : runTimedSync;
|
|
316
|
+
const runOnceAsync = runIsAsync ? (nextNonce ? (ctx, dataValue) => runTimedAsync(ctx, dataValue, nextNonce()) : runTimedAsync) : null;
|
|
317
|
+
const preSync = preIsAsync ? null : pre;
|
|
318
|
+
const preAsync = preIsAsync ? pre : null;
|
|
319
|
+
const postSync = postIsAsync ? null : post;
|
|
320
|
+
const postAsync = postIsAsync ? post : null;
|
|
321
|
+
// choose batch size to amortize timer overhead
|
|
322
|
+
const durationPerRun = durationProbe === 0n ? 1n : durationProbe;
|
|
323
|
+
const suggestedBatch = Number(TARGET_SAMPLE_NS / durationPerRun);
|
|
324
|
+
const minBatchForFastOps = durationProbe < 100n ? 100_000 : 1;
|
|
325
|
+
const initialBatchSize = Math.min(MAX_BATCH, Math.max(minBatchForFastOps, suggestedBatch));
|
|
326
|
+
// auto-tune based on warmup samples
|
|
327
|
+
const tuned = await tuneParameters({
|
|
328
|
+
initialBatch: initialBatchSize,
|
|
329
|
+
run,
|
|
330
|
+
runRaw,
|
|
331
|
+
runIsAsync,
|
|
332
|
+
pre,
|
|
333
|
+
preIsAsync,
|
|
334
|
+
post,
|
|
335
|
+
postIsAsync,
|
|
336
|
+
context,
|
|
337
|
+
data: input,
|
|
338
|
+
minCycles,
|
|
339
|
+
relThreshold,
|
|
340
|
+
maxCycles,
|
|
341
|
+
nextNonce,
|
|
342
|
+
});
|
|
343
|
+
let batchSize = tuned.batchSize;
|
|
344
|
+
minCycles = tuned.minCycles;
|
|
345
|
+
relThreshold = tuned.relThreshold;
|
|
346
|
+
// warmup: run until requested cycles, adapt if unstable
|
|
347
|
+
const warmupStart = performance.now();
|
|
348
|
+
let warmupRemaining = warmupCycles;
|
|
349
|
+
const warmupWindow = [];
|
|
350
|
+
const warmupCap = Math.max(warmupCycles, Math.min(maxCycles, warmupCycles * 4 || 1000));
|
|
351
|
+
const canBatchTime = !runIsAsync && !preSync && !preAsync && !postSync && !postAsync;
|
|
352
|
+
const runWarmup = async () => {
|
|
353
|
+
if (canBatchTime) {
|
|
354
|
+
const batchStart = hr();
|
|
355
|
+
if (nextNonce) {
|
|
356
|
+
for (let b = 0; b < batchSize; b++) {
|
|
357
|
+
consume(runRaw(context, input, nextNonce()));
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
for (let b = 0; b < batchSize; b++) {
|
|
362
|
+
consume(runRaw(context, input));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return ((hr() - batchStart) * DURATION_SCALE) / BigInt(batchSize);
|
|
366
|
+
}
|
|
367
|
+
if (preSync) {
|
|
368
|
+
preSync(context, input);
|
|
369
|
+
}
|
|
370
|
+
else if (preAsync) {
|
|
371
|
+
await preAsync(context, input);
|
|
372
|
+
}
|
|
373
|
+
const duration = runIsAsync ? await runOnceAsync(context, input) : runOnceSync(context, input);
|
|
374
|
+
if (postSync) {
|
|
375
|
+
postSync(context, input);
|
|
376
|
+
}
|
|
377
|
+
else if (postAsync) {
|
|
378
|
+
await postAsync(context, input);
|
|
379
|
+
}
|
|
380
|
+
return duration * DURATION_SCALE;
|
|
381
|
+
};
|
|
382
|
+
while (performance.now() - warmupStart < 1_000 && warmupRemaining > 0) {
|
|
383
|
+
const duration = await runWarmup();
|
|
384
|
+
pushWindow(warmupWindow, Number(duration), warmupCap);
|
|
385
|
+
warmupRemaining--;
|
|
386
|
+
}
|
|
387
|
+
let warmupDone = 0;
|
|
388
|
+
while (warmupDone < warmupRemaining) {
|
|
389
|
+
const duration = await runWarmup();
|
|
390
|
+
pushWindow(warmupWindow, Number(duration), warmupCap);
|
|
391
|
+
warmupDone++;
|
|
392
|
+
if (global.gc && warmupDone % GC_STRIDE === 0) {
|
|
393
|
+
global.gc();
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
while (warmupWindow.length >= 8 && warmupWindow.length < warmupCap) {
|
|
397
|
+
const cv = windowCv(warmupWindow);
|
|
398
|
+
if (cv <= relThreshold * 2) {
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
const duration = await runWarmup();
|
|
402
|
+
pushWindow(warmupWindow, Number(duration), warmupCap);
|
|
403
|
+
}
|
|
404
|
+
let i = 0;
|
|
405
|
+
const WELFORD_SCALE = 1000000n;
|
|
406
|
+
let meanS = 0n;
|
|
407
|
+
let m2S = 0n;
|
|
408
|
+
const outlierWindow = [];
|
|
409
|
+
let skipped = 0;
|
|
410
|
+
const maxSkipped = maxCycles * 10;
|
|
411
|
+
let disableFiltering = false;
|
|
412
|
+
const absThScaled = BigInt(Math.round(absThreshold)) * WELFORD_SCALE;
|
|
413
|
+
const absThSq = absThScaled * absThScaled;
|
|
414
|
+
const REL_PRECISION = 1000000n;
|
|
415
|
+
const relThBigint = BigInt(Math.round(relThreshold * Number(REL_PRECISION)));
|
|
416
|
+
const relThSq = relThBigint * relThBigint;
|
|
417
|
+
const relPrecSq = REL_PRECISION * REL_PRECISION;
|
|
418
|
+
const Z95_SQ_NUM = 38416n;
|
|
419
|
+
const Z95_SQ_DENOM = 10000n;
|
|
420
|
+
while (true) {
|
|
421
|
+
if (i >= maxCycles)
|
|
422
|
+
break;
|
|
423
|
+
if (!disableFiltering && skipped >= maxSkipped) {
|
|
424
|
+
console.error(`Warning: ${skipped} samples skipped due to noise/outlier detection. ` + `Disabling filtering for remaining samples. Results may have higher variance.`);
|
|
425
|
+
disableFiltering = true;
|
|
426
|
+
}
|
|
427
|
+
if (global.gc && i > 0 && i % GC_STRIDE === 0) {
|
|
428
|
+
global.gc();
|
|
429
|
+
}
|
|
430
|
+
const gcMarker = gcWatcher?.start();
|
|
431
|
+
const sampleStart = performance.now();
|
|
432
|
+
let sampleDuration = 0n;
|
|
433
|
+
if (canBatchTime) {
|
|
434
|
+
const batchStart = hr();
|
|
435
|
+
if (nextNonce) {
|
|
436
|
+
for (let b = 0; b < batchSize; b++) {
|
|
437
|
+
consume(runRaw(context, input, nextNonce()));
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
for (let b = 0; b < batchSize; b++) {
|
|
442
|
+
consume(runRaw(context, input));
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
const batchDuration = hr() - batchStart;
|
|
446
|
+
sampleDuration = (batchDuration * DURATION_SCALE) / BigInt(batchSize);
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
for (let b = 0; b < batchSize; b++) {
|
|
450
|
+
if (preSync) {
|
|
451
|
+
preSync(context, input);
|
|
452
|
+
}
|
|
453
|
+
else if (preAsync) {
|
|
454
|
+
await preAsync(context, input);
|
|
455
|
+
}
|
|
456
|
+
const duration = runIsAsync ? await runOnceAsync(context, input) : runOnceSync(context, input);
|
|
457
|
+
sampleDuration += duration;
|
|
458
|
+
if (postSync) {
|
|
459
|
+
postSync(context, input);
|
|
460
|
+
}
|
|
461
|
+
else if (postAsync) {
|
|
462
|
+
await postAsync(context, input);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
sampleDuration = (sampleDuration * DURATION_SCALE) / BigInt(batchSize);
|
|
466
|
+
}
|
|
467
|
+
const sampleEnd = performance.now();
|
|
468
|
+
if (!disableFiltering) {
|
|
469
|
+
const gcNoise = (gcMarker ? gcWatcher.seen(gcMarker) : false) || (gcTracker?.overlaps(sampleStart, sampleEnd) ?? false);
|
|
470
|
+
if (gcNoise) {
|
|
471
|
+
skipped++;
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
const durationNumber = Number(sampleDuration);
|
|
476
|
+
if (!disableFiltering) {
|
|
477
|
+
const { median, iqr } = medianAndIqr(outlierWindow);
|
|
478
|
+
const maxAllowed = median + OUTLIER_IQR_MULTIPLIER * iqr || Number.POSITIVE_INFINITY;
|
|
479
|
+
if (outlierWindow.length >= 8 && durationNumber > maxAllowed && durationNumber - median > OUTLIER_ABS_THRESHOLD) {
|
|
480
|
+
skipped++;
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
const meanNumber = Number(meanS / WELFORD_SCALE);
|
|
484
|
+
if (i >= 8 && meanNumber > 0 && durationNumber > OUTLIER_MULTIPLIER * meanNumber && durationNumber - meanNumber > OUTLIER_ABS_THRESHOLD) {
|
|
485
|
+
skipped++;
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
pushWindow(outlierWindow, durationNumber, OUTLIER_WINDOW);
|
|
490
|
+
durations[i++] = sampleDuration;
|
|
491
|
+
const deltaS = sampleDuration * WELFORD_SCALE - meanS;
|
|
492
|
+
meanS += deltaS / BigInt(i);
|
|
493
|
+
m2S += deltaS * (sampleDuration * WELFORD_SCALE - meanS);
|
|
494
|
+
const progress = (i / maxCycles) * COMPLETE_VALUE;
|
|
495
|
+
if (i % PROGRESS_STRIDE === 0) {
|
|
496
|
+
control[Control.PROGRESS] = progress;
|
|
497
|
+
}
|
|
498
|
+
if (i >= minCycles) {
|
|
499
|
+
if (m2S <= absThSq * BigInt(i - 1))
|
|
500
|
+
break;
|
|
501
|
+
// RME convergence: Z95 * sem/mean <= relThreshold
|
|
502
|
+
// Z95^2 * m2S / (n*(n-1)*meanS^2) <= relThreshold^2
|
|
503
|
+
const ni = BigInt(i);
|
|
504
|
+
if (meanS !== 0n && Z95_SQ_NUM * m2S * relPrecSq <= relThSq * ni * (ni - 1n) * meanS * meanS * Z95_SQ_DENOM)
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
control[Control.INDEX] = i;
|
|
509
|
+
control[Control.COMPLETE] = 0;
|
|
510
|
+
const heapAfter = process.memoryUsage().heapUsed;
|
|
511
|
+
control[Control.HEAP_USED] = Math.max(0, Math.round((heapAfter - heapBefore) / 1024));
|
|
512
|
+
}
|
|
513
|
+
catch (e) {
|
|
514
|
+
console.error(e && typeof e === 'object' && 'stack' in e ? e.stack : e);
|
|
515
|
+
control[Control.COMPLETE] = 1;
|
|
516
|
+
}
|
|
517
|
+
finally {
|
|
518
|
+
gcTracker?.dispose?.();
|
|
519
|
+
try {
|
|
520
|
+
await teardown?.(context);
|
|
521
|
+
}
|
|
522
|
+
catch (e) {
|
|
523
|
+
control[Control.COMPLETE] = 2;
|
|
524
|
+
console.error(e && typeof e === 'object' && 'stack' in e ? e.stack : e);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return control[Control.COMPLETE];
|
|
528
|
+
};
|
package/build/types.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export const REPORT_TYPES = Array.from({ length: 99 }, (_, idx) => `p${idx + 1}`).concat([
|
|
2
|
+
'ops',
|
|
3
|
+
'mean',
|
|
4
|
+
'min',
|
|
5
|
+
'max',
|
|
6
|
+
'median',
|
|
7
|
+
'mode',
|
|
8
|
+
'variance',
|
|
9
|
+
'sd',
|
|
10
|
+
'sem',
|
|
11
|
+
'moe',
|
|
12
|
+
'rme',
|
|
13
|
+
'mad',
|
|
14
|
+
'iqr',
|
|
15
|
+
'ci_lower',
|
|
16
|
+
'ci_upper',
|
|
17
|
+
]);
|
|
18
|
+
export const Control = {
|
|
19
|
+
INDEX: 0,
|
|
20
|
+
PROGRESS: 1,
|
|
21
|
+
COMPLETE: 2,
|
|
22
|
+
HEAP_USED: 3,
|
|
23
|
+
};
|
|
24
|
+
export const CONTROL_SLOTS = Object.keys(Control).length;
|
|
25
|
+
export const DEFAULT_CYCLES = 10_000;
|
|
26
|
+
export const Z95 = 1.96;
|
|
27
|
+
export const DURATION_SCALE = 1000n;
|
|
28
|
+
export const COMPLETE_VALUE = 100_00;
|
package/build/utils.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export declare const cmp: (a: bigint | number, b: bigint | number) => number;
|
|
|
4
4
|
export declare const max: (a: bigint, b: bigint) => bigint;
|
|
5
5
|
export declare function div(a: bigint, b: bigint, decimals?: number): string;
|
|
6
6
|
export declare function divs(a: bigint, b: bigint, scale: bigint): bigint;
|
|
7
|
+
export declare function normalizeFunction(code: string): string;
|
|
7
8
|
export declare function assertNoClosure(code: string, name: string): void;
|