@indutny/bencher 1.0.0-rc.1
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 +19 -0
- package/README.md +44 -0
- package/dist/bin/bencher.js +180 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright Fedor Indutny, 2022.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @indutny/bencher
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@indutny/bencher)
|
|
4
|
+
[](https://bundlephobia.com/result?p=@indutny/bencher)
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
Sneaky equals comparison between objects that checks only the properties that
|
|
8
|
+
were touched.
|
|
9
|
+
|
|
10
|
+
Inspired by [proxy-compare](https://github.com/dai-shi/proxy-compare).
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install -g @indutny/bencher
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
benchmark.js
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
export const name = 'benchmark name';
|
|
24
|
+
|
|
25
|
+
// Function to benchmark
|
|
26
|
+
export default () => {
|
|
27
|
+
// Make sure to return a side-effect number
|
|
28
|
+
// (possibly result of a computation) to ensure that the pure function calls
|
|
29
|
+
// are not optimized out by the runtime.
|
|
30
|
+
return 0;
|
|
31
|
+
};
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
bencher benchmark.js
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Credits
|
|
39
|
+
|
|
40
|
+
Name coined by [Scott Nonnenberg](https://github.com/scottnonnenberg/).
|
|
41
|
+
|
|
42
|
+
## LICENSE
|
|
43
|
+
|
|
44
|
+
This software is licensed under the MIT License.
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
+
if (mod && mod.__esModule) return mod;
|
|
21
|
+
var result = {};
|
|
22
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
+
__setModuleDefault(result, mod);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
const promises_1 = require("fs/promises");
|
|
28
|
+
async function main() {
|
|
29
|
+
const modules = await Promise.all(process.argv.slice(2).map(async (file) => {
|
|
30
|
+
var _a;
|
|
31
|
+
const path = await (0, promises_1.realpath)(file);
|
|
32
|
+
const m = await (_a = path, Promise.resolve().then(() => __importStar(require(_a))));
|
|
33
|
+
const { duration = 5, sweepWidth = 10, samples = 100, warmUpIterations = 100, } = m.options ?? {};
|
|
34
|
+
if (duration <= 0) {
|
|
35
|
+
throw new Error(`${file}: options.duration must be positive`);
|
|
36
|
+
}
|
|
37
|
+
if (sweepWidth <= 1) {
|
|
38
|
+
throw new Error(`${file}: options.sweepWidth must be greater than 0`);
|
|
39
|
+
}
|
|
40
|
+
if (samples <= 0) {
|
|
41
|
+
throw new Error(`${file}: options.samples must be positive`);
|
|
42
|
+
}
|
|
43
|
+
if (samples / sweepWidth < 2) {
|
|
44
|
+
throw new Error(`${file}: options.samples must be greater than 2 * sweepWidth`);
|
|
45
|
+
}
|
|
46
|
+
if (warmUpIterations <= 0) {
|
|
47
|
+
throw new Error(`${file}: options.warmUpIterations must be positive`);
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
name: m.name ?? file,
|
|
51
|
+
options: {
|
|
52
|
+
duration,
|
|
53
|
+
sweepWidth,
|
|
54
|
+
samples,
|
|
55
|
+
warmUpIterations,
|
|
56
|
+
},
|
|
57
|
+
default: m.default,
|
|
58
|
+
};
|
|
59
|
+
}));
|
|
60
|
+
for (const m of modules) {
|
|
61
|
+
run(m);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function run(m) {
|
|
65
|
+
const baseIterations = warmUp(m);
|
|
66
|
+
const samples = new Array();
|
|
67
|
+
for (let i = 0; i < m.options.samples; i++) {
|
|
68
|
+
const iterations = baseIterations * (1 + (i % m.options.sweepWidth));
|
|
69
|
+
const duration = measure(m, iterations);
|
|
70
|
+
samples.push({ duration, iterations });
|
|
71
|
+
}
|
|
72
|
+
const { beta, c95, outliers } = regress(m, samples);
|
|
73
|
+
const ops = 1 / beta;
|
|
74
|
+
const lowOps = 1 / (beta + c95);
|
|
75
|
+
const highOps = 1 / (beta - c95);
|
|
76
|
+
const maxError = Math.max(highOps - ops, ops - lowOps);
|
|
77
|
+
const usedSamples = samples.length - outliers;
|
|
78
|
+
console.log(`${m.name}: ${ops.toFixed(1)} ops/s ` +
|
|
79
|
+
`(±${maxError.toFixed(1)}, p=0.05, n=${usedSamples})`);
|
|
80
|
+
}
|
|
81
|
+
function warmUp(m) {
|
|
82
|
+
// Initial warm-up
|
|
83
|
+
for (let i = 0; i < m.options.warmUpIterations; i++) {
|
|
84
|
+
m.default();
|
|
85
|
+
}
|
|
86
|
+
// Compute max duration per base sample
|
|
87
|
+
let sampleMultiplier = 0;
|
|
88
|
+
for (let i = 0; i < m.options.samples; i++) {
|
|
89
|
+
sampleMultiplier += 1 + (i % m.options.sweepWidth);
|
|
90
|
+
}
|
|
91
|
+
const maxSampleDuration = m.options.duration / sampleMultiplier;
|
|
92
|
+
// Compute iteration count per base sample
|
|
93
|
+
let iterations = 1;
|
|
94
|
+
let duration = 0;
|
|
95
|
+
do {
|
|
96
|
+
iterations *= 1.25;
|
|
97
|
+
duration = measure(m, Math.round(iterations));
|
|
98
|
+
} while (duration < maxSampleDuration);
|
|
99
|
+
iterations = Math.round(iterations / 1.25);
|
|
100
|
+
return iterations;
|
|
101
|
+
}
|
|
102
|
+
function measure(m, iterations) {
|
|
103
|
+
let sum = 0;
|
|
104
|
+
const start = process.hrtime.bigint();
|
|
105
|
+
for (let i = 0; i < iterations; i++) {
|
|
106
|
+
sum += m.default();
|
|
107
|
+
}
|
|
108
|
+
const duration = Number(process.hrtime.bigint() - start) / 1e9;
|
|
109
|
+
if (isNaN(sum)) {
|
|
110
|
+
throw new Error(`${m.name}: runner function did not return number`);
|
|
111
|
+
}
|
|
112
|
+
return duration;
|
|
113
|
+
}
|
|
114
|
+
function regress(m, samples) {
|
|
115
|
+
// Bin the data by iteration count
|
|
116
|
+
const bins = new Map();
|
|
117
|
+
for (const { iterations, duration } of samples) {
|
|
118
|
+
let bin = bins.get(iterations);
|
|
119
|
+
if (bin === undefined) {
|
|
120
|
+
bin = [];
|
|
121
|
+
bins.set(iterations, bin);
|
|
122
|
+
}
|
|
123
|
+
bin.push(duration);
|
|
124
|
+
}
|
|
125
|
+
// Within each iteration bin get rid of the outliers.
|
|
126
|
+
const withoutOutliers = new Array();
|
|
127
|
+
for (const [iterations, durations] of bins) {
|
|
128
|
+
durations.sort();
|
|
129
|
+
const p25 = durations[Math.floor(durations.length * 0.25)] ?? -Infinity;
|
|
130
|
+
const p75 = durations[Math.ceil(durations.length * 0.75)] ?? +Infinity;
|
|
131
|
+
const iqr = p75 - p25;
|
|
132
|
+
const outlierLow = p25 - iqr * 1.5;
|
|
133
|
+
const outlierHigh = p75 + iqr * 1.5;
|
|
134
|
+
// Tukey's method
|
|
135
|
+
const filtered = durations.filter((d) => d >= outlierLow && d <= outlierHigh);
|
|
136
|
+
for (const duration of filtered) {
|
|
137
|
+
withoutOutliers.push({ iterations, duration });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (withoutOutliers.length < 2) {
|
|
141
|
+
throw new Error(`${m.name}: low sample count`);
|
|
142
|
+
}
|
|
143
|
+
let meanDuration = 0;
|
|
144
|
+
let meanIterations = 0;
|
|
145
|
+
for (const { duration, iterations } of withoutOutliers) {
|
|
146
|
+
meanDuration += duration;
|
|
147
|
+
meanIterations += iterations;
|
|
148
|
+
}
|
|
149
|
+
meanDuration /= withoutOutliers.length;
|
|
150
|
+
meanIterations /= withoutOutliers.length;
|
|
151
|
+
let betaNum = 0;
|
|
152
|
+
let betaDenom = 0;
|
|
153
|
+
for (const { duration, iterations } of withoutOutliers) {
|
|
154
|
+
betaNum += (duration - meanDuration) * (iterations - meanIterations);
|
|
155
|
+
betaDenom += (iterations - meanIterations) ** 2;
|
|
156
|
+
}
|
|
157
|
+
// Slope
|
|
158
|
+
const beta = betaNum / betaDenom;
|
|
159
|
+
// Intercept
|
|
160
|
+
const alpha = meanDuration - beta * meanIterations;
|
|
161
|
+
let stdError = 0;
|
|
162
|
+
for (const { duration, iterations } of withoutOutliers) {
|
|
163
|
+
stdError += (duration - alpha - beta * iterations) ** 2;
|
|
164
|
+
}
|
|
165
|
+
stdError /= withoutOutliers.length - 2;
|
|
166
|
+
stdError /= betaDenom;
|
|
167
|
+
stdError = Math.sqrt(stdError);
|
|
168
|
+
// t-distribution value for large sample count and p=0.05
|
|
169
|
+
const T_VALUE = 1.9719;
|
|
170
|
+
return {
|
|
171
|
+
alpha,
|
|
172
|
+
beta,
|
|
173
|
+
c95: T_VALUE * stdError,
|
|
174
|
+
outliers: samples.length - withoutOutliers.length,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
main().catch((err) => {
|
|
178
|
+
console.error(err);
|
|
179
|
+
process.exit(1);
|
|
180
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@indutny/bencher",
|
|
3
|
+
"version": "1.0.0-rc.1",
|
|
4
|
+
"description": "Simple benchmarking tool",
|
|
5
|
+
"bin": {
|
|
6
|
+
"bencher": "./dist/bin/bencher.js"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/bin/*.js",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"lint": "eslint --cache .",
|
|
15
|
+
"format": "prettier --cache --write .",
|
|
16
|
+
"check:format": "prettier --cache --check .",
|
|
17
|
+
"prepublish": "rm -rf dist && npm run build"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"benchmark",
|
|
21
|
+
"sweep",
|
|
22
|
+
"regression"
|
|
23
|
+
],
|
|
24
|
+
"author": "Fedor Indutny <238531+indutny@users.noreply.github.com>",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/indutny/bencher.git"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/indutny/bencher/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/indutny/bencher#readme",
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "^5.47.0",
|
|
36
|
+
"@typescript-eslint/parser": "^5.47.0",
|
|
37
|
+
"eslint": "^8.30.0",
|
|
38
|
+
"prettier": "^2.8.1",
|
|
39
|
+
"ts-node": "^10.9.1",
|
|
40
|
+
"typescript": "^4.9.4"
|
|
41
|
+
}
|
|
42
|
+
}
|