overtake 0.0.1-rc1
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/LICENSE +13 -0
- package/README.md +105 -0
- package/cli.js +28 -0
- package/index.d.ts +15 -0
- package/index.js +223 -0
- package/package.json +48 -0
- package/runner.js +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright (c) 2022-present Ivan Zakharchanka
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Overtake
|
|
2
|
+
|
|
3
|
+
NodeJS performance benchmark
|
|
4
|
+
|
|
5
|
+
[![Build Status][github-image]][github-url]
|
|
6
|
+
[![NPM version][npm-image]][npm-url]
|
|
7
|
+
[![Downloads][downloads-image]][npm-url]
|
|
8
|
+
[![Coverage Status][codecov-image]][codecov-url]
|
|
9
|
+
[![Maintainability][codeclimate-image]][codeclimate-url]
|
|
10
|
+
[![Snyk][snyk-image]][snyk-url]
|
|
11
|
+
|
|
12
|
+
## Table of Contents
|
|
13
|
+
|
|
14
|
+
- [Features](#features)
|
|
15
|
+
- [Installing](#installing)
|
|
16
|
+
- [Examples](#examples)
|
|
17
|
+
- [License](#license)
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
- CLI
|
|
22
|
+
- TypeScript support
|
|
23
|
+
- Running in thread worker
|
|
24
|
+
|
|
25
|
+
## Installing
|
|
26
|
+
|
|
27
|
+
Using yarn:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
$ yarn add -D overtake
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Using npm:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
$ npm install -D overtake
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Examples
|
|
40
|
+
|
|
41
|
+
Create a benchmark in `__benchmarks__` folder
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
benchmark('mongodb vs postgres', () => {
|
|
45
|
+
// initialize a context for benchmark
|
|
46
|
+
setup(async () => {
|
|
47
|
+
const { Client } = await import('pg');
|
|
48
|
+
const postgres = new Client();
|
|
49
|
+
await postgres.connect();
|
|
50
|
+
|
|
51
|
+
const { MongoClient } = await import('mongob');
|
|
52
|
+
const mongo = new MongoClient(uri);
|
|
53
|
+
await mongo.connect();
|
|
54
|
+
|
|
55
|
+
return { postgres, mongo };
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
measure('mongodb inserts', ({ mongo }/* context */, next) => {
|
|
59
|
+
// prepare a collection
|
|
60
|
+
const database = mongo.db('overtake');
|
|
61
|
+
const test = database.collection('test');
|
|
62
|
+
|
|
63
|
+
return (data) => test.insertOne(data).then(next);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
measure('postgres inserts', ({ postgres }/* context */, next) => {
|
|
67
|
+
// prepare a query
|
|
68
|
+
const query = 'INSERT INTO overtake(value) VALUES($1) RETURNING *';
|
|
69
|
+
|
|
70
|
+
return (data) => postgres.query(query, [data.value]).then(next);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
teardown(({ mongo, postgres }) => {
|
|
74
|
+
await postgres.end()
|
|
75
|
+
await mongo.end()
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
perform('simple test', 100000, [
|
|
79
|
+
{ value: 'test' },
|
|
80
|
+
]);
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
yarn overtake
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Please take a look at [benchmarks](__benchmarks__) to see more examples
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
License [Apache-2.0](http://www.apache.org/licenses/LICENSE-2.0)
|
|
93
|
+
Copyright (c) 2021-present Ivan Zakharchanka
|
|
94
|
+
|
|
95
|
+
[npm-url]: https://www.npmjs.com/package/overtake
|
|
96
|
+
[downloads-image]: https://img.shields.io/npm/dw/overtake.svg?maxAge=43200
|
|
97
|
+
[npm-image]: https://img.shields.io/npm/v/overtake.svg?maxAge=43200
|
|
98
|
+
[github-url]: https://github.com/3axap4eHko/overtake/actions/workflows/cicd.yml
|
|
99
|
+
[github-image]: https://github.com/3axap4eHko/overtake/actions/workflows/cicd.yml/badge.svg
|
|
100
|
+
[codecov-url]: https://codecov.io/gh/3axap4eHko/overtake
|
|
101
|
+
[codecov-image]: https://codecov.io/gh/3axap4eHko/overtake/branch/master/graph/badge.svg?token=JZ8QCGH6PI
|
|
102
|
+
[codeclimate-url]: https://codeclimate.com/github/3axap4eHko/overtake/maintainability
|
|
103
|
+
[codeclimate-image]: https://api.codeclimate.com/v1/badges/0ba20f27f6db2b0fec8c/maintainability
|
|
104
|
+
[snyk-url]: https://snyk.io/test/npm/overtake/latest
|
|
105
|
+
[snyk-image]: https://img.shields.io/snyk/vulnerabilities/github/3axap4eHko/overtake.svg?maxAge=43200
|
package/cli.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import Path from 'path';
|
|
3
|
+
import glob from 'glob';
|
|
4
|
+
import { benchmark, setup, teardown, measure, perform, suites, runner } from './index.js';
|
|
5
|
+
|
|
6
|
+
const pattern = process.argv[2] || '**/__benchmarks__/**/*.js';
|
|
7
|
+
|
|
8
|
+
Object.assign(globalThis, {
|
|
9
|
+
benchmark,
|
|
10
|
+
setup,
|
|
11
|
+
teardown,
|
|
12
|
+
measure,
|
|
13
|
+
perform,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
glob(pattern, async (err, files) => {
|
|
17
|
+
try {
|
|
18
|
+
for (const file of files) {
|
|
19
|
+
const filepath = Path.resolve(file);
|
|
20
|
+
await import(filepath);
|
|
21
|
+
}
|
|
22
|
+
} catch (e) {
|
|
23
|
+
console.error(e.stack);
|
|
24
|
+
}
|
|
25
|
+
for (const suite of suites) {
|
|
26
|
+
await runner(suite);
|
|
27
|
+
}
|
|
28
|
+
});
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
declare function benchmark(title: string, init: () => void): void;
|
|
3
|
+
|
|
4
|
+
declare function setup<C>(init: () => C): any;
|
|
5
|
+
|
|
6
|
+
declare function teardown<C>(teardown: (context: C) => void): void;
|
|
7
|
+
|
|
8
|
+
declare function measure(title: string, init: (next: () => void) => void): void;
|
|
9
|
+
declare function measure<C>(title: string, init: (context: C, next: () => void) => void): void;
|
|
10
|
+
declare function measure<C, A>(title: string, init: (context: C, args: A, next: () => void) => void): void;
|
|
11
|
+
|
|
12
|
+
declare function perform<A>(title: string, counter: number, args: A): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { benchmark, setup, teardown, measure, perform };
|
package/index.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import WorkerThreads from 'worker_threads';
|
|
2
|
+
|
|
3
|
+
export const suites = [];
|
|
4
|
+
|
|
5
|
+
const NOOP = () => {};
|
|
6
|
+
|
|
7
|
+
const SPINNER = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏', '⠿'];
|
|
8
|
+
const SPINNER_INTERVAL = 80;
|
|
9
|
+
|
|
10
|
+
const ACCURACY = 6;
|
|
11
|
+
|
|
12
|
+
const renderSpinner = (index) => {
|
|
13
|
+
process.stdout.moveCursor(0, -1);
|
|
14
|
+
process.stdout.write(SPINNER[index]);
|
|
15
|
+
process.stdout.moveCursor(-3, 1);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function benchmark(title, init) {
|
|
19
|
+
const suite = {
|
|
20
|
+
title,
|
|
21
|
+
current: true,
|
|
22
|
+
measures: [],
|
|
23
|
+
performs: [],
|
|
24
|
+
setup: NOOP,
|
|
25
|
+
teardown: NOOP,
|
|
26
|
+
};
|
|
27
|
+
suites.push(suite);
|
|
28
|
+
init();
|
|
29
|
+
suite.current = false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function getSuite() {
|
|
33
|
+
const suite = suites[suites.length - 1];
|
|
34
|
+
if (!suite.current) {
|
|
35
|
+
throw new Error('should be inside benchmark');
|
|
36
|
+
}
|
|
37
|
+
return suite;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function setup(init) {
|
|
41
|
+
const suite = getSuite();
|
|
42
|
+
suite.setup = init;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function teardown(init) {
|
|
46
|
+
const suite = getSuite();
|
|
47
|
+
suite.teardown = init;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function measure(title, init) {
|
|
51
|
+
const suite = getSuite();
|
|
52
|
+
suite.measures.push({ title, init });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function perform(title, count, args) {
|
|
56
|
+
const suite = getSuite();
|
|
57
|
+
suite.performs.push({ title, count, args });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function formatFloat(value, digits = ACCURACY) {
|
|
61
|
+
return parseFloat(value.toFixed(digits));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function runWorker({ args, count, ...options }) {
|
|
65
|
+
const setupCode = options.setup.toString();
|
|
66
|
+
const teardownCode = options.teardown.toString();
|
|
67
|
+
const initCode = options.init.toString();
|
|
68
|
+
const params = JSON.stringify({
|
|
69
|
+
setupCode,
|
|
70
|
+
teardownCode,
|
|
71
|
+
initCode,
|
|
72
|
+
count,
|
|
73
|
+
args,
|
|
74
|
+
});
|
|
75
|
+
let i = 0;
|
|
76
|
+
const spinnerSize = SPINNER.length - 1;
|
|
77
|
+
|
|
78
|
+
const timerId = setInterval(() => renderSpinner(i++ % spinnerSize), SPINNER_INTERVAL);
|
|
79
|
+
|
|
80
|
+
const worker = new WorkerThreads.Worker(new URL('runner.js', import.meta.url), { argv: [params] });
|
|
81
|
+
return new Promise((resolve) => {
|
|
82
|
+
worker.on('message', resolve);
|
|
83
|
+
worker.on('error', (error) => resolve({ success: false, error: error.message }));
|
|
84
|
+
}).finally((result) => {
|
|
85
|
+
clearInterval(timerId);
|
|
86
|
+
renderSpinner(spinnerSize);
|
|
87
|
+
|
|
88
|
+
return result;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function runner(suite) {
|
|
93
|
+
console.group(`\nStart ${suite.title} benchmark`);
|
|
94
|
+
|
|
95
|
+
for (let measureIdx = 0; measureIdx < suite.measures.length; measureIdx++) {
|
|
96
|
+
const currentMeasure = suite.measures[measureIdx];
|
|
97
|
+
const reports = {};
|
|
98
|
+
console.group(`\n Measuring performance of ${currentMeasure.title}`);
|
|
99
|
+
for (let performIdx = 0; performIdx < suite.performs.length; performIdx++) {
|
|
100
|
+
const currentPerform = suite.performs[performIdx];
|
|
101
|
+
const report = await runWorker({
|
|
102
|
+
setup: suite.setup,
|
|
103
|
+
teardown: suite.teardown,
|
|
104
|
+
init: currentMeasure.init,
|
|
105
|
+
count: currentPerform.count,
|
|
106
|
+
args: currentPerform.args,
|
|
107
|
+
});
|
|
108
|
+
reports[currentPerform.title] = { title: perform.title, report };
|
|
109
|
+
if (report.success) {
|
|
110
|
+
reports[currentPerform.title] = {
|
|
111
|
+
Count: currentPerform.count,
|
|
112
|
+
'Setup, ms': formatFloat(report.setup),
|
|
113
|
+
'Work, ms': formatFloat(report.work),
|
|
114
|
+
'Avg, ms': formatFloat(report.avg),
|
|
115
|
+
'Mode, ms': report.mode,
|
|
116
|
+
};
|
|
117
|
+
} else {
|
|
118
|
+
reports[`${currentPerform.title} ${currentMeasure.title}`] = {
|
|
119
|
+
Count: currentPerform.count,
|
|
120
|
+
'Setup, ms': '?',
|
|
121
|
+
'Work, ms': '?',
|
|
122
|
+
'Avg, ms': '?',
|
|
123
|
+
'Mode, ms': '?',
|
|
124
|
+
Error: report.error,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
console.groupEnd();
|
|
129
|
+
console.table(reports);
|
|
130
|
+
}
|
|
131
|
+
console.groupEnd();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const FALSE_START = () => {
|
|
135
|
+
throw new Error('False start');
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export async function start(input) {
|
|
139
|
+
const { setupCode, teardownCode, initCode, count, args = [[]] } = JSON.parse(input);
|
|
140
|
+
const setup = Function(`return ${setupCode};`)();
|
|
141
|
+
const teardown = Function(`return ${teardownCode};`)();
|
|
142
|
+
const init = Function(`return ${initCode};`)();
|
|
143
|
+
|
|
144
|
+
let i = count;
|
|
145
|
+
let done = FALSE_START;
|
|
146
|
+
|
|
147
|
+
const timings = [];
|
|
148
|
+
const argSize = args.length;
|
|
149
|
+
const context = await setup();
|
|
150
|
+
const initArgs = [() => done()];
|
|
151
|
+
if (init.length > 2) {
|
|
152
|
+
initArgs.unshift(args);
|
|
153
|
+
}
|
|
154
|
+
if (init.length > 1) {
|
|
155
|
+
initArgs.unshift(context);
|
|
156
|
+
}
|
|
157
|
+
const startMark = performance.now();
|
|
158
|
+
const action = await init(...initArgs);
|
|
159
|
+
const workMark = performance.now();
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
const loop = (resolve, reject) => {
|
|
163
|
+
const argIdx = i % argSize;
|
|
164
|
+
const timerId = setTimeout(reject, 10000, new Error('Timeout'));
|
|
165
|
+
|
|
166
|
+
done = () => {
|
|
167
|
+
// eslint-disable-next-line
|
|
168
|
+
const elapsed = performance.now() - startTickTime;
|
|
169
|
+
clearTimeout(timerId);
|
|
170
|
+
done = FALSE_START;
|
|
171
|
+
timings.push(elapsed);
|
|
172
|
+
|
|
173
|
+
resolve();
|
|
174
|
+
};
|
|
175
|
+
const startTickTime = performance.now();
|
|
176
|
+
action(...args[argIdx], count - i);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
while (i--) {
|
|
180
|
+
await new Promise(loop);
|
|
181
|
+
}
|
|
182
|
+
const completeMark = performance.now();
|
|
183
|
+
|
|
184
|
+
await teardown(context);
|
|
185
|
+
|
|
186
|
+
timings.sort();
|
|
187
|
+
|
|
188
|
+
const {
|
|
189
|
+
mode: { time: mode },
|
|
190
|
+
} = timings
|
|
191
|
+
.map((time) => parseFloat(time.toFixed(ACCURACY)))
|
|
192
|
+
.reduce((result, time) => {
|
|
193
|
+
const value = (result[time] || 0) + 1;
|
|
194
|
+
const mode = !result.mode || result.mode.value < value ? { time, value } : result.mode;
|
|
195
|
+
return { ...result, [time]: value, mode };
|
|
196
|
+
}, {});
|
|
197
|
+
|
|
198
|
+
const min = timings.reduce((a, b) => Math.min(a, b), Infinity);
|
|
199
|
+
const max = timings.reduce((a, b) => Math.max(a, b), 0);
|
|
200
|
+
const avg = timings.reduce((a, b) => a + b, 0) / count;
|
|
201
|
+
const p90idx = parseInt(timings.length * 0.9, 10);
|
|
202
|
+
const p95idx = parseInt(timings.length * 0.95, 10);
|
|
203
|
+
const p99idx = parseInt(timings.length * 0.99, 10);
|
|
204
|
+
const p90 = timings[p90idx];
|
|
205
|
+
const p95 = timings[p95idx];
|
|
206
|
+
const p99 = timings[p99idx];
|
|
207
|
+
|
|
208
|
+
WorkerThreads.parentPort.postMessage({
|
|
209
|
+
min,
|
|
210
|
+
max,
|
|
211
|
+
avg,
|
|
212
|
+
p90,
|
|
213
|
+
p95,
|
|
214
|
+
p99,
|
|
215
|
+
mode,
|
|
216
|
+
setup: workMark - startMark,
|
|
217
|
+
work: completeMark - workMark,
|
|
218
|
+
success: true,
|
|
219
|
+
});
|
|
220
|
+
} catch (error) {
|
|
221
|
+
WorkerThreads.parentPort.postMessage({ success: false, error: error.stack });
|
|
222
|
+
}
|
|
223
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "overtake",
|
|
3
|
+
"version": "0.0.1-rc1",
|
|
4
|
+
"description": "NodeJS performance benchmark",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"bin": {
|
|
9
|
+
"overtake": "cli.js"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"measure": "node ./__runner__/class-vs-function.js",
|
|
13
|
+
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
|
|
14
|
+
"lint": "eslint .",
|
|
15
|
+
"prepare": "husky install"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/3axap4eHko/overtake.git"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"benchmark",
|
|
23
|
+
"performance",
|
|
24
|
+
"measure",
|
|
25
|
+
"worker"
|
|
26
|
+
],
|
|
27
|
+
"author": "Ivan Zakharchanka",
|
|
28
|
+
"license": "Apache-2.0",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/3axap4eHko/overtake/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/3axap4eHko/overtake#readme",
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@jest/globals": "^27.5.1",
|
|
35
|
+
"@types/jest": "^27.4.1",
|
|
36
|
+
"eslint": "^8.14.0",
|
|
37
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
38
|
+
"eslint-config-prettier": "^8.5.0",
|
|
39
|
+
"eslint-plugin-import": "^2.26.0",
|
|
40
|
+
"husky": "^7.0.4",
|
|
41
|
+
"jest": "^27.5.1",
|
|
42
|
+
"prettier": "^2.6.2",
|
|
43
|
+
"pretty-quick": "^3.1.3"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"glob": "^8.0.1"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/runner.js
ADDED