Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve benchmarks accuracy #148

Merged
merged 10 commits into from Nov 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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