overtake 0.0.2 → 0.0.5
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/cli.js +54 -15
- package/index.js +39 -23
- package/package.json +3 -2
package/cli.js
CHANGED
|
@@ -1,23 +1,62 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env -S node --no-warnings
|
|
2
2
|
|
|
3
|
+
import { Command } from 'commander';
|
|
3
4
|
import { promisify } from 'util';
|
|
4
5
|
import Path from 'path';
|
|
5
6
|
import glob from 'glob';
|
|
6
|
-
import { load, benchmark, setup, teardown, measure, perform, run, defaultReporter } from './index.js';
|
|
7
|
+
import { load, createScript, benchmark, setup, teardown, measure, perform, run, defaultReporter } from './index.js';
|
|
8
|
+
import packageJson from './package.json' assert { type: 'json' };
|
|
7
9
|
|
|
8
|
-
const
|
|
9
|
-
const pattern = process.argv[2] || '**/__benchmarks__/**/*.js';
|
|
10
|
+
const commands = new Command();
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
commands.name('overtake').description(packageJson.description).version(packageJson.version, '-v, --version');
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
for (
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
scripts.push(script);
|
|
20
|
-
}
|
|
14
|
+
commands
|
|
15
|
+
.argument('[files...]', 'file paths or path patterns to search benchmark scripts')
|
|
16
|
+
.option('-i, --inline [inline]', 'inline code to benchmark', (value, previous) => previous.concat([value]), [])
|
|
17
|
+
.option('-c, --count [count]', 'perform count for inline code', (v) => parseInt(v))
|
|
18
|
+
.action(async (patterns, { count = 1, inline }) => {
|
|
19
|
+
Object.assign(globalThis, { benchmark, setup, teardown, measure, perform });
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const globAsync = promisify(glob);
|
|
22
|
+
const foundFiles = await Promise.all(patterns.map((pattern) => globAsync(pattern)));
|
|
23
|
+
const files = [
|
|
24
|
+
...new Set(
|
|
25
|
+
[]
|
|
26
|
+
.concat(...foundFiles)
|
|
27
|
+
.map((filename) => Path.resolve(filename))
|
|
28
|
+
.filter(Boolean)
|
|
29
|
+
),
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
const scripts = [];
|
|
33
|
+
if (inline.length) {
|
|
34
|
+
const inlineScript = await createScript('', () => {
|
|
35
|
+
benchmark('', () => {
|
|
36
|
+
inline.forEach((code) => {
|
|
37
|
+
measure(code, `() => () => { ${code} }`);
|
|
38
|
+
});
|
|
39
|
+
perform('', count);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
scripts.push(inlineScript);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const file of files) {
|
|
46
|
+
const filename = Path.resolve(file);
|
|
47
|
+
const script = await load(filename);
|
|
48
|
+
scripts.push(script);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
await run(scripts, defaultReporter);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
commands.on('--help', () => {
|
|
55
|
+
console.log('');
|
|
56
|
+
console.log('Examples:');
|
|
57
|
+
console.log(' $ overtake **/__benchmarks__/*.js');
|
|
58
|
+
console.log(' $ overtake -i "class A{}" -i "function A(){}" -i "const A = () => {}" -c 1000000');
|
|
59
|
+
console.log(' $ overtake -v');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
commands.parse(process.argv);
|
package/index.js
CHANGED
|
@@ -38,13 +38,18 @@ export const benchmark = (title, fn) => {
|
|
|
38
38
|
});
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
-
export const
|
|
41
|
+
export const createScript = async (filename, fn) => {
|
|
42
42
|
const suites = [];
|
|
43
43
|
const script = { filename, suites };
|
|
44
|
-
await overtakeContext.contextualize(script,
|
|
44
|
+
await overtakeContext.contextualize(script, fn);
|
|
45
45
|
|
|
46
46
|
return script;
|
|
47
47
|
};
|
|
48
|
+
|
|
49
|
+
export const load = async (filename) => {
|
|
50
|
+
return createScript(filename, () => import(filename));
|
|
51
|
+
};
|
|
52
|
+
|
|
48
53
|
const map = {
|
|
49
54
|
script: '⭐ Script ',
|
|
50
55
|
suite: '⇶ Suite ',
|
|
@@ -54,7 +59,27 @@ const map = {
|
|
|
54
59
|
|
|
55
60
|
export const defaultReporter = async (type, title, test) => {
|
|
56
61
|
console.group(`${map[type]} ${title}`);
|
|
57
|
-
await test({
|
|
62
|
+
await test({
|
|
63
|
+
test: defaultReporter,
|
|
64
|
+
output: (report) =>
|
|
65
|
+
console.table(
|
|
66
|
+
report.success
|
|
67
|
+
? {
|
|
68
|
+
[formatFloat(report.mode)]: {
|
|
69
|
+
med: formatFloat(report.med),
|
|
70
|
+
p95: formatFloat(report.p95),
|
|
71
|
+
p99: formatFloat(report.p99),
|
|
72
|
+
total: formatFloat(report.sum),
|
|
73
|
+
count: report.count,
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
: {
|
|
77
|
+
error: {
|
|
78
|
+
reason: report.error,
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
),
|
|
82
|
+
});
|
|
58
83
|
console.groupEnd();
|
|
59
84
|
};
|
|
60
85
|
|
|
@@ -80,22 +105,7 @@ export const run = async (scripts, reporter) => {
|
|
|
80
105
|
count: perform.count,
|
|
81
106
|
args: perform.args,
|
|
82
107
|
});
|
|
83
|
-
|
|
84
|
-
measureTest.output({
|
|
85
|
-
[formatFloat(result.mode)]: {
|
|
86
|
-
total: formatFloat(result.total),
|
|
87
|
-
med: formatFloat(result.med),
|
|
88
|
-
p95: formatFloat(result.p95),
|
|
89
|
-
p99: formatFloat(result.p99),
|
|
90
|
-
},
|
|
91
|
-
});
|
|
92
|
-
} else {
|
|
93
|
-
measureTest.output({
|
|
94
|
-
error: {
|
|
95
|
-
reason: result.error,
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
}
|
|
108
|
+
measureTest.output(result);
|
|
99
109
|
});
|
|
100
110
|
}
|
|
101
111
|
});
|
|
@@ -140,8 +150,8 @@ export async function start(input) {
|
|
|
140
150
|
const setup = Function(`return ${setupCode};`)();
|
|
141
151
|
const teardown = Function(`return ${teardownCode};`)();
|
|
142
152
|
const init = Function(`return ${initCode};`)();
|
|
153
|
+
const initArgsSize = init.length;
|
|
143
154
|
const send = WorkerThreads.parentPort ? (data) => WorkerThreads.parentPort.postMessage(data) : (data) => console.log(data);
|
|
144
|
-
|
|
145
155
|
let i = count;
|
|
146
156
|
let done = FALSE_START;
|
|
147
157
|
|
|
@@ -153,11 +163,14 @@ export async function start(input) {
|
|
|
153
163
|
const context = await setup();
|
|
154
164
|
const setupMark = performance.now();
|
|
155
165
|
|
|
156
|
-
const initArgs = [
|
|
157
|
-
if (
|
|
166
|
+
const initArgs = [];
|
|
167
|
+
if (initArgsSize !== 0) {
|
|
168
|
+
initArgs.push(() => done());
|
|
169
|
+
}
|
|
170
|
+
if (initArgsSize > 2) {
|
|
158
171
|
initArgs.unshift(args);
|
|
159
172
|
}
|
|
160
|
-
if (
|
|
173
|
+
if (initArgsSize > 1) {
|
|
161
174
|
initArgs.unshift(context);
|
|
162
175
|
}
|
|
163
176
|
|
|
@@ -188,6 +201,9 @@ export async function start(input) {
|
|
|
188
201
|
|
|
189
202
|
const startTickTime = performance.now();
|
|
190
203
|
action(...args[argIdx], idx);
|
|
204
|
+
if (!initArgsSize) {
|
|
205
|
+
done();
|
|
206
|
+
}
|
|
191
207
|
};
|
|
192
208
|
const cyclesMark = performance.now();
|
|
193
209
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "overtake",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "NodeJS performance benchmark",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"overtake": "cli.js"
|
|
10
10
|
},
|
|
11
11
|
"scripts": {
|
|
12
|
-
"start": "
|
|
12
|
+
"start": "./cli.js",
|
|
13
13
|
"test": "yarn node --experimental-vm-modules $(yarn bin jest) --detectOpenHandles",
|
|
14
14
|
"lint": "eslint .",
|
|
15
15
|
"prepare": "husky install"
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"pretty-quick": "^3.1.3"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
+
"commander": "^9.2.0",
|
|
42
43
|
"conode": "^0.1.0",
|
|
43
44
|
"glob": "^8.0.1"
|
|
44
45
|
}
|