Convert benchmark files to modules

redux
Arlo Breault 9 years ago
parent 9815e49477
commit 7a94f97b46

@ -1,38 +1,48 @@
benchmarks = [ (function(root, factory) {
{ if (typeof module !== 'undefined' && module.exports) {
id: "json", module.exports = factory();
title: "JSON", } else {
tests: [ root.benchmarks = factory();
{ file: "example1.json", title: "Example 1" },
{ file: "example2.json", title: "Example 2" },
{ file: "example3.json", title: "Example 3" },
{ file: "example4.json", title: "Example 4" },
{ file: "example5.json", title: "Example 5" }
]
},
{
id: "css",
title: "CSS",
tests: [
{ file: "blueprint/src/reset.css", title: "Blueprint - reset.css (source)" },
{ file: "blueprint/src/typography.css", title: "Blueprint - typography.css (source)" },
{ file: "blueprint/src/forms.css", title: "Blueprint - forms.css (source)" },
{ file: "blueprint/src/grid.css", title: "Blueprint - grid.css (source)" },
{ file: "blueprint/src/print.css", title: "Blueprint - print.css (source)" },
// Contains syntax errors.
// { file: "blueprint/src/ie.css", title: "Blueprint - ie.css (source)" },
{ file: "blueprint/min/screen.css", title: "Blueprint - screen.css (minified)" },
{ file: "blueprint/min/print.css", title: "Blueprint - print.css (minified)" },
// Contains syntax errors.
// { file: "blueprint/min/ie.css", title: "Blueprint - ie.css (minified)" },
{ file: "960.gs/src/reset.css", title: "960.gs - reset.css (source)" },
{ file: "960.gs/src/text.css", title: "960.gs - text.css (source)" },
{ file: "960.gs/src/960.css", title: "960.gs - 960.css (source)" },
{ file: "960.gs/src/960_24_col.css", title: "960.gs - 960_24_col.css (source)" },
{ file: "960.gs/min/reset.css", title: "960.gs - reset.css (minified)" },
{ file: "960.gs/min/text.css", title: "960.gs - text.css (minified)" },
{ file: "960.gs/min/960.css", title: "960.gs - 960.css (minified)" },
{ file: "960.gs/min/960_24_col.css", title: "960.gs - 960_24_col.css (minified)" }
]
} }
]; }(this, function() {
return [
{
id: "json",
title: "JSON",
tests: [
{ file: "example1.json", title: "Example 1" },
{ file: "example2.json", title: "Example 2" },
{ file: "example3.json", title: "Example 3" },
{ file: "example4.json", title: "Example 4" },
{ file: "example5.json", title: "Example 5" }
]
},
{
id: "css",
title: "CSS",
tests: [
{ file: "blueprint/src/reset.css", title: "Blueprint - reset.css (source)" },
{ file: "blueprint/src/typography.css", title: "Blueprint - typography.css (source)" },
{ file: "blueprint/src/forms.css", title: "Blueprint - forms.css (source)" },
{ file: "blueprint/src/grid.css", title: "Blueprint - grid.css (source)" },
{ file: "blueprint/src/print.css", title: "Blueprint - print.css (source)" },
// Contains syntax errors.
// { file: "blueprint/src/ie.css", title: "Blueprint - ie.css (source)" },
{ file: "blueprint/min/screen.css", title: "Blueprint - screen.css (minified)" },
{ file: "blueprint/min/print.css", title: "Blueprint - print.css (minified)" },
// Contains syntax errors.
// { file: "blueprint/min/ie.css", title: "Blueprint - ie.css (minified)" },
{ file: "960.gs/src/reset.css", title: "960.gs - reset.css (source)" },
{ file: "960.gs/src/text.css", title: "960.gs - text.css (source)" },
{ file: "960.gs/src/960.css", title: "960.gs - 960.css (source)" },
{ file: "960.gs/src/960_24_col.css", title: "960.gs - 960_24_col.css (source)" },
{ file: "960.gs/min/reset.css", title: "960.gs - reset.css (minified)" },
{ file: "960.gs/min/text.css", title: "960.gs - text.css (minified)" },
{ file: "960.gs/min/960.css", title: "960.gs - 960.css (minified)" },
{ file: "960.gs/min/960_24_col.css", title: "960.gs - 960_24_col.css (minified)" }
]
}
];
}));

@ -4,12 +4,8 @@ var util = require("util");
var fs = require("fs"); var fs = require("fs");
var PEG = require("../lib/peg"); var PEG = require("../lib/peg");
[ var benchmarks = require("./benchmarks.js");
"benchmarks.js", var Runner = require("./runner.js")(PEG);
"runner.js",
].forEach(function(file) {
eval(fs.readFileSync(__dirname + "/" + file, "utf8"));
});
/* Results Table Manipulation */ /* Results Table Manipulation */

