@indutny/bencher 1.1.0 → 1.1.2

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 (2) hide show
  1. package/dist/bin/bencher.js +31 -11
  2. package/package.json +1 -1
@@ -30,6 +30,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
30
30
  const promises_1 = require("fs/promises");
31
31
  const yargs_1 = __importDefault(require("yargs"));
32
32
  const helpers_1 = require("yargs/helpers");
33
+ // ANSI colors
34
+ const BOLD = 1;
35
+ const ITALIC = 3;
36
+ // Go back to previous line, clear the line
37
+ const PREV_LINE = '\x1b[F\x1b[K';
38
+ // t-distribution value for large sample count and p=0.001
39
+ // https://www.itl.nist.gov/div898/handbook/eda/section3/eda3672.htm
40
+ const STUDENT_T = 3.09;
41
+ const P_VALUE = 0.001;
33
42
  async function main() {
34
43
  const argv = await (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
35
44
  .alias('h', 'help')
@@ -87,8 +96,15 @@ async function main() {
87
96
  default: m.default,
88
97
  };
89
98
  }));
99
+ const maxNameLength = modules.reduce((len, { name }) => Math.max(len, name.length), 0);
90
100
  for (const m of modules) {
91
- run(m);
101
+ const paddedName = m.name + ' '.repeat(maxNameLength - m.name.length);
102
+ process.stdout.write(`${style(paddedName, BOLD)}: running...\n`);
103
+ const { ops, maxError, usedSamples } = run(m);
104
+ process.stdout.write(PREV_LINE);
105
+ process.stdout.write(`${style(paddedName, BOLD)}: ${ops.toFixed(1)} ops/s ` +
106
+ style(`(±${maxError.toFixed(1)}, p=${P_VALUE}, n=${usedSamples})`, ITALIC) +
107
+ '\n');
92
108
  }
93
109
  }
94
110
  function run(m) {
@@ -99,14 +115,17 @@ function run(m) {
99
115
  const duration = measure(m, iterations);
100
116
  samples.push({ duration, iterations });
101
117
  }
102
- const { beta, c95, outliers } = regress(m, samples);
118
+ const { beta, confidence, outliers } = regress(m, samples);
103
119
  const ops = 1 / beta;
104
- const lowOps = 1 / (beta + c95);
105
- const highOps = 1 / (beta - c95);
120
+ const lowOps = 1 / (beta + confidence);
121
+ const highOps = 1 / (beta - confidence);
106
122
  const maxError = Math.max(highOps - ops, ops - lowOps);
107
123
  const usedSamples = samples.length - outliers;
108
- console.log(`${m.name}: ${ops.toFixed(1)} ops/s ` +
109
- `(±${maxError.toFixed(1)}, p=0.05, n=${usedSamples})`);
124
+ return {
125
+ ops,
126
+ maxError,
127
+ usedSamples,
128
+ };
110
129
  }
111
130
  function warmUp(m) {
112
131
  // Initial warm-up
@@ -123,10 +142,10 @@ function warmUp(m) {
123
142
  let iterations = 1;
124
143
  let duration = 0;
125
144
  do {
126
- iterations *= 1.25;
145
+ iterations *= 2;
127
146
  duration = measure(m, Math.round(iterations));
128
147
  } while (duration < maxSampleDuration);
129
- iterations = Math.round(iterations / 1.25);
148
+ iterations = Math.max(iterations / 2, (maxSampleDuration / duration) * iterations);
130
149
  return iterations;
131
150
  }
132
151
  function measure(m, iterations) {
@@ -195,15 +214,16 @@ function regress(m, samples) {
195
214
  stdError /= withoutOutliers.length - 2;
196
215
  stdError /= betaDenom;
197
216
  stdError = Math.sqrt(stdError);
198
- // t-distribution value for large sample count and p=0.05
199
- const T_VALUE = 1.9719;
200
217
  return {
201
218
  alpha,
202
219
  beta,
203
- c95: T_VALUE * stdError,
220
+ confidence: STUDENT_T * stdError,
204
221
  outliers: samples.length - withoutOutliers.length,
205
222
  };
206
223
  }
224
+ function style(text, code) {
225
+ return `\x1b[${code}m${text}\x1b[m`;
226
+ }
207
227
  main().catch((err) => {
208
228
  console.error(err);
209
229
  process.exit(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@indutny/bencher",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Simple benchmarking tool",
5
5
  "bin": {
6
6
  "bencher": "./dist/bin/bencher.js"