Index: tools/telemetry/telemetry/core/network_quiescence.js |
diff --git a/tools/perf/metrics/speedindex.js b/tools/telemetry/telemetry/core/network_quiescence.js |
similarity index 60% |
rename from tools/perf/metrics/speedindex.js |
rename to tools/telemetry/telemetry/core/network_quiescence.js |
index c64f38b1667be1901935f3ec6afb6b9cc3a8f2a5..d49db5b1ff1d3749a736e75aa093c999f1e6ab2b 100644 |
--- a/tools/perf/metrics/speedindex.js |
+++ b/tools/telemetry/telemetry/core/network_quiescence.js |
@@ -6,10 +6,16 @@ |
/** |
* @fileoverview This file provides a JavaScript helper function that |
- * determines when the last resource was received. |
+ * determines when network quiescence has been reached based on the time since |
+ * the last resource was received. |
*/ |
(function() { |
+ // Make executing this code idempotent. |
+ if (window.__telemetry_testHasReachedNetworkQuiescence) { |
+ return; |
+ } |
+ |
// Set the Resource Timing interface functions that will be used below |
// to use whatever version is available currently regardless of vendor |
// prefix. |
@@ -38,19 +44,32 @@ |
// so we can clear new entries when they're added. |
var lastEntry = null; |
+ // True when no resource has been loaded from the network for |
+ //|QUIESCENCE_TIMEOUT_MS| milliseconds. This value is sticky. |
+ var hasReachedQuiesence = false; |
+ |
+ // Time to wait before declaring network quiescence in milliseconds. |
+ var QUIESCENCE_TIMEOUT_MS = 2000; |
+ |
/** |
* This method uses the Resource Timing interface, which is described at |
- * http://www.w3.org/TR/resource-timing/. It provides information about |
- * timings for resources such as images and script files, and it includes |
- * resources requested via XMLHttpRequest. |
+ * http://www.w3.org/TR/resource-timing/. It determines whether the time |
+ * since lodading any resources such as images and script files (including |
+ * resources requested via XMLHttpRequest) has exceeded a threshold defined |
+ # by |QUIESCENCE_TIMEOUT_MS|. |
* |
- * @return {number} The time since either the load event, or the last |
- * the last resource was received after the load event. If the load |
- * event hasn't yet happened, return 0. |
+ * @return {boolean} True if the time since either the load event, or the last |
+ * resource was received after the load event exceeds the aforementioned |
+ * threshold. This state is sticky, so once this function returns true for a |
+ * given page, it will always return true. |
*/ |
- window.timeSinceLastResponseAfterLoadMs = function() { |
+ window.__telemetry_testHasReachedNetworkQuiescence = function() { |
+ if (hasReachedQuiesence) { |
+ return true; |
+ } |
+ |
if (window.document.readyState !== 'complete') { |
- return 0; |
+ return false; |
} |
var resourceTimings = window.performance.getEntriesByType('resource'); |
@@ -64,13 +83,21 @@ |
// so we must also get load time in the same terms. |
var timing = window.performance.timing; |
var loadTime = timing.loadEventEnd - timing.navigationStart; |
+ var lastResponseTimeMs = 0; |
// If there have been no resource timing entries, or the last entry was |
- // before the load event, then return the time since the load event. |
+ // before the load event, then use the time since the load event. |
if (!lastEntry || lastEntry.responseEnd < loadTime) { |
- return window.performance.now() - loadTime; |
+ lastResponseTimeMs = window.performance.now() - loadTime; |
+ } else { |
+ lastResponseTimeMs = window.performance.now() - lastEntry.responseEnd; |
+ } |
+ |
+ if (lastResponseTimeMs >= QUIESCENCE_TIMEOUT_MS) { |
+ hasReachedQuiesence = true; |
} |
- return window.performance.now() - lastEntry.responseEnd; |
+ |
+ return hasReachedQuiesence; |
} |
})(); |