js/src/v8/base.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // Copyright 2012 the V8 project authors. All rights reserved.
michael@0 2 // Redistribution and use in source and binary forms, with or without
michael@0 3 // modification, are permitted provided that the following conditions are
michael@0 4 // met:
michael@0 5 //
michael@0 6 // * Redistributions of source code must retain the above copyright
michael@0 7 // notice, this list of conditions and the following disclaimer.
michael@0 8 // * Redistributions in binary form must reproduce the above
michael@0 9 // copyright notice, this list of conditions and the following
michael@0 10 // disclaimer in the documentation and/or other materials provided
michael@0 11 // with the distribution.
michael@0 12 // * Neither the name of Google Inc. nor the names of its
michael@0 13 // contributors may be used to endorse or promote products derived
michael@0 14 // from this software without specific prior written permission.
michael@0 15 //
michael@0 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 27
michael@0 28
michael@0 29 // Simple framework for running the benchmark suites and
michael@0 30 // computing a score based on the timing measurements.
michael@0 31
michael@0 32
michael@0 33 // A benchmark has a name (string) and a function that will be run to
michael@0 34 // do the performance measurement. The optional setup and tearDown
michael@0 35 // arguments are functions that will be invoked before and after
michael@0 36 // running the benchmark, but the running time of these functions will
michael@0 37 // not be accounted for in the benchmark score.
michael@0 38 function Benchmark(name, run, setup, tearDown) {
michael@0 39 this.name = name;
michael@0 40 this.run = run;
michael@0 41 this.Setup = setup ? setup : function() { };
michael@0 42 this.TearDown = tearDown ? tearDown : function() { };
michael@0 43 }
michael@0 44
michael@0 45
michael@0 46 // Benchmark results hold the benchmark and the measured time used to
michael@0 47 // run the benchmark. The benchmark score is computed later once a
michael@0 48 // full benchmark suite has run to completion.
michael@0 49 function BenchmarkResult(benchmark, time) {
michael@0 50 this.benchmark = benchmark;
michael@0 51 this.time = time;
michael@0 52 }
michael@0 53
michael@0 54
michael@0 55 // Automatically convert results to numbers. Used by the geometric
michael@0 56 // mean computation.
michael@0 57 BenchmarkResult.prototype.valueOf = function() {
michael@0 58 return this.time;
michael@0 59 }
michael@0 60
michael@0 61
michael@0 62 // Suites of benchmarks consist of a name and the set of benchmarks in
michael@0 63 // addition to the reference timing that the final score will be based
michael@0 64 // on. This way, all scores are relative to a reference run and higher
michael@0 65 // scores implies better performance.
michael@0 66 function BenchmarkSuite(name, reference, benchmarks) {
michael@0 67 this.name = name;
michael@0 68 this.reference = reference;
michael@0 69 this.benchmarks = benchmarks;
michael@0 70 BenchmarkSuite.suites.push(this);
michael@0 71 }
michael@0 72
michael@0 73
michael@0 74 // Keep track of all declared benchmark suites.
michael@0 75 BenchmarkSuite.suites = [];
michael@0 76
michael@0 77
michael@0 78 // Scores are not comparable across versions. Bump the version if
michael@0 79 // you're making changes that will affect that scores, e.g. if you add
michael@0 80 // a new benchmark or change an existing one.
michael@0 81 BenchmarkSuite.version = '7';
michael@0 82
michael@0 83
michael@0 84 // To make the benchmark results predictable, we replace Math.random
michael@0 85 // with a 100% deterministic alternative.
michael@0 86 Math.random = (function() {
michael@0 87 var seed = 49734321;
michael@0 88 return function() {
michael@0 89 // Robert Jenkins' 32 bit integer hash function.
michael@0 90 seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff;
michael@0 91 seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
michael@0 92 seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff;
michael@0 93 seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff;
michael@0 94 seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff;
michael@0 95 seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff;
michael@0 96 return (seed & 0xfffffff) / 0x10000000;
michael@0 97 };
michael@0 98 })();
michael@0 99
michael@0 100
michael@0 101 // Runs all registered benchmark suites and optionally yields between
michael@0 102 // each individual benchmark to avoid running for too long in the
michael@0 103 // context of browsers. Once done, the final score is reported to the
michael@0 104 // runner.
michael@0 105 BenchmarkSuite.RunSuites = function(runner) {
michael@0 106 var continuation = null;
michael@0 107 var suites = BenchmarkSuite.suites;
michael@0 108 var length = suites.length;
michael@0 109 BenchmarkSuite.scores = [];
michael@0 110 var index = 0;
michael@0 111 function RunStep() {
michael@0 112 while (continuation || index < length) {
michael@0 113 if (continuation) {
michael@0 114 continuation = continuation();
michael@0 115 } else {
michael@0 116 var suite = suites[index++];
michael@0 117 if (runner.NotifyStart) runner.NotifyStart(suite.name);
michael@0 118 continuation = suite.RunStep(runner);
michael@0 119 }
michael@0 120 if (continuation && typeof window != 'undefined' && window.setTimeout) {
michael@0 121 window.setTimeout(RunStep, 25);
michael@0 122 return;
michael@0 123 }
michael@0 124 }
michael@0 125 if (runner.NotifyScore) {
michael@0 126 var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores);
michael@0 127 var formatted = BenchmarkSuite.FormatScore(100 * score);
michael@0 128 runner.NotifyScore(formatted);
michael@0 129 }
michael@0 130 }
michael@0 131 RunStep();
michael@0 132 }
michael@0 133
michael@0 134
michael@0 135 // Counts the total number of registered benchmarks. Useful for
michael@0 136 // showing progress as a percentage.
michael@0 137 BenchmarkSuite.CountBenchmarks = function() {
michael@0 138 var result = 0;
michael@0 139 var suites = BenchmarkSuite.suites;
michael@0 140 for (var i = 0; i < suites.length; i++) {
michael@0 141 result += suites[i].benchmarks.length;
michael@0 142 }
michael@0 143 return result;
michael@0 144 }
michael@0 145
michael@0 146
michael@0 147 // Computes the geometric mean of a set of numbers.
michael@0 148 BenchmarkSuite.GeometricMean = function(numbers) {
michael@0 149 var log = 0;
michael@0 150 for (var i = 0; i < numbers.length; i++) {
michael@0 151 log += Math.log(numbers[i]);
michael@0 152 }
michael@0 153 return Math.pow(Math.E, log / numbers.length);
michael@0 154 }
michael@0 155
michael@0 156
michael@0 157 // Converts a score value to a string with at least three significant
michael@0 158 // digits.
michael@0 159 BenchmarkSuite.FormatScore = function(value) {
michael@0 160 if (value > 100) {
michael@0 161 return value.toFixed(0);
michael@0 162 } else {
michael@0 163 return value.toPrecision(3);
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 // Notifies the runner that we're done running a single benchmark in
michael@0 168 // the benchmark suite. This can be useful to report progress.
michael@0 169 BenchmarkSuite.prototype.NotifyStep = function(result) {
michael@0 170 this.results.push(result);
michael@0 171 if (this.runner.NotifyStep) this.runner.NotifyStep(result.benchmark.name);
michael@0 172 }
michael@0 173
michael@0 174
michael@0 175 // Notifies the runner that we're done with running a suite and that
michael@0 176 // we have a result which can be reported to the user if needed.
michael@0 177 BenchmarkSuite.prototype.NotifyResult = function() {
michael@0 178 var mean = BenchmarkSuite.GeometricMean(this.results);
michael@0 179 var score = this.reference / mean;
michael@0 180 BenchmarkSuite.scores.push(score);
michael@0 181 if (this.runner.NotifyResult) {
michael@0 182 var formatted = BenchmarkSuite.FormatScore(100 * score);
michael@0 183 this.runner.NotifyResult(this.name, formatted);
michael@0 184 }
michael@0 185 }
michael@0 186
michael@0 187
michael@0 188 // Notifies the runner that running a benchmark resulted in an error.
michael@0 189 BenchmarkSuite.prototype.NotifyError = function(error) {
michael@0 190 if (this.runner.NotifyError) {
michael@0 191 this.runner.NotifyError(this.name, error);
michael@0 192 }
michael@0 193 if (this.runner.NotifyStep) {
michael@0 194 this.runner.NotifyStep(this.name);
michael@0 195 }
michael@0 196 }
michael@0 197
michael@0 198
michael@0 199 // Runs a single benchmark for at least a second and computes the
michael@0 200 // average time it takes to run a single iteration.
michael@0 201 BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark, data) {
michael@0 202 function Measure(data) {
michael@0 203 var elapsed = 0;
michael@0 204 var start = new Date();
michael@0 205 for (var n = 0; elapsed < 1000; n++) {
michael@0 206 benchmark.run();
michael@0 207 elapsed = new Date() - start;
michael@0 208 }
michael@0 209 if (data != null) {
michael@0 210 data.runs += n;
michael@0 211 data.elapsed += elapsed;
michael@0 212 }
michael@0 213 }
michael@0 214
michael@0 215 if (data == null) {
michael@0 216 // Measure the benchmark once for warm up and throw the result
michael@0 217 // away. Return a fresh data object.
michael@0 218 Measure(null);
michael@0 219 return { runs: 0, elapsed: 0 };
michael@0 220 } else {
michael@0 221 Measure(data);
michael@0 222 // If we've run too few iterations, we continue for another second.
michael@0 223 if (data.runs < 32) return data;
michael@0 224 var usec = (data.elapsed * 1000) / data.runs;
michael@0 225 this.NotifyStep(new BenchmarkResult(benchmark, usec));
michael@0 226 return null;
michael@0 227 }
michael@0 228 }
michael@0 229
michael@0 230
michael@0 231 // This function starts running a suite, but stops between each
michael@0 232 // individual benchmark in the suite and returns a continuation
michael@0 233 // function which can be invoked to run the next benchmark. Once the
michael@0 234 // last benchmark has been executed, null is returned.
michael@0 235 BenchmarkSuite.prototype.RunStep = function(runner) {
michael@0 236 this.results = [];
michael@0 237 this.runner = runner;
michael@0 238 var length = this.benchmarks.length;
michael@0 239 var index = 0;
michael@0 240 var suite = this;
michael@0 241 var data;
michael@0 242
michael@0 243 // Run the setup, the actual benchmark, and the tear down in three
michael@0 244 // separate steps to allow the framework to yield between any of the
michael@0 245 // steps.
michael@0 246
michael@0 247 function RunNextSetup() {
michael@0 248 if (index < length) {
michael@0 249 try {
michael@0 250 suite.benchmarks[index].Setup();
michael@0 251 } catch (e) {
michael@0 252 suite.NotifyError(e);
michael@0 253 return null;
michael@0 254 }
michael@0 255 return RunNextBenchmark;
michael@0 256 }
michael@0 257 suite.NotifyResult();
michael@0 258 return null;
michael@0 259 }
michael@0 260
michael@0 261 function RunNextBenchmark() {
michael@0 262 try {
michael@0 263 data = suite.RunSingleBenchmark(suite.benchmarks[index], data);
michael@0 264 } catch (e) {
michael@0 265 suite.NotifyError(e);
michael@0 266 return null;
michael@0 267 }
michael@0 268 // If data is null, we're done with this benchmark.
michael@0 269 return (data == null) ? RunNextTearDown : RunNextBenchmark();
michael@0 270 }
michael@0 271
michael@0 272 function RunNextTearDown() {
michael@0 273 try {
michael@0 274 suite.benchmarks[index++].TearDown();
michael@0 275 } catch (e) {
michael@0 276 suite.NotifyError(e);
michael@0 277 return null;
michael@0 278 }
michael@0 279 return RunNextSetup;
michael@0 280 }
michael@0 281
michael@0 282 // Start out running the setup.
michael@0 283 return RunNextSetup();
michael@0 284 }

mercurial