| 1 |
|
| 2 |
|
| 3 |
"use strict"; |
| 4 |
|
| 5 |
(function(root, factory) { |
| 6 |
if (typeof module !== 'undefined' && module.exports) { |
| 7 |
module.exports = factory; |
| 8 |
} else { |
| 9 |
root.Runner = factory(root.peg); |
| 10 |
} |
| 11 |
}(this, function(peg) { |
| 12 |
|
| 13 |
return { |
| 14 |
run: function(benchmarks, runCount, options, callbacks) { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
var Q = { |
| 19 |
functions: [], |
| 20 |
|
| 21 |
add: function(f) { |
| 22 |
this.functions.push(f); |
| 23 |
}, |
| 24 |
|
| 25 |
run: function() { |
| 26 |
if (this.functions.length > 0) { |
| 27 |
this.functions.shift()(); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
setTimeout(function() { Q.run(); }, 0); |
| 34 |
} |
| 35 |
} |
| 36 |
}; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
var state = {}, i, j; |
| 54 |
|
| 55 |
function initialize() { |
| 56 |
callbacks.start(); |
| 57 |
|
| 58 |
state.totalInputSize = 0; |
| 59 |
state.totalParseTime = 0; |
| 60 |
} |
| 61 |
|
| 62 |
function benchmarkInitializer(i) { |
| 63 |
return function() { |
| 64 |
callbacks.benchmarkStart(benchmarks[i]); |
| 65 |
|
| 66 |
state.parser = peg.generate( |
| 67 |
callbacks.readFile("../examples/" + benchmarks[i].id + ".pegjs"), |
| 68 |
options |
| 69 |
); |
| 70 |
state.benchmarkInputSize = 0; |
| 71 |
state.benchmarkParseTime = 0; |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
function testRunner(i, j) { |
| 76 |
return function() { |
| 77 |
var benchmark = benchmarks[i], |
| 78 |
test = benchmark.tests[j], |
| 79 |
input, parseTime, averageParseTime, k, t; |
| 80 |
|
| 81 |
callbacks.testStart(benchmark, test); |
| 82 |
|
| 83 |
input = callbacks.readFile(benchmark.id + "/" + test.file); |
| 84 |
|
| 85 |
parseTime = 0; |
| 86 |
for (k = 0; k < runCount; k++) { |
| 87 |
t = (new Date()).getTime(); |
| 88 |
state.parser.parse(input); |
| 89 |
parseTime += (new Date()).getTime() - t; |
| 90 |
} |
| 91 |
averageParseTime = parseTime / runCount; |
| 92 |
|
| 93 |
callbacks.testFinish(benchmark, test, input.length, averageParseTime); |
| 94 |
|
| 95 |
state.benchmarkInputSize += input.length; |
| 96 |
state.benchmarkParseTime += averageParseTime; |
| 97 |
}; |
| 98 |
} |
| 99 |
|
| 100 |
function benchmarkFinalizer(i) { |
| 101 |
return function() { |
| 102 |
callbacks.benchmarkFinish( |
| 103 |
benchmarks[i], |
| 104 |
state.benchmarkInputSize, |
| 105 |
state.benchmarkParseTime |
| 106 |
); |
| 107 |
|
| 108 |
state.totalInputSize += state.benchmarkInputSize; |
| 109 |
state.totalParseTime += state.benchmarkParseTime; |
| 110 |
}; |
| 111 |
} |
| 112 |
|
| 113 |
function finalize() { |
| 114 |
callbacks.finish(state.totalInputSize, state.totalParseTime); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
Q.add(initialize); |
| 120 |
for (i = 0; i < benchmarks.length; i++) { |
| 121 |
Q.add(benchmarkInitializer(i)); |
| 122 |
for (j = 0; j < benchmarks[i].tests.length; j++) { |
| 123 |
Q.add(testRunner(i, j)); |
| 124 |
} |
| 125 |
Q.add(benchmarkFinalizer(i)); |
| 126 |
} |
| 127 |
Q.add(finalize); |
| 128 |
|
| 129 |
Q.run(); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
})); |