test_performance_now.html (2416B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test for High Resolution Timer</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 </head> 8 <body> 9 <script> 10 ok(window.performance, "Performance object should exist."); 11 ok(typeof window.performance.now == 'function', "Performance object should have a 'now' method."); 12 var n = window.performance.now(), d = Date.now(); 13 ok(n >= 0, "The value of now() should be equal to or greater than 0."); 14 ok(window.performance.now() >= n, "The value of now() should monotonically increase."); 15 16 SimpleTest.waitForExplicitFinish(); 17 SimpleTest.requestFlakyTimeout("using setTimeout() to measure performance.now()"); 18 19 // Spin on setTimeout() until performance.now() increases. Due to recent 20 // security developments, the hr-time working group has not yet reached 21 // consensus on what the recommend minimum clock resolution should be: 22 // https://w3c.github.io/hr-time/#clock-resolution 23 // Since setTimeout might return too early/late, our goal is for 24 // performance.now() to increase before a 2 ms deadline rather than specific 25 // number of setTimeout(N) invocations. 26 // See bug 749894 (intermittent failures of this test) 27 var checks = 0; 28 29 function checkAfterTimeout() { 30 checks++; 31 var d2 = Date.now(); 32 var n2 = window.performance.now(); 33 34 // Spin on setTimeout() until performance.now() increases. Abort the 35 // test if it runs for more than 2 ms or 50 timeouts. 36 let elapsedTime = d2 - d; 37 let elapsedPerf = n2 - n; 38 if (elapsedPerf == 0 && elapsedTime < 2 && checks < 50) { 39 setTimeout(checkAfterTimeout, 1); 40 return; 41 } 42 43 // Our implementation provides 1 ms resolution (bug 1451790). 44 ok(elapsedPerf >= 1, 45 `Loose - the value of now() should increase by no less than 1 ms ` + 46 `after 2 ms. delta now(): ${elapsedPerf} ms`); 47 48 // If we need more than 1 iteration, then either performance.now() 49 // resolution is shorter than 1 ms or setTimeout() is returning too early. 50 ok(checks == 1, 51 `Strict - the value of now() should increase after one setTimeout. ` + 52 `iters: ${checks}, dt: ${elapsedTime}, now(): ${n2}`); 53 54 SimpleTest.finish(); 55 }; 56 setTimeout(checkAfterTimeout, 1); 57 </script> 58 </body> 59 </html>