Skip to content

Commit

Permalink
Improve benchmarks accuracy (#148)
Browse files Browse the repository at this point in the history
* Improve benchmarks accuracy

* Fix test

* Remove plugins from package.json

* Revert parts of benchamrk

* Add best size / best speed

* Add an option to simplify

* Fix linter

* Remove multiPass

* Remove state arg
  • Loading branch information
kangax authored and boopathi committed Nov 27, 2016
1 parent f2178c8 commit ffca8e6
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions scripts/benchmark.js
Expand Up @@ -17,6 +17,8 @@ const compile = require("google-closure-compiler-js").compile;

let packagename, filename;

const NUM_TEST_RUNS = 3;

const script = new Command("benchmark.js")
.option("-o, --offline", "Only install package if not present; package not removed after testing")
.usage("[options] <package> [file]")
Expand All @@ -35,7 +37,7 @@ if (!packagename) {
const pathToScripts = __dirname;

const table = new Table({
head: ["", "raw", "raw win", "gzip", "gzip win", "parse time", "run"],
head: ["", "raw", "raw win", "gzip", "gzip win", "parse time", "run time (average)"],
chars: {
top: "",
"top-mid": "" ,
Expand Down Expand Up @@ -100,6 +102,7 @@ function checkFile() {
}

function test(name, callback) {

console.log("testing", name);

const start = Date.now();
Expand All @@ -116,20 +119,39 @@ function test(name, callback) {
const parseEnd = Date.now();
const parseNow = parseEnd - parseStart;

const runTimes = [run];

for (let i = 1; i < NUM_TEST_RUNS; i++) {
const start = Date.now();
callback(code);
runTimes.push(Date.now() - start);
}

const totalTime = runTimes.reduce((a, b) => a + b, 0);
const average = parseInt(totalTime / runTimes.length, 10);

results.push({
name: name,
raw: result.length,
gzip: gzipped.length,
parse: parseNow,
run: run,
run: average,
});
}

function testFile() {
code = fs.readFileSync(filename, "utf8");
gzippedCode = zlib.gzipSync(code);

test("babili", function (code) {
test("babili (best speed)", function (code) {
return babel.transform(code, {
sourceType: "script",
presets: [require("../packages/babel-preset-babili")],
comments: false,
}).code;
});

test("babili (best size)", function (code) {
return babel.transform(code, {
sourceType: "script",
presets: [require("../packages/babel-preset-babili")],
Expand Down Expand Up @@ -161,9 +183,7 @@ function testFile() {
}

function processResults() {
results = results.sort(function (a, b) {
return a.gzip > b.gzip;
});
results = results.sort((a, b) => a.gzip > b.gzip);

results.forEach(function (result, i) {
let row = [
Expand Down

0 comments on commit ffca8e6

Please sign in to comment.