1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // Performance.now is used in latency benchmarks, the fallback is Date.now.
30 var performance = performance || {};
31 performance.now = (function() {
32 return performance.now ||
36 performance.webkitNow ||
40 // Simple framework for running the benchmark suites and
41 // computing a score based on the timing measurements.
44 // A benchmark has a name (string) and a function that will be run to
45 // do the performance measurement. The optional setup and tearDown
46 // arguments are functions that will be invoked before and after
47 // running the benchmark, but the running time of these functions will
48 // not be accounted for in the benchmark score.
49 function Benchmark(name, doWarmup, doDeterministic, run, setup, tearDown, rmsResult, minIterations) {
51 this.doWarmup = doWarmup;
52 this.doDeterministic = doDeterministic;
54 this.Setup = setup ? setup : function() { };
55 this.TearDown = tearDown ? tearDown : function() { };
56 this.rmsResult = rmsResult ? rmsResult : null;
57 this.minIterations = minIterations ? minIterations : 32;
61 // Benchmark results hold the benchmark and the measured time used to
62 // run the benchmark. The benchmark score is computed later once a
63 // full benchmark suite has run to completion. If latency is set to 0
64 // then there is no latency score for this benchmark.
65 function BenchmarkResult(benchmark, time, latency) {
66 this.benchmark = benchmark;
68 this.latency = latency;
72 // Automatically convert results to numbers. Used by the geometric
74 BenchmarkResult.prototype.valueOf = function() {
79 // Suites of benchmarks consist of a name and the set of benchmarks in
80 // addition to the reference timing that the final score will be based
81 // on. This way, all scores are relative to a reference run and higher
82 // scores implies better performance.
83 function BenchmarkSuite(name, reference, benchmarks) {
85 this.reference = reference;
86 this.benchmarks = benchmarks;
87 BenchmarkSuite.suites.push(this);
91 // Keep track of all declared benchmark suites.
92 BenchmarkSuite.suites = [];
94 // Scores are not comparable across versions. Bump the version if
95 // you're making changes that will affect that scores, e.g. if you add
96 // a new benchmark or change an existing one.
97 BenchmarkSuite.version = '9';
99 // Override the alert function to throw an exception instead.
100 alert = function(s) {
101 throw "Alert called with argument: " + s;
105 // To make the benchmark results predictable, we replace Math.random
106 // with a 100% deterministic alternative.
107 BenchmarkSuite.ResetRNG = function() {
108 Math.random = (function() {
111 // Robert Jenkins' 32 bit integer hash function.
112 seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff;
113 seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
114 seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff;
115 seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff;
116 seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff;
117 seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff;
118 return (seed & 0xfffffff) / 0x10000000;
124 // Runs all registered benchmark suites and optionally yields between
125 // each individual benchmark to avoid running for too long in the
126 // context of browsers. Once done, the final score is reported to the
128 BenchmarkSuite.RunSuites = function(runner) {
129 var continuation = null;
130 var suites = BenchmarkSuite.suites;
131 var length = suites.length;
132 BenchmarkSuite.scores = [];
135 while (continuation || index < length) {
137 continuation = continuation();
139 var suite = suites[index++];
140 if (runner.NotifyStart) runner.NotifyStart(suite.name);
141 continuation = suite.RunStep(runner);
143 if (continuation && typeof window != 'undefined' && window.setTimeout) {
144 window.setTimeout(RunStep, 25);
150 if (runner.NotifyScore) {
151 var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores);
152 var formatted = BenchmarkSuite.FormatScore(100 * score);
153 runner.NotifyScore(formatted);
160 // Counts the total number of registered benchmarks. Useful for
161 // showing progress as a percentage.
162 BenchmarkSuite.CountBenchmarks = function() {
164 var suites = BenchmarkSuite.suites;
165 for (var i = 0; i < suites.length; i++) {
166 result += suites[i].benchmarks.length;
172 // Computes the geometric mean of a set of numbers.
173 BenchmarkSuite.GeometricMean = function(numbers) {
175 for (var i = 0; i < numbers.length; i++) {
176 log += Math.log(numbers[i]);
178 return Math.pow(Math.E, log / numbers.length);
182 // Computes the geometric mean of a set of throughput time measurements.
183 BenchmarkSuite.GeometricMeanTime = function(measurements) {
185 for (var i = 0; i < measurements.length; i++) {
186 log += Math.log(measurements[i].time);
188 return Math.pow(Math.E, log / measurements.length);
192 // Computes the geometric mean of a set of rms measurements.
193 BenchmarkSuite.GeometricMeanLatency = function(measurements) {
195 var hasLatencyResult = false;
196 for (var i = 0; i < measurements.length; i++) {
197 if (measurements[i].latency != 0) {
198 log += Math.log(measurements[i].latency);
199 hasLatencyResult = true;
202 if (hasLatencyResult) {
203 return Math.pow(Math.E, log / measurements.length);
210 // Converts a score value to a string with at least three significant
212 BenchmarkSuite.FormatScore = function(value) {
214 return value.toFixed(0);
216 return value.toPrecision(3);
220 // Notifies the runner that we're done running a single benchmark in
221 // the benchmark suite. This can be useful to report progress.
222 BenchmarkSuite.prototype.NotifyStep = function(result) {
223 this.results.push(result);
224 if (this.runner.NotifyStep) this.runner.NotifyStep(result.benchmark.name);
228 // Notifies the runner that we're done with running a suite and that
229 // we have a result which can be reported to the user if needed.
230 BenchmarkSuite.prototype.NotifyResult = function() {
231 var mean = BenchmarkSuite.GeometricMeanTime(this.results);
232 var score = this.reference[0] / mean;
233 BenchmarkSuite.scores.push(score);
234 if (this.runner.NotifyResult) {
235 var formatted = BenchmarkSuite.FormatScore(100 * score);
236 this.runner.NotifyResult(this.name, formatted);
238 if (this.reference.length == 2) {
239 var meanLatency = BenchmarkSuite.GeometricMeanLatency(this.results);
240 if (meanLatency != 0) {
241 var scoreLatency = this.reference[1] / meanLatency;
242 BenchmarkSuite.scores.push(scoreLatency);
243 if (this.runner.NotifyResult) {
244 var formattedLatency = BenchmarkSuite.FormatScore(100 * scoreLatency)
245 this.runner.NotifyResult(this.name + "Latency", formattedLatency);
252 // Notifies the runner that running a benchmark resulted in an error.
253 BenchmarkSuite.prototype.NotifyError = function(error) {
254 if (this.runner.NotifyError) {
255 this.runner.NotifyError(this.name, error);
257 if (this.runner.NotifyStep) {
258 this.runner.NotifyStep(this.name);
263 // Runs a single benchmark for at least a second and computes the
264 // average time it takes to run a single iteration.
265 BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark, data) {
266 function Measure(data) {
268 var start = new Date();
270 // Run either for 1 second or for the number of iterations specified
271 // by minIterations, depending on the config flag doDeterministic.
272 for (var i = 0; (benchmark.doDeterministic ?
273 i<benchmark.minIterations : elapsed < 1000); i++) {
275 elapsed = new Date() - start;
279 data.elapsed += elapsed;
283 // Sets up data in order to skip or not the warmup phase.
284 if (!benchmark.doWarmup && data == null) {
285 data = { runs: 0, elapsed: 0 };
290 return { runs: 0, elapsed: 0 };
293 // If we've run too few iterations, we continue for another second.
294 if (data.runs < benchmark.minIterations) return data;
295 var usec = (data.elapsed * 1000) / data.runs;
296 var rms = (benchmark.rmsResult != null) ? benchmark.rmsResult() : 0;
297 this.NotifyStep(new BenchmarkResult(benchmark, usec, rms));
303 // This function starts running a suite, but stops between each
304 // individual benchmark in the suite and returns a continuation
305 // function which can be invoked to run the next benchmark. Once the
306 // last benchmark has been executed, null is returned.
307 BenchmarkSuite.prototype.RunStep = function(runner) {
308 BenchmarkSuite.ResetRNG();
310 this.runner = runner;
311 var length = this.benchmarks.length;
316 // Run the setup, the actual benchmark, and the tear down in three
317 // separate steps to allow the framework to yield between any of the
320 function RunNextSetup() {
321 if (index < length) {
323 suite.benchmarks[index].Setup();
325 suite.NotifyError(e);
328 return RunNextBenchmark;
330 suite.NotifyResult();
334 function RunNextBenchmark() {
336 data = suite.RunSingleBenchmark(suite.benchmarks[index], data);
338 suite.NotifyError(e);
341 // If data is null, we're done with this benchmark.
342 return (data == null) ? RunNextTearDown : RunNextBenchmark();
345 function RunNextTearDown() {
347 suite.benchmarks[index++].TearDown();
349 suite.NotifyError(e);
355 // Start out running the setup.
356 return RunNextSetup();