|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <title>Test for High Resolution Timer</title> |
|
5 <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> |
|
6 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
8 </head> |
|
9 <body> |
|
10 <script> |
|
11 ok(window.performance, "Performance object should exist."); |
|
12 ok(typeof window.performance.now == 'function', "Performance object should have a 'now' method."); |
|
13 var n = window.performance.now(), d = Date.now(); |
|
14 ok(n >= 0, "The value of now() should be equal to or greater than 0."); |
|
15 ok(window.performance.now() >= n, "The value of now() should monotonically increase."); |
|
16 SimpleTest.waitForExplicitFinish(); |
|
17 |
|
18 // The spec says performance.now() should have micro-second resolution, but allows 1ms if the platform doesn't support it. |
|
19 // Our implementation does provide micro-second resolution, except for windows XP combined with some HW properties |
|
20 // where we can't use QueryPerformanceCounters (see comments at mozilla-central/xpcom/ds/TimeStamp_windows.cpp). |
|
21 // This XP-low-res case results in about 15ms resolutions, and can be identified when perf.now() returns only integers. |
|
22 // |
|
23 // Since setTimeout might return too early/late, our goal is that perf.now() changed within 2ms |
|
24 // (or 25ms for XP-low-res), rather than specific number of setTimeout(N) invocations. |
|
25 // See bug 749894 (intermittent failures of this test) |
|
26 var platformPossiblyLowRes = navigator.oscpu.indexOf("Windows NT 5.1") == 0; // XP only |
|
27 var allInts = (n % 1) == 0; // Indicator of limited HW resolution. |
|
28 var checks = 0; |
|
29 |
|
30 function checkAfterTimeout() { |
|
31 checks++; |
|
32 var d2 = Date.now(); |
|
33 var n2 = window.performance.now(); |
|
34 |
|
35 allInts = allInts && (n2 % 1) == 0; |
|
36 var lowResCounter = platformPossiblyLowRes && allInts; |
|
37 |
|
38 if ( n2 == n && checks < 50 && // 50 is just a failsafe. Our real goals are 2ms or 25ms. |
|
39 ( (d2 - d) < 2 // The spec allows 1ms resolution. We allow up to measured 2ms to ellapse. |
|
40 || |
|
41 lowResCounter && |
|
42 (d2 - d) < 25 |
|
43 ) |
|
44 ) { |
|
45 setTimeout(checkAfterTimeout, 1); |
|
46 return; |
|
47 } |
|
48 |
|
49 // Loose spec: 1ms resolution, or 15ms resolution for the XP-low-res case. |
|
50 // We shouldn't test that dt is actually within 2/25ms since the iterations break if it isn't, and timeout could be late. |
|
51 ok(n2 > n, "Loose - the value of now() should increase within 2ms (or 25ms if low-res counter) (delta now(): " + (n2 - n) + " ms)."); |
|
52 |
|
53 // Strict spec: if it's not the XP-low-res case, while the spec allows 1ms resolution, it prefers microseconds, which we provide. |
|
54 // Since the fastest setTimeout return which I observed was ~500 microseconds, a microseconds counter should change in 1 iteretion. |
|
55 ok(n2 > n && (lowResCounter || checks == 1), |
|
56 "Strict - [if high-res counter] the value of now() should increase after one setTimeout (hi-res: " + (!lowResCounter) + |
|
57 ", iters: " + checks + |
|
58 ", dt: " + (d2 - d) + |
|
59 ", now(): " + n2 + ")."); |
|
60 SimpleTest.finish(); |
|
61 }; |
|
62 setTimeout(checkAfterTimeout, 1); |
|
63 </script> |
|
64 </body> |
|
65 </html> |