@ -1,119 +1,129 @@
Runner = { (function(root, factory) {
run: function(benchmarks, runCount, options, callbacks) { if (typeof module !== 'undefined' && module.exports) {
module.exports = factory;
} else {
root.Runner = factory(root.PEG);
}
}(this, function(PEG) {
return {
run: function(benchmarks, runCount, options, callbacks) {
/* Queue */ /* Queue */
var Q = { var Q = {
functions: [], functions: [],
add: function(f) { add: function(f) {
this.functions.push(f); this.functions.push(f);
}, },
run: function() { run: function() {
if (this.functions.length > 0) { if (this.functions.length > 0) {
this.functions.shift()(); this.functions.shift()();
/* /*
* We can't use |arguments.callee| here because |this| would get * We can't use |arguments.callee| here because |this| would get
* messed-up in that case. * messed-up in that case.
*/ */
setTimeout(function() { Q.run(); }, 0); setTimeout(function() { Q.run(); }, 0);
}
} }
} };
};
/*
* The benchmark itself is factored out into several functions (some of them
* generated), which are enqueued and run one by one using |setTimeout|. We
* do this for two reasons:
*
* 1. To avoid bowser mechanism for interrupting long-running scripts to
* kick-in (or at least to not kick-in that often).
*
* 2. To ensure progressive rendering of results in the browser (some
* browsers do not render at all when running JavaScript code).
*
* The enqueued functions share state, which is all stored in the properties
* of the |state| object.
*/
var state = {}, i, j;
function initialize() {
callbacks.start();
state.totalInputSize = 0;
state.totalParseTime = 0;
}
/* function benchmarkInitializer(i) {
* The benchmark itself is factored out into several functions (some of them return function() {
* generated), which are enqueued and run one by one using |setTimeout|. We callbacks.benchmarkStart(benchmarks[i]);
* do this for two reasons:
*
* 1. To avoid bowser mechanism for interrupting long-running scripts to
* kick-in (or at least to not kick-in that often).
*
* 2. To ensure progressive rendering of results in the browser (some
* browsers do not render at all when running JavaScript code).
*
* The enqueued functions share state, which is all stored in the properties
* of the |state| object.
*/
var state = {}, i, j;
function initialize() {
callbacks.start();
state.totalInputSize = 0;
state.totalParseTime = 0;
}
function benchmarkInitializer(i) { state.parser = PEG.buildParser(
return function() { callbacks.readFile("../examples/" + benchmarks[i].id + ".pegjs"),
callbacks.benchmarkStart(benchmarks[i]); options
);
state.parser = PEG.buildParser( state.benchmarkInputSize = 0;
callbacks.readFile("../examples/" + benchmarks[i].id + ".pegjs"), state.benchmarkParseTime = 0;
options };
); }
state.benchmarkInputSize = 0;
state.benchmarkParseTime = 0;
};
}
function testRunner(i, j) { function testRunner(i, j) {
return function() { return function() {
var benchmark = benchmarks[i], var benchmark = benchmarks[i],
test = benchmark.tests[j], test = benchmark.tests[j],
input, parseTime, averageParseTime, k, t; input, parseTime, averageParseTime, k, t;
callbacks.testStart(benchmark, test); callbacks.testStart(benchmark, test);
input = callbacks.readFile(benchmark.id + "/" + test.file); input = callbacks.readFile(benchmark.id + "/" + test.file);
parseTime = 0; parseTime = 0;
for (k = 0; k < runCount; k++) { for (k = 0; k < runCount; k++) {
t = (new Date()).getTime(); t = (new Date()).getTime();
state.parser.parse(input); state.parser.parse(input);
parseTime += (new Date()).getTime() - t; parseTime += (new Date()).getTime() - t;
} }
averageParseTime = parseTime / runCount; averageParseTime = parseTime / runCount;
callbacks.testFinish(benchmark, test, input.length, averageParseTime); callbacks.testFinish(benchmark, test, input.length, averageParseTime);
state.benchmarkInputSize += input.length; state.benchmarkInputSize += input.length;
state.benchmarkParseTime += averageParseTime; state.benchmarkParseTime += averageParseTime;
}; };
} }
function benchmarkFinalizer(i) { function benchmarkFinalizer(i) {
return function() { return function() {
callbacks.benchmarkFinish( callbacks.benchmarkFinish(
benchmarks[i], benchmarks[i],
state.benchmarkInputSize, state.benchmarkInputSize,
state.benchmarkParseTime state.benchmarkParseTime
); );
state.totalInputSize += state.benchmarkInputSize;
state.totalParseTime += state.benchmarkParseTime;
};
}
function finalize() { state.totalInputSize += state.benchmarkInputSize;
callbacks.finish(state.totalInputSize, state.totalParseTime); state.totalParseTime += state.benchmarkParseTime;
} };
}
/* Main */ function finalize() {
callbacks.finish(state.totalInputSize, state.totalParseTime);
}
/* Main */
Q.add(initialize); Q.add(initialize);
for (i = 0; i < benchmarks.length; i++) { for (i = 0; i < benchmarks.length; i++) {
Q.add(benchmarkInitializer(i)); Q.add(benchmarkInitializer(i));
for (j = 0; j < benchmarks[i].tests.length; j++) { for (j = 0; j < benchmarks[i].tests.length; j++) {
Q.add(testRunner(i, j)); Q.add(testRunner(i, j));
}
Q.add(benchmarkFinalizer(i));
} }
Q.add(benchmarkFinalizer(i)); Q.add(finalize);
Q.run();
} }
Q.add(finalize); };
Q.run(); }));
}
};

Loading…
Cancel
Save