1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/general/test_performance_now.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,65 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<head> 1.7 + <title>Test for High Resolution Timer</title> 1.8 + <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> 1.9 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.10 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.11 +</head> 1.12 +<body> 1.13 + <script> 1.14 + ok(window.performance, "Performance object should exist."); 1.15 + ok(typeof window.performance.now == 'function', "Performance object should have a 'now' method."); 1.16 + var n = window.performance.now(), d = Date.now(); 1.17 + ok(n >= 0, "The value of now() should be equal to or greater than 0."); 1.18 + ok(window.performance.now() >= n, "The value of now() should monotonically increase."); 1.19 + SimpleTest.waitForExplicitFinish(); 1.20 + 1.21 + // The spec says performance.now() should have micro-second resolution, but allows 1ms if the platform doesn't support it. 1.22 + // Our implementation does provide micro-second resolution, except for windows XP combined with some HW properties 1.23 + // where we can't use QueryPerformanceCounters (see comments at mozilla-central/xpcom/ds/TimeStamp_windows.cpp). 1.24 + // This XP-low-res case results in about 15ms resolutions, and can be identified when perf.now() returns only integers. 1.25 + // 1.26 + // Since setTimeout might return too early/late, our goal is that perf.now() changed within 2ms 1.27 + // (or 25ms for XP-low-res), rather than specific number of setTimeout(N) invocations. 1.28 + // See bug 749894 (intermittent failures of this test) 1.29 + var platformPossiblyLowRes = navigator.oscpu.indexOf("Windows NT 5.1") == 0; // XP only 1.30 + var allInts = (n % 1) == 0; // Indicator of limited HW resolution. 1.31 + var checks = 0; 1.32 + 1.33 + function checkAfterTimeout() { 1.34 + checks++; 1.35 + var d2 = Date.now(); 1.36 + var n2 = window.performance.now(); 1.37 + 1.38 + allInts = allInts && (n2 % 1) == 0; 1.39 + var lowResCounter = platformPossiblyLowRes && allInts; 1.40 + 1.41 + if ( n2 == n && checks < 50 && // 50 is just a failsafe. Our real goals are 2ms or 25ms. 1.42 + ( (d2 - d) < 2 // The spec allows 1ms resolution. We allow up to measured 2ms to ellapse. 1.43 + || 1.44 + lowResCounter && 1.45 + (d2 - d) < 25 1.46 + ) 1.47 + ) { 1.48 + setTimeout(checkAfterTimeout, 1); 1.49 + return; 1.50 + } 1.51 + 1.52 + // Loose spec: 1ms resolution, or 15ms resolution for the XP-low-res case. 1.53 + // We shouldn't test that dt is actually within 2/25ms since the iterations break if it isn't, and timeout could be late. 1.54 + ok(n2 > n, "Loose - the value of now() should increase within 2ms (or 25ms if low-res counter) (delta now(): " + (n2 - n) + " ms)."); 1.55 + 1.56 + // Strict spec: if it's not the XP-low-res case, while the spec allows 1ms resolution, it prefers microseconds, which we provide. 1.57 + // Since the fastest setTimeout return which I observed was ~500 microseconds, a microseconds counter should change in 1 iteretion. 1.58 + ok(n2 > n && (lowResCounter || checks == 1), 1.59 + "Strict - [if high-res counter] the value of now() should increase after one setTimeout (hi-res: " + (!lowResCounter) + 1.60 + ", iters: " + checks + 1.61 + ", dt: " + (d2 - d) + 1.62 + ", now(): " + n2 + ")."); 1.63 + SimpleTest.finish(); 1.64 + }; 1.65 + setTimeout(checkAfterTimeout, 1); 1.66 + </script> 1.67 +</body> 1.68 +</html>