Skip to content

Commit 7786a25

Browse files
Inline 'beautify-benchmark' + heavy refactoring (#1748)
Also fixes Fishrock123/beautify-benchmark#3
1 parent f205c99 commit 7786a25

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"@babel/preset-env": "7.3.1",
5656
"@babel/register": "7.0.0",
5757
"babel-eslint": "10.0.1",
58-
"beautify-benchmark": "0.2.4",
5958
"benchmark": "2.1.4",
6059
"chai": "4.2.0",
6160
"eslint": "5.14.1",

resources/benchmark.js

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
'use strict';
1111

1212
const { Suite } = require('benchmark');
13-
const beautifyBenchmark = require('beautify-benchmark');
1413
const { execSync } = require('child_process');
1514
const os = require('os');
1615
const fs = require('fs');
@@ -83,25 +82,89 @@ function runBenchmark(benchmark, environments) {
8382
const modules = environments.map(({ distPath }) =>
8483
require(path.join(distPath, benchmark))
8584
);
85+
const benchResults = []
86+
8687
const suite = new Suite(modules[0].name, {
8788
onStart(event) {
8889
console.log('⏱️ ' + event.currentTarget.name);
89-
beautifyBenchmark.reset();
9090
},
91-
onCycle(event) {
92-
beautifyBenchmark.add(event.target);
91+
onCycle({ target }) {
92+
benchResults.push(target);
93+
process.stdout.write(
94+
' ' + cyan(benchResults.length) + ' tests completed.\u000D',
95+
);
9396
},
9497
onError(event) {
9598
console.error(event.target.error);
9699
},
97100
onComplete() {
98-
beautifyBenchmark.log();
101+
console.log('\n');
102+
beautifyBenchmark(benchResults);
99103
},
100104
});
101105
for (let i = 0; i < environments.length; i++) {
102106
suite.add(environments[i].revision, modules[i].measure);
103107
}
104108
suite.run({ async: false });
109+
console.log('');
110+
}
111+
112+
function beautifyBenchmark(results) {
113+
const benches = results.map(result => ({
114+
name: result.name,
115+
error: result.error,
116+
ops: result.hz,
117+
deviation: result.stats.rme,
118+
numRuns: result.stats.sample.length,
119+
}));
120+
121+
const nameMaxLen = maxBy(benches, ({ name }) => name.length);
122+
const opsTop = maxBy(benches, ({ ops }) => ops);
123+
const opsMaxLen = maxBy(benches, ({ ops }) => beautifyNumber(ops).length);
124+
125+
for (const bench of benches) {
126+
if (bench.error) {
127+
console.log(' ' + bench.name + ': ' + red(String(bench.error)));
128+
continue;
129+
}
130+
131+
const { name, ops, deviation, numRuns } = bench;
132+
console.log(
133+
' ' + nameStr() + grey(' x ') + opsStr() + ' ops/sec ' +
134+
grey('\xb1') + deviationStr() + cyan('%') +
135+
grey(' (' + numRuns + ' runs sampled)')
136+
);
137+
138+
function nameStr() {
139+
const nameFmt = name.padEnd(nameMaxLen);
140+
return (ops === opsTop) ? green(nameFmt) : nameFmt;
141+
}
142+
143+
function opsStr() {
144+
const percent = ops / opsTop;
145+
const colorFn = percent > 0.95 ? green : (percent > 0.80 ? yellow : red);
146+
return colorFn(beautifyNumber(ops).padStart(opsMaxLen));
147+
}
148+
149+
function deviationStr() {
150+
const colorFn = deviation > 5 ? red : (deviation > 2 ? yellow : green);
151+
return colorFn(deviation.toFixed(2));
152+
}
153+
}
154+
}
155+
156+
function red(str) { return '\u001b[31m' + str + '\u001b[0m' }
157+
function green(str) { return '\u001b[32m' + str + '\u001b[0m' }
158+
function yellow(str) { return '\u001b[33m' + str + '\u001b[0m' }
159+
function cyan(str) { return '\u001b[36m' + str + '\u001b[0m' }
160+
function grey(str) { return '\u001b[90m' + str + '\u001b[0m' }
161+
162+
function beautifyNumber(num) {
163+
return Number(num.toFixed(num > 100 ? 0 : 2)).toLocaleString();
164+
}
165+
166+
function maxBy(array, fn) {
167+
return Math.max(...array.map(fn));
105168
}
106169

107170
// Prepare all revisions and run benchmarks matching a pattern against them.

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -829,11 +829,6 @@ base@^0.11.1:
829829
mixin-deep "^1.2.0"
830830
pascalcase "^0.1.1"
831831

832-
833-
version "0.2.4"
834-
resolved "https://registry.yarnpkg.com/beautify-benchmark/-/beautify-benchmark-0.2.4.tgz#3151def14c1a2e0d07ff2e476861c7ed0e1ae39b"
835-
integrity sha1-MVHe8UwaLg0H/y5HaGHH7Q4a45s=
836-
837832
838833
version "2.1.4"
839834
resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-2.1.4.tgz#09f3de31c916425d498cc2ee565a0ebf3c2a5629"

0 commit comments

Comments
 (0)