dom/tests/mochitest/general/test_performance_now.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial