| 1 |
#!/usr/bin/env node |
| 2 |
|
| 3 |
/* eslint-env node */ |
| 4 |
/* eslint no-console: 0*/ |
| 5 |
|
| 6 |
"use strict"; |
| 7 |
|
| 8 |
var fs = require("fs"); |
| 9 |
var peg = require("../lib/peg"); |
| 10 |
|
| 11 |
var benchmarks = require("./benchmarks.js"); |
| 12 |
var Runner = require("./runner.js")(peg); |
| 13 |
|
| 14 |
/* Results Table Manipulation */ |
| 15 |
|
| 16 |
function dup(text, count) { |
| 17 |
var result = ""; |
| 18 |
for (var i = 1; i <= count; i++) { |
| 19 |
result += text; |
| 20 |
} |
| 21 |
return result; |
| 22 |
} |
| 23 |
|
| 24 |
function padLeft(text, length) { |
| 25 |
return dup(" ", length - text.length) + text; |
| 26 |
} |
| 27 |
|
| 28 |
function padRight(text, length) { |
| 29 |
return text + dup(" ", length - text.length); |
| 30 |
} |
| 31 |
|
| 32 |
function center(text, length) { |
| 33 |
var padLength = (length - text.length) / 2; |
| 34 |
return dup(" ", Math.floor(padLength)) |
| 35 |
+ text |
| 36 |
+ dup(" ", Math.ceil(padLength)); |
| 37 |
} |
| 38 |
|
| 39 |
function writeTableHeader() { |
| 40 |
console.log("┌─────────────────────────────────────┬───────────┬────────────┬──────────────┐"); |
| 41 |
console.log("│ Test │ Inp. size │ Avg. time │ Avg. speed │"); |
| 42 |
} |
| 43 |
|
| 44 |
function writeHeading(heading) { |
| 45 |
console.log("├─────────────────────────────────────┴───────────┴────────────┴──────────────┤"); |
| 46 |
console.log("│ " + center(heading, 75) + " │"); |
| 47 |
console.log("├─────────────────────────────────────┬───────────┬────────────┬──────────────┤"); |
| 48 |
} |
| 49 |
|
| 50 |
function writeResult(title, inputSize, parseTime) { |
| 51 |
var KB = 1024; |
| 52 |
var MS_IN_S = 1000; |
| 53 |
|
| 54 |
console.log("│ " |
| 55 |
+ padRight(title, 35) |
| 56 |
+ " │ " |
| 57 |
+ padLeft((inputSize / KB).toFixed(2), 6) |
| 58 |
+ " kB │ " |
| 59 |
+ padLeft(parseTime.toFixed(2), 7) |
| 60 |
+ " ms │ " |
| 61 |
+ padLeft(((inputSize / KB) / (parseTime / MS_IN_S)).toFixed(2), 7) |
| 62 |
+ " kB/s │" |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
function writeSeparator() { |
| 67 |
console.log("├─────────────────────────────────────┼───────────┼────────────┼──────────────┤"); |
| 68 |
} |
| 69 |
|
| 70 |
function writeTableFooter() { |
| 71 |
console.log("└─────────────────────────────────────┴───────────┴────────────┴──────────────┘"); |
| 72 |
} |
| 73 |
|
| 74 |
/* Helpers */ |
| 75 |
|
| 76 |
function printHelp() { |
| 77 |
console.log("Usage: run [options]"); |
| 78 |
console.log(""); |
| 79 |
console.log("Runs PEG.js benchmark suite."); |
| 80 |
console.log(""); |
| 81 |
console.log("Options:"); |
| 82 |
console.log(" -n, --run-count <n> number of runs (default: 10)"); |
| 83 |
console.log(" --cache make tested parsers cache results"); |
| 84 |
console.log(" -o, --optimize <goal> select optimization for speed or size (default:"); |
| 85 |
console.log(" speed)"); |
| 86 |
} |
| 87 |
|
| 88 |
function exitSuccess() { |
| 89 |
process.exit(0); |
| 90 |
} |
| 91 |
|
| 92 |
function exitFailure() { |
| 93 |
process.exit(1); |
| 94 |
} |
| 95 |
|
| 96 |
function abort(message) { |
| 97 |
console.error(message); |
| 98 |
exitFailure(); |
| 99 |
} |
| 100 |
|
| 101 |
/* Arguments */ |
| 102 |
|
| 103 |
var args = process.argv.slice(2); // Trim "node" and the script path. |
| 104 |
|
| 105 |
function isOption(arg) { |
| 106 |
return (/^-/).test(arg); |
| 107 |
} |
| 108 |
|
| 109 |
function nextArg() { |
| 110 |
args.shift(); |
| 111 |
} |
| 112 |
|
| 113 |
/* Main */ |
| 114 |
|
| 115 |
var runCount = 10; |
| 116 |
var options = { |
| 117 |
cache: false, |
| 118 |
optimize: "speed" |
| 119 |
}; |
| 120 |
|
| 121 |
while (args.length > 0 && isOption(args[0])) { |
| 122 |
switch (args[0]) { |
| 123 |
case "-n": |
| 124 |
case "--run-count": |
| 125 |
nextArg(); |
| 126 |
if (args.length === 0) { |
| 127 |
abort("Missing parameter of the -n/--run-count option."); |
| 128 |
} |
| 129 |
runCount = parseInt(args[0], 10); |
| 130 |
if (isNaN(runCount) || runCount <= 0) { |
| 131 |
abort("Number of runs must be a positive integer."); |
| 132 |
} |
| 133 |
break; |
| 134 |
|
| 135 |
case "--cache": |
| 136 |
options.cache = true; |
| 137 |
break; |
| 138 |
|
| 139 |
case "-o": |
| 140 |
case "--optimize": |
| 141 |
nextArg(); |
| 142 |
if (args.length === 0) { |
| 143 |
abort("Missing parameter of the -o/--optimize option."); |
| 144 |
} |
| 145 |
if (args[0] !== "speed" && args[0] !== "size") { |
| 146 |
abort("Optimization goal must be either \"speed\" or \"size\"."); |
| 147 |
} |
| 148 |
options.optimize = args[0]; |
| 149 |
break; |
| 150 |
|
| 151 |
case "-h": |
| 152 |
case "--help": |
| 153 |
printHelp(); |
| 154 |
exitSuccess(); |
| 155 |
break; |
| 156 |
|
| 157 |
default: |
| 158 |
abort("Unknown option: " + args[0] + "."); |
| 159 |
} |
| 160 |
nextArg(); |
| 161 |
} |
| 162 |
|
| 163 |
if (args.length > 0) { |
| 164 |
abort("No arguments are allowed."); |
| 165 |
} |
| 166 |
|
| 167 |
Runner.run(benchmarks, runCount, options, { |
| 168 |
readFile: function(file) { |
| 169 |
return fs.readFileSync(__dirname + "/" + file, "utf8"); |
| 170 |
}, |
| 171 |
|
| 172 |
testStart: function() { |
| 173 |
/* Nothing to do. */ |
| 174 |
}, |
| 175 |
|
| 176 |
testFinish: function(benchmark, test, inputSize, parseTime) { |
| 177 |
writeResult(test.title, inputSize, parseTime); |
| 178 |
}, |
| 179 |
|
| 180 |
benchmarkStart: function(benchmark) { |
| 181 |
writeHeading(benchmark.title); |
| 182 |
}, |
| 183 |
|
| 184 |
benchmarkFinish: function(benchmark, inputSize, parseTime) { |
| 185 |
writeSeparator(); |
| 186 |
writeResult(benchmark.title + " total", inputSize, parseTime); |
| 187 |
}, |
| 188 |
|
| 189 |
start: function() { |
| 190 |
writeTableHeader(); |
| 191 |
}, |
| 192 |
|
| 193 |
finish: function(inputSize, parseTime) { |
| 194 |
writeSeparator(); |
| 195 |
writeResult("Total", inputSize, parseTime); |
| 196 |
writeTableFooter(); |
| 197 |
} |
| 198 |
}); |