-
Notifications
You must be signed in to change notification settings - Fork 22
/
lib.mjs
268 lines (219 loc) · 7.45 KB
/
lib.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
const AsyncFunction = (async () => { }).constructor;
const GeneratorFunction = (function* () { }).constructor;
const AsyncGeneratorFunction = (async function* () { }).constructor;
export async function measure(f, ...args) {
return await {
fn, iter, yield: generator,
[void 0]() { throw new TypeError(`expected iterator, generator or one-shot function`); },
}[kind(f)](f, ...args);
}
export async function generator(gen, opts = {}) {
const ctx = {
get(name) { return opts.args?.[name] },
};
const g = gen(ctx);
const n = await g.next();
if (n.done || 'fn' !== kind(n.value)) throw new TypeError(`expected benchmarkable yield from generator`);
const stats = await fn(n.value, opts);
if (!(await g.next()).done) throw new TypeError(`expected generator to yield once`);
return {
...stats,
kind: 'yield',
};
}
export const print = (() => {
if (globalThis.print) return globalThis.print;
if (globalThis.console?.log) return globalThis.console.log;
return () => { throw new Error('no print function available'); };
})();
export const gc = (() => {
try { return (Bun.gc(true), () => Bun.gc(true)); } catch { }
try { return (globalThis.gc(), () => globalThis.gc()); } catch { }
try { return (globalThis.std.gc(), () => globalThis.std.gc()); } catch { }
try { return (globalThis.$262.gc(), () => globalThis.$262.gc()); } catch { }
if (globalThis.Graal) return () => new Uint8Array(2 ** 29); return () => new Uint8Array(2 ** 30);
})();
export const now = (() => {
try { // bun
Bun.nanoseconds();
return Bun.nanoseconds;
} catch { }
try { // jsc
$.agent.monotonicNow();
return () => 1e6 * $.agent.monotonicNow();
} catch { }
try { // 262 agent
$262.agent.monotonicNow();
return () => 1e6 * $262.agent.monotonicNow();
} catch { }
try { // node/deno/... (v8 inline, anti-deopts)
const now = performance.now.bind(performance);
now(); return () => 1e6 * now();
} catch { return () => 1e6 * Date.now(); }
})();
export function kind(fn) {
if (!(
fn instanceof Function
|| fn instanceof AsyncFunction
|| fn instanceof GeneratorFunction
|| fn instanceof AsyncGeneratorFunction
)) return;
if (
fn instanceof GeneratorFunction
|| fn instanceof AsyncGeneratorFunction
) return 'yield';
if (
0 === fn.length
&& (
fn instanceof Function
|| fn instanceof AsyncFunction
)
) return 'fn';
if (
0 !== fn.length
&& (
fn instanceof Function
|| fn instanceof AsyncFunction
)
) return 'iter';
}
export const k_min_samples = 12;
export const k_batch_unroll = 4;
export const k_max_samples = 1e9;
export const k_warmup_samples = 2;
export const k_batch_samples = 4096;
export const k_samples_threshold = 12;
export const k_batch_threshold = 65536;
export const k_min_cpu_time = 642 * 1e6;
export const k_warmup_threshold = 500_000;
function defaults(opts) {
opts.gc ??= gc;
opts.now ??= now;
opts.min_samples ??= k_min_samples;
opts.max_samples ??= k_max_samples;
opts.min_cpu_time ??= k_min_cpu_time;
opts.batch_unroll ??= k_batch_unroll;
opts.batch_samples ??= k_batch_samples;
opts.warmup_samples ??= k_warmup_samples;
opts.batch_threshold ??= k_batch_threshold;
opts.warmup_threshold ??= k_warmup_threshold;
opts.samples_threshold ??= k_samples_threshold;
}
export async function fn(fn, opts = {}) {
defaults(opts);
let async = false;
let batch = false;
warmup: {
const t0 = now();
const r = fn(); let t1 = now();
if (async = r instanceof Promise) (await r, t1 = now());
if ((t1 - t0) <= opts.warmup_threshold) {
for (let o = 0; o < opts.warmup_samples; o++) {
const t0 = now();
await fn(); const t1 = now();
if (batch = (t1 - t0) <= opts.batch_threshold) break;
}
}
}
const loop = new AsyncFunction('$fn', '$gc', '$now', `
let _ = 0; let t = 0;
let samples = new Array(2 ** 20);
${!opts.gc ? '' : `$gc();`}
for (; _ < ${opts.max_samples}; _++) {
if (_ >= ${opts.min_samples} && t >= ${opts.min_cpu_time}) break;
const t0 = $now();
${!batch ? `${!async ? '' : 'await'} $fn();` : `
for (let o = 0; o < ${(opts.batch_samples / opts.batch_unroll) | 0}; o++) {
${new Array(opts.batch_unroll).fill(`${!async ? '' : 'await'} $fn();`).join(' ')}
}
`}
const t1 = $now();
const diff = t1 - t0;
t += diff;
samples[_] = diff ${!batch ? '' : `/ ${opts.batch_samples}`};
}
samples.length = _;
samples.sort((a, b) => a - b);
if (samples.length > ${opts.samples_threshold}) samples = samples.slice(2, -2);
return {
samples,
min: samples[0],
max: samples[samples.length - 1],
p25: samples[(.25 * (samples.length - 1)) | 0],
p50: samples[(.50 * (samples.length - 1)) | 0],
p75: samples[(.75 * (samples.length - 1)) | 0],
p99: samples[(.99 * (samples.length - 1)) | 0],
p999: samples[(.999 * (samples.length - 1)) | 0],
avg: samples.reduce((a, v) => a + v, 0) / samples.length,
ticks: samples.length ${!batch ? '' : `* ${opts.batch_samples}`},
};
`);
return {
kind: 'fn',
debug: loop.toString(),
...(await loop(fn, opts.gc, opts.now)),
};
}
export async function iter(iter, opts = {}) {
const _ = {};
defaults(opts);
let samples = new Array(2 ** 20);
const _i = { next() { return _.next() } };
const ctx = {
[Symbol.iterator]() { return _i },
[Symbol.asyncIterator]() { return _i },
get(name) { return opts.args?.[name] },
};
const gen = (function* () {
let batch = false;
warmup: {
const t0 = now();
yield void 0; const t1 = now();
if ((t1 - t0) <= opts.warmup_threshold) {
for (let o = 0; o < opts.warmup_samples; o++) {
const t0 = now();
yield void 0; const t1 = now();
if (batch = (t1 - t0) <= opts.batch_threshold) break;
}
}
}
const loop = new GeneratorFunction('$gc', '$now', '$samples', _.debug = `
let _ = 0; let t = 0;
${!opts.gc ? '' : `$gc();`}
for (; _ < ${opts.max_samples}; _++) {
if (_ >= ${opts.min_samples} && t >= ${opts.min_cpu_time}) break;
const t0 = $now();
${!batch ? 'yield void 0;' : `
for (let o = 0; o < ${(opts.batch_samples / opts.batch_unroll) | 0}; o++) {
${new Array(opts.batch_unroll).fill('yield void 0;').join(' ')}
}
`}
const t1 = $now();
const diff = t1 - t0;
t += diff;
$samples[_] = diff ${!batch ? '' : `/ ${opts.batch_samples}`};
}
$samples.length = _;
`)(opts.gc, opts.now, samples);
_.batch = batch;
_.next = loop.next.bind(loop); yield void 0;
})();
await iter((_.next = gen.next.bind(gen), ctx));
if (samples.length < opts.min_samples) throw new TypeError(`expected at least ${opts.min_samples} samples from iterator`);
samples.sort((a, b) => a - b);
if (samples.length > opts.samples_threshold) samples = samples.slice(2, -2);
return {
samples,
kind: 'iter',
debug: _.debug,
min: samples[0],
max: samples[samples.length - 1],
p25: samples[(.25 * (samples.length - 1)) | 0],
p50: samples[(.50 * (samples.length - 1)) | 0],
p75: samples[(.75 * (samples.length - 1)) | 0],
p99: samples[(.99 * (samples.length - 1)) | 0],
p999: samples[(.999 * (samples.length - 1)) | 0],
avg: samples.reduce((a, v) => a + v, 0) / samples.length,
ticks: samples.length * (!_.batch ? 1 : opts.batch_samples),
};
}