overtake 0.1.2 → 1.0.0-rc.0
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/README.md +72 -79
- package/bin/overtake.js +2 -0
- package/build/__tests__/runner.d.ts +1 -0
- package/build/benchmark.cjs +237 -0
- package/build/benchmark.cjs.map +1 -0
- package/build/benchmark.d.ts +64 -0
- package/build/benchmark.js +189 -0
- package/build/benchmark.js.map +1 -0
- package/build/cli.cjs +149 -0
- package/build/cli.cjs.map +1 -0
- package/build/cli.d.ts +1 -0
- package/build/cli.js +104 -0
- package/build/cli.js.map +1 -0
- package/build/executor.cjs +68 -0
- package/build/executor.cjs.map +1 -0
- package/build/executor.d.ts +10 -0
- package/build/executor.js +58 -0
- package/build/executor.js.map +1 -0
- package/build/index.cjs +20 -0
- package/build/index.cjs.map +1 -0
- package/build/index.d.ts +5 -0
- package/build/index.js +3 -0
- package/build/index.js.map +1 -0
- package/build/queue.cjs +48 -0
- package/build/queue.cjs.map +1 -0
- package/build/queue.d.ts +3 -0
- package/build/queue.js +38 -0
- package/build/queue.js.map +1 -0
- package/build/reporter.cjs +175 -0
- package/build/reporter.cjs.map +1 -0
- package/build/reporter.d.ts +11 -0
- package/build/reporter.js +157 -0
- package/build/reporter.js.map +1 -0
- package/build/runner.cjs +92 -0
- package/build/runner.cjs.map +1 -0
- package/build/runner.d.ts +2 -0
- package/build/runner.js +82 -0
- package/build/runner.js.map +1 -0
- package/build/types.cjs +48 -0
- package/build/types.cjs.map +1 -0
- package/build/types.d.ts +59 -0
- package/build/types.js +21 -0
- package/build/types.js.map +1 -0
- package/build/utils.cjs +100 -0
- package/build/utils.cjs.map +1 -0
- package/build/utils.d.ts +20 -0
- package/build/utils.js +67 -0
- package/build/utils.js.map +1 -0
- package/build/worker.cjs +29 -0
- package/build/worker.cjs.map +1 -0
- package/build/worker.d.ts +1 -0
- package/build/worker.js +25 -0
- package/build/worker.js.map +1 -0
- package/package.json +13 -11
- package/src/__tests__/runner.ts +34 -0
- package/src/benchmark.ts +231 -0
- package/src/cli.ts +114 -0
- package/src/executor.ts +73 -0
- package/src/index.ts +6 -0
- package/src/queue.ts +42 -0
- package/src/reporter.ts +139 -0
- package/src/runner.ts +111 -0
- package/src/types.ts +72 -0
- package/src/utils.ts +65 -0
- package/src/worker.ts +46 -0
- package/tsconfig.json +17 -0
- package/cli.js +0 -70
- package/index.d.ts +0 -56
- package/index.js +0 -303
- package/runner.js +0 -3
package/README.md
CHANGED
|
@@ -14,7 +14,6 @@ Performance benchmark for NodeJS
|
|
|
14
14
|
- [Features](#features)
|
|
15
15
|
- [Installing](#installing)
|
|
16
16
|
- [Examples](#examples)
|
|
17
|
-
- [Showcase](#showcase)
|
|
18
17
|
- [License](#license)
|
|
19
18
|
|
|
20
19
|
## Features
|
|
@@ -25,10 +24,10 @@ Performance benchmark for NodeJS
|
|
|
25
24
|
|
|
26
25
|
## Installing
|
|
27
26
|
|
|
28
|
-
Using
|
|
27
|
+
Using pnpm:
|
|
29
28
|
|
|
30
29
|
```bash
|
|
31
|
-
$
|
|
30
|
+
$ pnpm add -D overtake
|
|
32
31
|
```
|
|
33
32
|
|
|
34
33
|
Using npm:
|
|
@@ -39,101 +38,95 @@ $ npm install -D overtake
|
|
|
39
38
|
|
|
40
39
|
## Examples
|
|
41
40
|
|
|
42
|
-
###
|
|
41
|
+
### From command line
|
|
43
42
|
|
|
44
|
-
Create a benchmark
|
|
43
|
+
Create a benchmark file
|
|
45
44
|
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const postgres = new Client();
|
|
52
|
-
await postgres.connect();
|
|
45
|
+
```typescript
|
|
46
|
+
// src/__bench__/array-copy.ts
|
|
47
|
+
const suite = benchmark('1M array of strings', () => Array.from({ length: 1_000_000 }, (_, idx) => `${idx}`))
|
|
48
|
+
.feed('1M array of numbers', () => Array.from({ length: 1_000_000 }, (_, idx) => idx))
|
|
49
|
+
.feed('1M typed array', () => new Uint32Array(1_000_000).map((_, idx) => idx));
|
|
53
50
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
measure('mongodb inserts', ({ mongo } /* context */, next) => {
|
|
62
|
-
// prepare a collection
|
|
63
|
-
const database = mongo.db('overtake');
|
|
64
|
-
const test = database.collection('test');
|
|
65
|
-
|
|
66
|
-
return (data) => test.insertOne(data).then(next);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
measure('postgres inserts', ({ postgres } /* context */, next) => {
|
|
70
|
-
// prepare a query
|
|
71
|
-
const query = 'INSERT INTO overtake(value) VALUES($1) RETURNING *';
|
|
72
|
-
|
|
73
|
-
return (data) => postgres.query(query, [data.value]).then(next);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
teardown(async ({ mongo, postgres }) => {
|
|
77
|
-
await postgres.end();
|
|
78
|
-
await mongo.end();
|
|
79
|
-
});
|
|
51
|
+
suite.target('for loop').measure('copy half', (_, input) => {
|
|
52
|
+
const n = input?.length ?? 0;
|
|
53
|
+
const mid = n / 2;
|
|
54
|
+
for (let i = 0; i < mid; i++) {
|
|
55
|
+
input[i + mid] = input[i];
|
|
56
|
+
}
|
|
57
|
+
});
|
|
80
58
|
|
|
81
|
-
|
|
59
|
+
suite.target('copyWithin').measure('copy half', (_, input) => {
|
|
60
|
+
const n = input?.length ?? 0;
|
|
61
|
+
const mid = n / 2;
|
|
62
|
+
input.copyWithin(mid, 0, mid);
|
|
82
63
|
});
|
|
83
64
|
```
|
|
84
65
|
|
|
85
|
-
|
|
66
|
+
Run the command
|
|
86
67
|
|
|
87
68
|
```bash
|
|
88
|
-
|
|
69
|
+
npx overtake src/__bench__/array-copy.ts -f table -r ops mode mean p99
|
|
89
70
|
```
|
|
90
71
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
72
|
+
```
|
|
73
|
+
for loop copy half
|
|
74
|
+
┌─────────────────────┬──────────────────────┬─────────────┬─────────────┬─────────────────────┬────────┐
|
|
75
|
+
│ (index) │ ops │ mode │ mean │ p99 │ count │
|
|
76
|
+
├─────────────────────┼──────────────────────┼─────────────┼─────────────┼─────────────────────┼────────┤
|
|
77
|
+
│ 1M typed array │ '3698 ops/s ± 0.81%' │ '256.65 µs' │ '270.38 µs' │ '574.7 µs ± 0.19%' │ '1000' │
|
|
78
|
+
│ 1M array of numbers │ '2902 ops/s ± 0.3%' │ '343.92 µs' │ '344.51 µs' │ '429.24 µs ± 0.2%' │ '1000' │
|
|
79
|
+
│ 1M array of strings │ '2277 ops/s ± 0.46%' │ '397.15 µs' │ '438.99 µs' │ '569.15 µs ± 0.12%' │ '1000' │
|
|
80
|
+
└─────────────────────┴──────────────────────┴─────────────┴─────────────┴─────────────────────┴────────┘
|
|
81
|
+
|
|
82
|
+
copyWithin copy half
|
|
83
|
+
┌─────────────────────┬───────────────────────┬────────────┬────────────┬────────────────────┬────────┐
|
|
84
|
+
│ (index) │ ops │ mode │ mean │ p99 │ count │
|
|
85
|
+
├─────────────────────┼───────────────────────┼────────────┼────────────┼────────────────────┼────────┤
|
|
86
|
+
│ 1M typed array │ '17454 ops/s ± 0.67%' │ '53.11 µs' │ '57.29 µs' │ '81.18 µs ± 1.54%' │ '1000' │
|
|
87
|
+
│ 1M array of numbers │ '103 ops/s ± 0.02%' │ '9.49 ms' │ '9.64 ms' │ '9.91 ms ± 0.38%' │ '50' │
|
|
88
|
+
│ 1M array of strings │ '101 ops/s ± 0.06%' │ '9.55 ms' │ '9.87 ms' │ '10.87 ms ± 2.67%' │ '98' │
|
|
89
|
+
└─────────────────────┴───────────────────────┴────────────┴────────────┴────────────────────┴────────┘
|
|
95
90
|
```
|
|
96
91
|
|
|
97
|
-
###
|
|
92
|
+
### From a standalone module
|
|
98
93
|
|
|
99
|
-
|
|
100
|
-
npx overtake -i "class A{}" -i "function A() {}" -i "A = () => {};" -c 20000
|
|
101
|
-
```
|
|
94
|
+
Create a benchmark file
|
|
102
95
|
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
├─────────┼──────────┼──────────┼──────────┼───────────┼───────┤
|
|
111
|
-
│ 0.0005 │ 0.000551 │ 0.001493 │ 0.002344 │ 16.506385 │ 20000 │
|
|
112
|
-
└─────────┴──────────┴──────────┴──────────┴───────────┴───────┘
|
|
113
|
-
✓ Measure function A() {}
|
|
114
|
-
┌─────────┬─────────┬────────┬──────────┬──────────┬───────┐
|
|
115
|
-
│ (index) │ med │ p95 │ p99 │ total │ count │
|
|
116
|
-
├─────────┼─────────┼────────┼──────────┼──────────┼───────┤
|
|
117
|
-
│ 0.00008 │ 0.00009 │ 0.0003 │ 0.000441 │ 2.875578 │ 20000 │
|
|
118
|
-
└─────────┴─────────┴────────┴──────────┴──────────┴───────┘
|
|
119
|
-
✓ Measure A = () => {};
|
|
120
|
-
┌─────────┬─────────┬──────────┬──────────┬─────────┬───────┐
|
|
121
|
-
│ (index) │ med │ p95 │ p99 │ total │ count │
|
|
122
|
-
├─────────┼─────────┼──────────┼──────────┼─────────┼───────┤
|
|
123
|
-
│ 0.00008 │ 0.00012 │ 0.000331 │ 0.000601 │ 3.42556 │ 20000 │
|
|
124
|
-
└─────────┴─────────┴──────────┴──────────┴─────────┴───────┘
|
|
125
|
-
```
|
|
96
|
+
```typescript
|
|
97
|
+
// src/__bench__/array-copy.js
|
|
98
|
+
import { Benchmark, printTableReports } from 'overtake';
|
|
99
|
+
|
|
100
|
+
const benchmark = Benchmark.create('1M array of strings', () => Array.from({ length: 1_000_000 }, (_, idx) => `${idx}`))
|
|
101
|
+
.feed('1M array of numbers', () => Array.from({ length: 1_000_000 }, (_, idx) => idx))
|
|
102
|
+
.feed('1M typed array', () => new Uint32Array(1_000_000).map((_, idx) => idx));
|
|
126
103
|
|
|
127
|
-
|
|
104
|
+
benchmark.target('for loop').measure('copy half', (_, input) => {
|
|
105
|
+
const n = input?.length ?? 0;
|
|
106
|
+
const mid = n / 2;
|
|
107
|
+
for (let i = 0; i < mid; i++) {
|
|
108
|
+
input[i + mid] = input[i];
|
|
109
|
+
}
|
|
110
|
+
});
|
|
128
111
|
|
|
129
|
-
|
|
112
|
+
benchmark.target('copyWithin').measure('copy half', (_, input) => {
|
|
113
|
+
const n = input?.length ?? 0;
|
|
114
|
+
const mid = n / 2;
|
|
115
|
+
input.copyWithin(mid, 0, mid);
|
|
116
|
+
});
|
|
130
117
|
|
|
131
|
-
|
|
118
|
+
const reports = await benchmark.execute({
|
|
119
|
+
reportTypes: ['ops', 'mode', 'mean', 'p99'],
|
|
120
|
+
});
|
|
132
121
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
122
|
+
printTableReports(reports);
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
And run the command
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
node src/__bench__/array-copy.js
|
|
129
|
+
```
|
|
137
130
|
|
|
138
131
|
## License
|
|
139
132
|
|
package/bin/overtake.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
AsyncFunction: function() {
|
|
13
|
+
return AsyncFunction;
|
|
14
|
+
},
|
|
15
|
+
Benchmark: function() {
|
|
16
|
+
return Benchmark;
|
|
17
|
+
},
|
|
18
|
+
DEFAULT_REPORT_TYPES: function() {
|
|
19
|
+
return DEFAULT_REPORT_TYPES;
|
|
20
|
+
},
|
|
21
|
+
DEFAULT_WORKERS: function() {
|
|
22
|
+
return DEFAULT_WORKERS;
|
|
23
|
+
},
|
|
24
|
+
FeedContext: function() {
|
|
25
|
+
return FeedContext;
|
|
26
|
+
},
|
|
27
|
+
Measure: function() {
|
|
28
|
+
return Measure;
|
|
29
|
+
},
|
|
30
|
+
MeasureContext: function() {
|
|
31
|
+
return MeasureContext;
|
|
32
|
+
},
|
|
33
|
+
Target: function() {
|
|
34
|
+
return Target;
|
|
35
|
+
},
|
|
36
|
+
TargetContext: function() {
|
|
37
|
+
return TargetContext;
|
|
38
|
+
},
|
|
39
|
+
printJSONReports: function() {
|
|
40
|
+
return printJSONReports;
|
|
41
|
+
},
|
|
42
|
+
printSimpleReports: function() {
|
|
43
|
+
return printSimpleReports;
|
|
44
|
+
},
|
|
45
|
+
printTableReports: function() {
|
|
46
|
+
return printTableReports;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
const _nodeos = require("node:os");
|
|
50
|
+
const _executorcjs = require("./executor.cjs");
|
|
51
|
+
const _typescjs = require("./types.cjs");
|
|
52
|
+
const DEFAULT_WORKERS = (0, _nodeos.cpus)().length;
|
|
53
|
+
const AsyncFunction = (async ()=>{}).constructor;
|
|
54
|
+
const DEFAULT_REPORT_TYPES = [
|
|
55
|
+
'ops'
|
|
56
|
+
];
|
|
57
|
+
class MeasureContext {
|
|
58
|
+
title;
|
|
59
|
+
run;
|
|
60
|
+
pre;
|
|
61
|
+
post;
|
|
62
|
+
constructor(title, run){
|
|
63
|
+
this.title = title;
|
|
64
|
+
this.run = run;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
class Measure {
|
|
68
|
+
#ctx;
|
|
69
|
+
constructor(ctx){
|
|
70
|
+
this.#ctx = ctx;
|
|
71
|
+
}
|
|
72
|
+
pre(fn) {
|
|
73
|
+
this.#ctx.pre = fn;
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
post(fn) {
|
|
77
|
+
this.#ctx.post = fn;
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
class TargetContext {
|
|
82
|
+
title;
|
|
83
|
+
setup;
|
|
84
|
+
teardown;
|
|
85
|
+
measures;
|
|
86
|
+
constructor(title, setup){
|
|
87
|
+
this.title = title;
|
|
88
|
+
this.setup = setup;
|
|
89
|
+
this.measures = [];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
class Target {
|
|
93
|
+
#ctx;
|
|
94
|
+
constructor(ctx){
|
|
95
|
+
this.#ctx = ctx;
|
|
96
|
+
}
|
|
97
|
+
teardown(fn) {
|
|
98
|
+
this.#ctx.teardown = fn;
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
measure(title, run) {
|
|
102
|
+
const measure = new MeasureContext(title, run);
|
|
103
|
+
this.#ctx.measures.push(measure);
|
|
104
|
+
return new Measure(measure);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
class FeedContext {
|
|
108
|
+
title;
|
|
109
|
+
fn;
|
|
110
|
+
constructor(title, fn){
|
|
111
|
+
this.title = title;
|
|
112
|
+
this.fn = fn;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
class Benchmark {
|
|
116
|
+
#targets = [];
|
|
117
|
+
#feeds = [];
|
|
118
|
+
#executed = false;
|
|
119
|
+
static create(title, fn) {
|
|
120
|
+
if (fn) {
|
|
121
|
+
return new Benchmark(title, fn);
|
|
122
|
+
} else {
|
|
123
|
+
return new Benchmark(title);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
constructor(title, fn){
|
|
127
|
+
if (fn) {
|
|
128
|
+
this.feed(title, fn);
|
|
129
|
+
} else {
|
|
130
|
+
this.feed(title);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
feed(title, fn) {
|
|
134
|
+
const self = this;
|
|
135
|
+
self.#feeds.push(fn ? new FeedContext(title, fn) : new FeedContext(title));
|
|
136
|
+
return self;
|
|
137
|
+
}
|
|
138
|
+
target(title, setup) {
|
|
139
|
+
const target = new TargetContext(title, setup);
|
|
140
|
+
this.#targets.push(target);
|
|
141
|
+
return new Target(target);
|
|
142
|
+
}
|
|
143
|
+
async execute({ workers = DEFAULT_WORKERS, warmupCycles = 20, maxCycles = _typescjs.DEFAULT_CYCLES, minCycles = 50, absThreshold = 1_000, relThreshold = 0.02, reportTypes = DEFAULT_REPORT_TYPES }) {
|
|
144
|
+
if (this.#executed) {
|
|
145
|
+
throw new Error('Benchmark is executed and can\'t be reused');
|
|
146
|
+
}
|
|
147
|
+
this.#executed = true;
|
|
148
|
+
const executor = (0, _executorcjs.createExecutor)({
|
|
149
|
+
workers,
|
|
150
|
+
warmupCycles,
|
|
151
|
+
maxCycles,
|
|
152
|
+
minCycles,
|
|
153
|
+
absThreshold,
|
|
154
|
+
relThreshold,
|
|
155
|
+
reportTypes
|
|
156
|
+
});
|
|
157
|
+
const reports = [];
|
|
158
|
+
for (const target of this.#targets){
|
|
159
|
+
const targetReport = {
|
|
160
|
+
target: target.title,
|
|
161
|
+
measures: []
|
|
162
|
+
};
|
|
163
|
+
for (const measure of target.measures){
|
|
164
|
+
const measureReport = {
|
|
165
|
+
measure: measure.title,
|
|
166
|
+
feeds: []
|
|
167
|
+
};
|
|
168
|
+
for (const feed of this.#feeds){
|
|
169
|
+
const data = await feed.fn?.();
|
|
170
|
+
executor.push({
|
|
171
|
+
setup: target.setup,
|
|
172
|
+
teardown: target.teardown,
|
|
173
|
+
pre: measure.pre,
|
|
174
|
+
run: measure.run,
|
|
175
|
+
post: measure.post,
|
|
176
|
+
data
|
|
177
|
+
}).then((data)=>{
|
|
178
|
+
measureReport.feeds.push({
|
|
179
|
+
feed: feed.title,
|
|
180
|
+
data
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
targetReport.measures.push(measureReport);
|
|
185
|
+
}
|
|
186
|
+
reports.push(targetReport);
|
|
187
|
+
}
|
|
188
|
+
await executor.drain();
|
|
189
|
+
executor.kill();
|
|
190
|
+
return reports;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const printSimpleReports = (reports)=>{
|
|
194
|
+
for (const report of reports){
|
|
195
|
+
for (const { measure, feeds } of report.measures){
|
|
196
|
+
console.group('\n', report.target, measure);
|
|
197
|
+
for (const { feed, data } of feeds){
|
|
198
|
+
const output = Object.entries(data).map(([key, report])=>`${key}: ${report.toString()}`).join('; ');
|
|
199
|
+
console.log(feed, output);
|
|
200
|
+
}
|
|
201
|
+
console.groupEnd();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
const printTableReports = (reports)=>{
|
|
206
|
+
for (const report of reports){
|
|
207
|
+
for (const { measure, feeds } of report.measures){
|
|
208
|
+
console.log('\n', report.target, measure);
|
|
209
|
+
const table = {};
|
|
210
|
+
for (const { feed, data } of feeds){
|
|
211
|
+
table[feed] = Object.fromEntries(Object.entries(data).map(([key, report])=>[
|
|
212
|
+
key,
|
|
213
|
+
report.toString()
|
|
214
|
+
]));
|
|
215
|
+
}
|
|
216
|
+
console.table(table);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
const printJSONReports = (reports, padding)=>{
|
|
221
|
+
const output = {};
|
|
222
|
+
for (const report of reports){
|
|
223
|
+
for (const { measure, feeds } of report.measures){
|
|
224
|
+
const row = {};
|
|
225
|
+
for (const { feed, data } of feeds){
|
|
226
|
+
row[feed] = Object.fromEntries(Object.entries(data).map(([key, report])=>[
|
|
227
|
+
key,
|
|
228
|
+
report.toString()
|
|
229
|
+
]));
|
|
230
|
+
}
|
|
231
|
+
output[`${report.target} ${measure}`] = row;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
console.log(JSON.stringify(output, null, padding));
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
//# sourceMappingURL=benchmark.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/benchmark.ts"],"sourcesContent":["import { cpus } from 'node:os';\nimport { createExecutor, ExecutorOptions, ExecutorReport } from './executor.js';\nimport { MaybePromise, StepFn, SetupFn, TeardownFn, FeedFn, ReportType, ReportTypeList, DEFAULT_CYCLES } from './types.js';\n\nexport const DEFAULT_WORKERS = cpus().length;\n\nexport const AsyncFunction = (async () => { }).constructor;\n\nexport interface TargetReport<R extends ReportTypeList> {\n target: string;\n measures: MeasureReport<R>[];\n}\n\nexport interface MeasureReport<R extends ReportTypeList> {\n measure: string;\n feeds: FeedReport<R>[];\n}\n\nexport interface FeedReport<R extends ReportTypeList> {\n feed: string;\n data: ExecutorReport<R>;\n}\n\nexport const DEFAULT_REPORT_TYPES = ['ops'] as const;\nexport type DefaultReportTypes = typeof DEFAULT_REPORT_TYPES[number];\n\nexport class MeasureContext<TContext, TInput> {\n public pre?: StepFn<TContext, TInput>;\n public post?: StepFn<TContext, TInput>;\n\n constructor(\n public title: string,\n public run: StepFn<TContext, TInput>,\n ) { }\n}\n\nexport class Measure<TContext, TInput> {\n #ctx: MeasureContext<TContext, TInput>;\n\n constructor(ctx: MeasureContext<TContext, TInput>) {\n this.#ctx = ctx;\n }\n\n pre(fn: StepFn<TContext, TInput>): Measure<TContext, TInput> {\n this.#ctx.pre = fn;\n return this;\n }\n post(fn: StepFn<TContext, TInput>): Measure<TContext, TInput> {\n this.#ctx.post = fn;\n return this;\n }\n}\n\nexport class TargetContext<TContext, TInput> {\n public teardown?: TeardownFn<TContext>;\n public measures: MeasureContext<TContext, TInput>[] = [];\n\n constructor(\n readonly title: string,\n readonly setup?: SetupFn<MaybePromise<TContext>>,\n ) {\n }\n}\n\nexport class Target<TContext, TInput> {\n #ctx: TargetContext<TContext, TInput>;\n\n constructor(ctx: TargetContext<TContext, TInput>) {\n this.#ctx = ctx;\n }\n teardown(fn: TeardownFn<TContext>): Target<TContext, TInput> {\n this.#ctx.teardown = fn;\n\n return this;\n }\n measure(title: string, run: StepFn<TContext, TInput>): Measure<TContext, TInput> {\n const measure = new MeasureContext(title, run);\n this.#ctx.measures.push(measure);\n\n return new Measure(measure);\n }\n}\n\nexport class FeedContext<TInput> {\n constructor(\n readonly title: string,\n readonly fn?: FeedFn<TInput>,\n ) { }\n}\n\nexport class Benchmark<TInput> {\n #targets: TargetContext<unknown, TInput>[] = [];\n #feeds: FeedContext<TInput>[] = [];\n #executed = false;\n\n static create(title: string): Benchmark<void>;\n static create<I>(title: string, fn: FeedFn<I>): Benchmark<I>;\n static create<I>(title: string, fn?: FeedFn<I> | undefined): Benchmark<I> {\n if (fn) {\n return new Benchmark(title, fn);\n } else {\n return new Benchmark(title);\n }\n }\n\n constructor(title: string);\n constructor(title: string, fn: FeedFn<TInput>);\n constructor(title: string, fn?: FeedFn<TInput> | undefined) {\n if (fn) {\n this.feed(title, fn);\n } else {\n this.feed(title);\n }\n }\n\n feed(title: string): Benchmark<TInput | void>;\n feed<I>(title: string, fn: FeedFn<I>): Benchmark<TInput | I>;\n feed<I>(title: string, fn?: FeedFn<I> | undefined): Benchmark<TInput | I> {\n const self = (this as Benchmark<TInput | I>);\n self.#feeds.push(\n fn\n ? new FeedContext(title, fn)\n : new FeedContext(title)\n );\n\n return self;\n }\n\n target<TContext>(title: string): Target<void, TInput>;\n target<TContext>(title: string, setup: SetupFn<Awaited<TContext>>): Target<TContext, TInput>;\n target<TContext>(title: string, setup?: SetupFn<Awaited<TContext>> | undefined): Target<TContext, TInput> {\n const target = new TargetContext<TContext, TInput>(title, setup);\n this.#targets.push(target as TargetContext<unknown, TInput>);\n\n return new Target<TContext, TInput>(target);\n }\n\n async execute<R extends readonly ReportType[] = typeof DEFAULT_REPORT_TYPES>(\n {\n workers = DEFAULT_WORKERS,\n warmupCycles = 20,\n maxCycles = DEFAULT_CYCLES,\n minCycles = 50,\n absThreshold = 1_000,\n relThreshold = 0.02,\n reportTypes = DEFAULT_REPORT_TYPES as unknown as R,\n }: ExecutorOptions<R>): Promise<TargetReport<R>[]> {\n if (this.#executed) {\n throw new Error('Benchmark is executed and can\\'t be reused');\n }\n this.#executed = true;\n\n const executor = createExecutor<unknown, TInput, R>({\n workers,\n warmupCycles,\n maxCycles,\n minCycles,\n absThreshold,\n relThreshold,\n reportTypes,\n });\n\n const reports: TargetReport<R>[] = [];\n for (const target of this.#targets) {\n const targetReport: TargetReport<R> = { target: target.title, measures: [] };\n for (const measure of target.measures) {\n const measureReport: MeasureReport<R> = { measure: measure.title, feeds: [] };\n for (const feed of this.#feeds) {\n const data = await feed.fn?.();\n executor.push<ExecutorReport<R>>({\n setup: target.setup,\n teardown: target.teardown,\n pre: measure.pre,\n run: measure.run,\n post: measure.post,\n data,\n }).then(data => {\n measureReport.feeds.push({\n feed: feed.title,\n data,\n });\n });\n }\n targetReport.measures.push(measureReport);\n }\n reports.push(targetReport);\n }\n await executor.drain();\n executor.kill();\n\n return reports;\n }\n}\n\nexport const printSimpleReports = <R extends ReportTypeList>(reports: TargetReport<R>[]) => {\n for (const report of reports) {\n for (const { measure, feeds } of report.measures) {\n console.group('\\n', report.target, measure);\n for (const { feed, data } of feeds) {\n const output = Object.entries(data).map(([key, report]) => `${key}: ${report.toString()}`).join('; ');\n console.log(feed, output);\n }\n console.groupEnd();\n }\n }\n};\n\nexport const printTableReports = <R extends ReportTypeList>(reports: TargetReport<R>[]) => {\n for (const report of reports) {\n for (const { measure, feeds } of report.measures) {\n console.log('\\n', report.target, measure);\n const table: Record<string, unknown> = {};\n for (const { feed, data } of feeds) {\n table[feed] = Object.fromEntries(Object.entries(data).map(([key, report]) => [key, report.toString()]));\n }\n console.table(table);\n }\n }\n};\n\nexport const printJSONReports = <R extends ReportTypeList>(reports: TargetReport<R>[], padding?: number) => {\n const output = {} as Record<string, Record<string, Record<string, string>>>;\n for (const report of reports) {\n for (const { measure, feeds } of report.measures) {\n const row = {} as Record<string, Record<string, string>>;\n for (const { feed, data } of feeds) {\n row[feed] = Object.fromEntries(Object.entries(data).map(([key, report]) => [key, report.toString()]));\n }\n output[`${report.target} ${measure}`] = row;\n }\n }\n console.log(JSON.stringify(output, null, padding));\n};\n\n"],"names":["AsyncFunction","Benchmark","DEFAULT_REPORT_TYPES","DEFAULT_WORKERS","FeedContext","Measure","MeasureContext","Target","TargetContext","printJSONReports","printSimpleReports","printTableReports","cpus","length","constructor","pre","post","title","run","ctx","fn","teardown","measures","setup","measure","push","create","feed","self","target","execute","workers","warmupCycles","maxCycles","DEFAULT_CYCLES","minCycles","absThreshold","relThreshold","reportTypes","Error","executor","createExecutor","reports","targetReport","measureReport","feeds","data","then","drain","kill","report","console","group","output","Object","entries","map","key","toString","join","log","groupEnd","table","fromEntries","padding","row","JSON","stringify"],"mappings":";;;;;;;;;;;IAMaA,aAAa;eAAbA;;IAoFAC,SAAS;eAATA;;IAnEAC,oBAAoB;eAApBA;;IAnBAC,eAAe;eAAfA;;IA+EAC,WAAW;eAAXA;;IA/CAC,OAAO;eAAPA;;IAVAC,cAAc;eAAdA;;IAsCAC,MAAM;eAANA;;IAXAC,aAAa;eAAbA;;IAuKAC,gBAAgB;eAAhBA;;IA1BAC,kBAAkB;eAAlBA;;IAaAC,iBAAiB;eAAjBA;;;wBA/MQ;6BAC2C;0BAC8C;AAEvG,MAAMR,kBAAkBS,IAAAA,YAAI,IAAGC,MAAM;AAErC,MAAMb,gBAAgB,AAAC,CAAA,WAAc,CAAA,EAAGc,WAAW;AAiBnD,MAAMZ,uBAAuB;IAAC;CAAM;AAGpC,MAAMI;;;IACJS,IAA+B;IAC/BC,KAAgC;IAEvCF,YACE,AAAOG,KAAa,EACpB,AAAOC,GAA6B,CACpC;aAFOD,QAAAA;aACAC,MAAAA;IACL;AACN;AAEO,MAAMb;IACX,CAAA,GAAI,CAAmC;IAEvCS,YAAYK,GAAqC,CAAE;QACjD,IAAI,CAAC,CAAA,GAAI,GAAGA;IACd;IAEAJ,IAAIK,EAA4B,EAA6B;QAC3D,IAAI,CAAC,CAAA,GAAI,CAACL,GAAG,GAAGK;QAChB,OAAO,IAAI;IACb;IACAJ,KAAKI,EAA4B,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BAAKP,OAAOQ,QAAQ;qBAAG;YACvG;YACAP,QAAQW,KAAK,CAACA;QAChB;IACF;AACF;AAEO,MAAMrD,mBAAmB,CAA2BiC,SAA4BsB;IACrF,MAAMX,SAAS,CAAC;IAChB,KAAK,MAAMH,UAAUR,QAAS;QAC5B,KAAK,MAAM,EAAElB,OAAO,EAAEqB,KAAK,EAAE,IAAIK,OAAO5B,QAAQ,CAAE;YAChD,MAAM2C,MAAM,CAAC;YACb,KAAK,MAAM,EAAEtC,IAAI,EAAEmB,IAAI,EAAE,IAAID,MAAO;gBAClCoB,GAAG,CAACtC,KAAK,GAAG2B,OAAOS,WAAW,CAACT,OAAOC,OAAO,CAACT,MAAMU,GAAG,CAAC,CAAC,CAACC,KAAKP,OAAO,GAAK;wBAACO;wBAAKP,OAAOQ,QAAQ;qBAAG;YACrG;YACAL,MAAM,CAAC,GAAGH,OAAOrB,MAAM,CAAC,CAAC,EAAEL,SAAS,CAAC,GAAGyC;QAC1C;IACF;IACAd,QAAQS,GAAG,CAACM,KAAKC,SAAS,CAACd,QAAQ,MAAMW;AAC3C"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ExecutorOptions, ExecutorReport } from './executor.js';
|
|
2
|
+
import { MaybePromise, StepFn, SetupFn, TeardownFn, FeedFn, ReportType, ReportTypeList } from './types.js';
|
|
3
|
+
export declare const DEFAULT_WORKERS: number;
|
|
4
|
+
export declare const AsyncFunction: Function;
|
|
5
|
+
export interface TargetReport<R extends ReportTypeList> {
|
|
6
|
+
target: string;
|
|
7
|
+
measures: MeasureReport<R>[];
|
|
8
|
+
}
|
|
9
|
+
export interface MeasureReport<R extends ReportTypeList> {
|
|
10
|
+
measure: string;
|
|
11
|
+
feeds: FeedReport<R>[];
|
|
12
|
+
}
|
|
13
|
+
export interface FeedReport<R extends ReportTypeList> {
|
|
14
|
+
feed: string;
|
|
15
|
+
data: ExecutorReport<R>;
|
|
16
|
+
}
|
|
17
|
+
export declare const DEFAULT_REPORT_TYPES: readonly ["ops"];
|
|
18
|
+
export type DefaultReportTypes = typeof DEFAULT_REPORT_TYPES[number];
|
|
19
|
+
export declare class MeasureContext<TContext, TInput> {
|
|
20
|
+
title: string;
|
|
21
|
+
run: StepFn<TContext, TInput>;
|
|
22
|
+
pre?: StepFn<TContext, TInput>;
|
|
23
|
+
post?: StepFn<TContext, TInput>;
|
|
24
|
+
constructor(title: string, run: StepFn<TContext, TInput>);
|
|
25
|
+
}
|
|
26
|
+
export declare class Measure<TContext, TInput> {
|
|
27
|
+
#private;
|
|
28
|
+
constructor(ctx: MeasureContext<TContext, TInput>);
|
|
29
|
+
pre(fn: StepFn<TContext, TInput>): Measure<TContext, TInput>;
|
|
30
|
+
post(fn: StepFn<TContext, TInput>): Measure<TContext, TInput>;
|
|
31
|
+
}
|
|
32
|
+
export declare class TargetContext<TContext, TInput> {
|
|
33
|
+
readonly title: string;
|
|
34
|
+
readonly setup?: SetupFn<MaybePromise<TContext>> | undefined;
|
|
35
|
+
teardown?: TeardownFn<TContext>;
|
|
36
|
+
measures: MeasureContext<TContext, TInput>[];
|
|
37
|
+
constructor(title: string, setup?: SetupFn<MaybePromise<TContext>> | undefined);
|
|
38
|
+
}
|
|
39
|
+
export declare class Target<TContext, TInput> {
|
|
40
|
+
#private;
|
|
41
|
+
constructor(ctx: TargetContext<TContext, TInput>);
|
|
42
|
+
teardown(fn: TeardownFn<TContext>): Target<TContext, TInput>;
|
|
43
|
+
measure(title: string, run: StepFn<TContext, TInput>): Measure<TContext, TInput>;
|
|
44
|
+
}
|
|
45
|
+
export declare class FeedContext<TInput> {
|
|
46
|
+
readonly title: string;
|
|
47
|
+
readonly fn?: FeedFn<TInput> | undefined;
|
|
48
|
+
constructor(title: string, fn?: FeedFn<TInput> | undefined);
|
|
49
|
+
}
|
|
50
|
+
export declare class Benchmark<TInput> {
|
|
51
|
+
#private;
|
|
52
|
+
static create(title: string): Benchmark<void>;
|
|
53
|
+
static create<I>(title: string, fn: FeedFn<I>): Benchmark<I>;
|
|
54
|
+
constructor(title: string);
|
|
55
|
+
constructor(title: string, fn: FeedFn<TInput>);
|
|
56
|
+
feed(title: string): Benchmark<TInput | void>;
|
|
57
|
+
feed<I>(title: string, fn: FeedFn<I>): Benchmark<TInput | I>;
|
|
58
|
+
target<TContext>(title: string): Target<void, TInput>;
|
|
59
|
+
target<TContext>(title: string, setup: SetupFn<Awaited<TContext>>): Target<TContext, TInput>;
|
|
60
|
+
execute<R extends readonly ReportType[] = typeof DEFAULT_REPORT_TYPES>({ workers, warmupCycles, maxCycles, minCycles, absThreshold, relThreshold, reportTypes, }: ExecutorOptions<R>): Promise<TargetReport<R>[]>;
|
|
61
|
+
}
|
|
62
|
+
export declare const printSimpleReports: <R extends ReportTypeList>(reports: TargetReport<R>[]) => void;
|
|
63
|
+
export declare const printTableReports: <R extends ReportTypeList>(reports: TargetReport<R>[]) => void;
|
|
64
|
+
export declare const printJSONReports: <R extends ReportTypeList>(reports: TargetReport<R>[], padding?: number) => void;
|