-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.js
More file actions
214 lines (189 loc) · 5.8 KB
/
index.js
File metadata and controls
214 lines (189 loc) · 5.8 KB
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
const { Worker } = require('node:worker_threads');
const { types } = require('node:util');
const path = require('node:path');
const {
textReport,
chartReport,
htmlReport,
jsonReport,
} = require('./report');
const { getInitialIterations, runBenchmark, runWarmup } = require('./lifecycle');
const { debugBench, timer, createFnString } = require('./clock');
const {
validatePlugins,
V8NeverOptimizePlugin,
V8GetOptimizationStatus,
V8OptimizeOnNextCallPlugin,
RecordMemorySpikePlugin,
} = require('./plugins');
const {
validateFunction,
validateNumber,
validateObject,
validateString,
validateArray,
} = require('./validators');
const getFunctionBody = (string) => string.substring(
string.indexOf("{") + 1,
string.lastIndexOf("}")
);
class Benchmark {
name = 'Benchmark';
fn;
minTime;
maxTime;
plugins;
repeatSuite;
constructor(name, fn, minTime, maxTime, plugins, repeatSuite) {
this.name = name;
this.fn = fn;
this.minTime = minTime;
this.maxTime = maxTime;
this.plugins = plugins;
this.repeatSuite = repeatSuite;
this.hasArg = this.fn.length >= 1;
if (this.fn.length > 1) {
process.emitWarning(`The benchmark "${ this.name }" function should not have more than 1 argument.`);
}
this.isAsync = types.isAsyncFunction(this.fn);
this.fnStr = createFnString(this);
}
serializeBenchmark() {
// Regular functions can't be passed to worker.postMessage
// So we pass the string and deserialize fnStr into a new Function
// on worker
this.fn = getFunctionBody(this.fn.toString());
}
}
const defaultBenchOptions = {
// 0.05s - Arbitrary number used in some benchmark tools
minTime: 0.05,
// 0.5s - Arbitrary number used in some benchmark tools
maxTime: 0.5,
// Number of times the benchmark will be repeated
repeatSuite: 1,
};
function throwIfNoNativesSyntax() {
if (process.execArgv.includes('--allow-natives-syntax') === false) {
throw new Error('bench-node module must be run with --allow-natives-syntax argument');
}
}
class Suite {
#benchmarks;
#reporter;
#plugins;
#useWorkers;
constructor(options = {}) {
this.#benchmarks = [];
validateObject(options, 'options');
if (options?.reporter !== undefined) {
if (options?.reporter !== false && options?.reporter !== null) {
validateFunction(options.reporter, 'reporter');
}
this.#reporter = options.reporter;
} else {
this.#reporter = textReport;
}
this.#useWorkers = options.useWorkers || false;
if (options?.plugins) {
validateArray(options.plugins, 'plugin');
validatePlugins(options.plugins);
}
this.#plugins = options?.plugins || [new V8NeverOptimizePlugin()];
}
add(name, options, fn) {
validateString(name, 'name');
if (typeof options === 'function') {
fn = options;
options = defaultBenchOptions;
} else {
validateObject(options, 'options');
options = {
...defaultBenchOptions,
...options
};
validateNumber(options.minTime, 'options.minTime', timer.resolution * 1e3);
validateNumber(options.maxTime, 'options.maxTime', options.minTime);
validateNumber(options.repeatSuite, 'options.repeatSuite', options.repeatSuite);
}
validateFunction(fn, 'fn');
const benchmark = new Benchmark(
name,
fn,
options.minTime,
options.maxTime,
this.#plugins,
options.repeatSuite,
);
this.#benchmarks.push(benchmark);
return this;
}
async run() {
throwIfNoNativesSyntax();
const results = new Array(this.#benchmarks.length);
// It doesn't make sense to warmup a fresh new instance of Worker.
// TODO: support warmup directly in the Worker.
if (!this.#useWorkers) {
// This is required to avoid variance on first benchmark run
for (let i = 0; i < this.#benchmarks.length; ++i) {
const benchmark = this.#benchmarks[i];
debugBench(`Warmup ${ benchmark.name } with minTime=${ benchmark.minTime }, maxTime=${ benchmark.maxTime }`);
const initialIteration = await getInitialIterations(benchmark);
await runWarmup(benchmark, initialIteration, { minTime: 0.005, maxTime: 0.05 });
}
}
for (let i = 0; i < this.#benchmarks.length; ++i) {
const benchmark = this.#benchmarks[i];
// Warmup is calculated to reduce noise/bias on the results
const initialIterations = await getInitialIterations(benchmark);
debugBench(`Starting ${ benchmark.name } with minTime=${ benchmark.minTime }, maxTime=${ benchmark.maxTime }, repeatSuite=${ benchmark.repeatSuite }`);
let result;
if (this.#useWorkers) {
result = await this.runWorkerBenchmark(benchmark, initialIterations);
} else {
result = await runBenchmark(benchmark, initialIterations, benchmark.repeatSuite);
}
results[i] = result;
}
if (this.#reporter) {
this.#reporter(results);
}
return results;
}
runWorkerBenchmark(benchmark, initialIterations) {
benchmark.serializeBenchmark();
const worker = new Worker(path.join(__dirname, './worker-runner.js'));
worker.postMessage({
benchmark,
initialIterations,
repeatSuite: benchmark.repeatSuite,
});
return new Promise((resolve, reject) => {
worker.on('message', (result) => {
resolve(result);
// TODO: await?
worker.terminate();
});
worker.on('error', (err) => {
reject(err);
worker.terminate();
});
worker.on('exit', (code) => {
if (code !== 0) {
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
});
}
}
module.exports = {
Suite,
V8NeverOptimizePlugin,
V8GetOptimizationStatus,
V8OptimizeOnNextCallPlugin,
RecordMemorySpikePlugin,
chartReport,
textReport,
htmlReport,
jsonReport,
};