Debian Codegraph @

benchmark/runner.js

raw symbols SCIP
1 /* global module, setTimeout */
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 /* Queue */
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 * We can't use |arguments.callee| here because |this| would get
31 * messed-up in that case.
32 */
33 setTimeout(function() { Q.run(); }, 0);
34 }
35 }
36 };
37
38 /*
39 * The benchmark itself is factored out into several functions (some of them
40 * generated), which are enqueued and run one by one using |setTimeout|. We
41 * do this for two reasons:
42 *
43 * 1. To avoid bowser mechanism for interrupting long-running scripts to
44 * kick-in (or at least to not kick-in that often).
45 *
46 * 2. To ensure progressive rendering of results in the browser (some
47 * browsers do not render at all when running JavaScript code).
48 *
49 * The enqueued functions share state, which is all stored in the properties
50 * of the |state| object.
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 /* Main */
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 }));