Skip to content

Commit

Permalink
Inline 'beautify-benchmark' + heavy refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Feb 19, 2019
1 parent f205c99 commit bdee052
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"@babel/preset-env": "7.3.1",
"@babel/register": "7.0.0",
"babel-eslint": "10.0.1",
"beautify-benchmark": "0.2.4",
"benchmark": "2.1.4",
"chai": "4.2.0",
"eslint": "5.14.1",
Expand Down
73 changes: 68 additions & 5 deletions resources/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
'use strict';

const { Suite } = require('benchmark');
const beautifyBenchmark = require('beautify-benchmark');
const { execSync } = require('child_process');
const os = require('os');
const fs = require('fs');
Expand Down Expand Up @@ -83,25 +82,89 @@ function runBenchmark(benchmark, environments) {
const modules = environments.map(({ distPath }) =>
require(path.join(distPath, benchmark))
);
const benchResults = []

const suite = new Suite(modules[0].name, {
onStart(event) {
console.log('⏱️ ' + event.currentTarget.name);
beautifyBenchmark.reset();
},
onCycle(event) {
beautifyBenchmark.add(event.target);
onCycle({ target }) {
benchResults.push(target);
process.stdout.write(
' ' + cyan(benchResults.length) + ' tests completed.\u000D',
);
},
onError(event) {
console.error(event.target.error);
},
onComplete() {
beautifyBenchmark.log();
console.log('\n');
beautifyBenchmark(benchResults);
},
});
for (let i = 0; i < environments.length; i++) {
suite.add(environments[i].revision, modules[i].measure);
}
suite.run({ async: false });
console.log('');
}

function beautifyBenchmark(results) {
const benches = results.map(result => ({
name: result.name,
error: result.error,
ops: result.hz,
deviation: result.stats.rme,
numRuns: result.stats.sample.length,
}));

const nameMaxLen = maxBy(benches, ({ name }) => name.length);
const opsTop = maxBy(benches, ({ ops }) => ops);
const opsMaxLen = maxBy(benches, ({ ops }) => beautifyNumber(ops).length);

for (const bench of benches) {
if (bench.error) {
console.log(' ' + bench.name + ': ' + red(String(bench.error)));
continue;
}

const { name, ops, deviation, numRuns } = bench;
console.log(
' ' + nameStr() + grey(' x ') + opsStr() + ' ops/sec ' +
grey('\xb1') + deviationStr() + cyan('%') +
grey(' (' + numRuns + ' runs sampled)')
);

function nameStr() {
const nameFmt = name.padEnd(nameMaxLen);
return (ops === opsTop) ? green(nameFmt) : nameFmt;
}

function opsStr() {
const percent = ops / opsTop;
const colorFn = percent > 0.95 ? green : (percent > 0.80 ? yellow : red);
return colorFn(beautifyNumber(ops).padStart(opsMaxLen));
}

function deviationStr() {
const colorFn = deviation > 5 ? red : (deviation > 2 ? yellow : green);
return colorFn(deviation.toFixed(2));
}
}
}

function red(str) { return '\u001b[31m' + str + '\u001b[0m' }
function green(str) { return '\u001b[32m' + str + '\u001b[0m' }
function yellow(str) { return '\u001b[33m' + str + '\u001b[0m' }
function cyan(str) { return '\u001b[36m' + str + '\u001b[0m' }
function grey(str) { return '\u001b[90m' + str + '\u001b[0m' }

function beautifyNumber(num) {
return Number(num.toFixed(num > 100 ? 0 : 2)).toLocaleString();
}

function maxBy(array, fn) {
return Math.max(...array.map(fn));
}

// Prepare all revisions and run benchmarks matching a pattern against them.
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -829,11 +829,6 @@ base@^0.11.1:
mixin-deep "^1.2.0"
pascalcase "^0.1.1"

[email protected]:
version "0.2.4"
resolved "https://registry.yarnpkg.com/beautify-benchmark/-/beautify-benchmark-0.2.4.tgz#3151def14c1a2e0d07ff2e476861c7ed0e1ae39b"
integrity sha1-MVHe8UwaLg0H/y5HaGHH7Q4a45s=

[email protected]:
version "2.1.4"
resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-2.1.4.tgz#09f3de31c916425d498cc2ee565a0ebf3c2a5629"
Expand Down

0 comments on commit bdee052

Please sign in to comment.