| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
function Benchmark(name, run, setup, tearDown) { |
| 39 |
this.name = name; |
| 40 |
this.run = run; |
| 41 |
this.Setup = setup ? setup : function() { }; |
| 42 |
this.TearDown = tearDown ? tearDown : function() { }; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
function BenchmarkResult(benchmark, time) { |
| 50 |
this.benchmark = benchmark; |
| 51 |
this.time = time; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
BenchmarkResult.prototype.valueOf = function() { |
| 58 |
return this.time; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
function BenchmarkSuite(name, reference, benchmarks) { |
| 67 |
this.name = name; |
| 68 |
this.reference = reference; |
| 69 |
this.benchmarks = benchmarks; |
| 70 |
BenchmarkSuite.suites.push(this); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
BenchmarkSuite.suites = []; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
BenchmarkSuite.version = '6'; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
Math.random = (function() { |
| 87 |
var seed = 49734321; |
| 88 |
return function() { |
| 89 |
|
| 90 |
seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff; |
| 91 |
seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff; |
| 92 |
seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff; |
| 93 |
seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff; |
| 94 |
seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff; |
| 95 |
seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff; |
| 96 |
return (seed & 0xfffffff) / 0x10000000; |
| 97 |
}; |
| 98 |
})(); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
BenchmarkSuite.RunSuites = function(runner) { |
| 106 |
var continuation = null; |
| 107 |
var suites = BenchmarkSuite.suites; |
| 108 |
var length = suites.length; |
| 109 |
BenchmarkSuite.scores = []; |
| 110 |
var index = 0; |
| 111 |
function RunStep() { |
| 112 |
while (continuation || index < length) { |
| 113 |
if (continuation) { |
| 114 |
continuation = continuation(); |
| 115 |
} else { |
| 116 |
var suite = suites[index++]; |
| 117 |
if (runner.NotifyStart) runner.NotifyStart(suite.name); |
| 118 |
continuation = suite.RunStep(runner); |
| 119 |
} |
| 120 |
if (continuation && typeof window != 'undefined' && window.setTimeout) { |
| 121 |
window.setTimeout(RunStep, 25); |
| 122 |
return; |
| 123 |
} |
| 124 |
} |
| 125 |
if (runner.NotifyScore) { |
| 126 |
var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores); |
| 127 |
var formatted = BenchmarkSuite.FormatScore(100 * score); |
| 128 |
runner.NotifyScore(formatted); |
| 129 |
} |
| 130 |
} |
| 131 |
RunStep(); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
BenchmarkSuite.CountBenchmarks = function() { |
| 138 |
var result = 0; |
| 139 |
var suites = BenchmarkSuite.suites; |
| 140 |
for (var i = 0; i < suites.length; i++) { |
| 141 |
result += suites[i].benchmarks.length; |
| 142 |
} |
| 143 |
return result; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
BenchmarkSuite.GeometricMean = function(numbers) { |
| 149 |
var log = 0; |
| 150 |
for (var i = 0; i < numbers.length; i++) { |
| 151 |
log += Math.log(numbers[i]); |
| 152 |
} |
| 153 |
return Math.pow(Math.E, log / numbers.length); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
BenchmarkSuite.FormatScore = function(value) { |
| 160 |
if (value > 100) { |
| 161 |
return value.toFixed(0); |
| 162 |
} else { |
| 163 |
return value.toPrecision(3); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
BenchmarkSuite.prototype.NotifyStep = function(result) { |
| 170 |
this.results.push(result); |
| 171 |
if (this.runner.NotifyStep) this.runner.NotifyStep(result.benchmark.name); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
BenchmarkSuite.prototype.NotifyResult = function() { |
| 178 |
var mean = BenchmarkSuite.GeometricMean(this.results); |
| 179 |
var score = this.reference / mean; |
| 180 |
BenchmarkSuite.scores.push(score); |
| 181 |
if (this.runner.NotifyResult) { |
| 182 |
var formatted = BenchmarkSuite.FormatScore(100 * score); |
| 183 |
this.runner.NotifyResult(this.name, formatted); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
BenchmarkSuite.prototype.NotifyError = function(error) { |
| 190 |
if (this.runner.NotifyError) { |
| 191 |
this.runner.NotifyError(this.name, error); |
| 192 |
} |
| 193 |
if (this.runner.NotifyStep) { |
| 194 |
this.runner.NotifyStep(this.name); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
var MIN_TIME = 10000; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark, data) { |
| 203 |
function Measure(data) { |
| 204 |
var elapsed = 0; |
| 205 |
var start = new Date(); |
| 206 |
for (var n = 0; elapsed < MIN_TIME; n++) { |
| 207 |
benchmark.run(); |
| 208 |
elapsed = new Date() - start; |
| 209 |
} |
| 210 |
if (data != null) { |
| 211 |
data.runs += n; |
| 212 |
data.elapsed += elapsed; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
if (data == null) { |
| 217 |
|
| 218 |
|
| 219 |
Measure(null); |
| 220 |
return { runs: 0, elapsed: 0 }; |
| 221 |
} else { |
| 222 |
Measure(data); |
| 223 |
|
| 224 |
if (data.runs < 32) return data; |
| 225 |
var usec = (data.elapsed * MIN_TIME) / data.runs; |
| 226 |
this.NotifyStep(new BenchmarkResult(benchmark, usec)); |
| 227 |
return null; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
BenchmarkSuite.prototype.RunStep = function(runner) { |
| 237 |
this.results = []; |
| 238 |
this.runner = runner; |
| 239 |
var length = this.benchmarks.length; |
| 240 |
var index = 0; |
| 241 |
var suite = this; |
| 242 |
var data; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
function RunNextSetup() { |
| 249 |
if (index < length) { |
| 250 |
try { |
| 251 |
suite.benchmarks[index].Setup(); |
| 252 |
} catch (e) { |
| 253 |
suite.NotifyError(e); |
| 254 |
return null; |
| 255 |
} |
| 256 |
return RunNextBenchmark; |
| 257 |
} |
| 258 |
suite.NotifyResult(); |
| 259 |
return null; |
| 260 |
} |
| 261 |
|
| 262 |
function RunNextBenchmark() { |
| 263 |
try { |
| 264 |
data = suite.RunSingleBenchmark(suite.benchmarks[index], data); |
| 265 |
} catch (e) { |
| 266 |
suite.NotifyError(e); |
| 267 |
return null; |
| 268 |
} |
| 269 |
|
| 270 |
return (data == null) ? RunNextTearDown : RunNextBenchmark(); |
| 271 |
} |
| 272 |
|
| 273 |
function RunNextTearDown() { |
| 274 |
try { |
| 275 |
suite.benchmarks[index++].TearDown(); |
| 276 |
} catch (e) { |
| 277 |
suite.NotifyError(e); |
| 278 |
return null; |
| 279 |
} |
| 280 |
return RunNextSetup; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
return RunNextSetup(); |
| 285 |
} |