overtake 0.0.2 → 0.0.3

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.
Files changed (3) hide show
  1. package/cli.js +68 -14
  2. package/index.js +18 -6
  3. package/package.json +3 -2
package/cli.js CHANGED
@@ -1,23 +1,77 @@
1
1
  #!/usr/bin/env node
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 globAsync = promisify(glob);
9
- const pattern = process.argv[2] || '**/__benchmarks__/**/*.js';
10
+ const commands = new Command();
10
11
 
11
- Object.assign(globalThis, { benchmark, setup, teardown, measure, perform });
12
+ commands.name('overtake').description(packageJson.description).version(packageJson.version, '-v, --version');
12
13
 
13
- (async () => {
14
- const files = await globAsync(pattern);
15
- const scripts = [];
16
- for (const file of files) {
17
- const filename = Path.resolve(file);
18
- const script = await load(filename);
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 = 100, inline }) => {
19
+ Object.assign(globalThis, { benchmark, setup, teardown, measure, perform });
21
20
 
22
- await run(scripts, defaultReporter);
23
- })().catch((e) => console.error(e));
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);
63
+
64
+ //
65
+
66
+ //
67
+ // (async () => {
68
+ // const files = await globAsync(pattern);
69
+ // const scripts = [];
70
+ // for (const file of files) {
71
+ // const filename = Path.resolve(file);
72
+ // const script = await load(filename);
73
+ // scripts.push(script);
74
+ // }
75
+ //
76
+ // await run(scripts, defaultReporter);
77
+ // })().catch((e) => console.error(e));
package/index.js CHANGED
@@ -38,13 +38,18 @@ export const benchmark = (title, fn) => {
38
38
  });
39
39
  };
40
40
 
41
- export const load = async (filename) => {
41
+ export const createScript = async (filename, fn) => {
42
42
  const suites = [];
43
43
  const script = { filename, suites };
44
- await overtakeContext.contextualize(script, () => import(filename));
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 ',
@@ -87,6 +92,7 @@ export const run = async (scripts, reporter) => {
87
92
  med: formatFloat(result.med),
88
93
  p95: formatFloat(result.p95),
89
94
  p99: formatFloat(result.p99),
95
+ count: result.count,
90
96
  },
91
97
  });
92
98
  } else {
@@ -140,8 +146,8 @@ export async function start(input) {
140
146
  const setup = Function(`return ${setupCode};`)();
141
147
  const teardown = Function(`return ${teardownCode};`)();
142
148
  const init = Function(`return ${initCode};`)();
149
+ const initArgsSize = init.length;
143
150
  const send = WorkerThreads.parentPort ? (data) => WorkerThreads.parentPort.postMessage(data) : (data) => console.log(data);
144
-
145
151
  let i = count;
146
152
  let done = FALSE_START;
147
153
 
@@ -153,11 +159,14 @@ export async function start(input) {
153
159
  const context = await setup();
154
160
  const setupMark = performance.now();
155
161
 
156
- const initArgs = [() => done()];
157
- if (init.length > 2) {
162
+ const initArgs = [];
163
+ if (initArgsSize !== 0) {
164
+ initArgs.push(() => done());
165
+ }
166
+ if (initArgsSize > 2) {
158
167
  initArgs.unshift(args);
159
168
  }
160
- if (init.length > 1) {
169
+ if (initArgsSize > 1) {
161
170
  initArgs.unshift(context);
162
171
  }
163
172
 
@@ -188,6 +197,9 @@ export async function start(input) {
188
197
 
189
198
  const startTickTime = performance.now();
190
199
  action(...args[argIdx], idx);
200
+ if (!initArgsSize) {
201
+ done();
202
+ }
191
203
  };
192
204
  const cyclesMark = performance.now();
193
205
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "overtake",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
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": "node ./cli.js",
12
+ "start": "node --no-warnings ./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
  }