tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_worker_performance_now.js (2270B)


      1 function ok(a, msg) {
      2  dump("OK: " + !!a + "  =>  " + a + ": " + msg + "\n");
      3  postMessage({ type: "status", status: !!a, msg: a + ": " + msg });
      4 }
      5 
      6 function workerTestDone() {
      7  postMessage({ type: "finish" });
      8 }
      9 
     10 ok(self.performance, "Performance object should exist.");
     11 ok(
     12  typeof self.performance.now == "function",
     13  "Performance object should have a 'now' method."
     14 );
     15 var n = self.performance.now(),
     16  d = Date.now();
     17 ok(n >= 0, "The value of now() should be equal to or greater than 0.");
     18 ok(
     19  self.performance.now() >= n,
     20  "The value of now() should monotonically increase."
     21 );
     22 
     23 // Spin on setTimeout() until performance.now() increases. Due to recent
     24 // security developments, the hr-time working group has not yet reached
     25 // consensus on what the recommend minimum clock resolution should be:
     26 // https://w3c.github.io/hr-time/#clock-resolution
     27 // Since setTimeout might return too early/late, our goal is for
     28 // performance.now() to increase before a 2 ms deadline rather than specific
     29 // number of setTimeout(N) invocations.
     30 // See bug 749894 (intermittent failures of this test)
     31 setTimeout(checkAfterTimeout, 1);
     32 
     33 var checks = 0;
     34 
     35 function checkAfterTimeout() {
     36  checks++;
     37  var d2 = Date.now();
     38  var n2 = self.performance.now();
     39 
     40  // Spin on setTimeout() until performance.now() increases. Abort the test
     41  // if it runs for more than 2 ms or 50 timeouts.
     42  let elapsedTime = d2 - d;
     43  let elapsedPerf = n2 - n;
     44  if (elapsedPerf == 0 && elapsedTime < 2 && checks < 50) {
     45    setTimeout(checkAfterTimeout, 1);
     46    return;
     47  }
     48 
     49  // Our implementation provides 1 ms resolution (bug 1451790), but we
     50  // can't assert that elapsedPerf >= 1 ms because this worker test runs with
     51  // "privacy.reduceTimerPrecision" == false so performance.now() is not
     52  // limited to 1 ms resolution.
     53  ok(
     54    elapsedPerf > 0,
     55    `Loose - the value of now() should increase after 2ms. ` +
     56      `delta now(): ${elapsedPerf} ms`
     57  );
     58 
     59  // If we need more than 1 iteration, then either performance.now() resolution
     60  // is shorter than 1 ms or setTimeout() is returning too early.
     61  ok(
     62    checks == 1,
     63    `Strict - the value of now() should increase after one setTimeout. ` +
     64      `iters: ${checks}, dt: ${elapsedTime}, now(): ${n2}`
     65  );
     66 
     67  workerTestDone();
     68 }