Skip to content

Commit

Permalink
Emit DNS and Connect timings
Browse files Browse the repository at this point in the history
Resolves #477
  • Loading branch information
macbre committed Oct 18, 2015
1 parent cc27c9f commit 006419e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
24 changes: 22 additions & 2 deletions lib/metadata/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -1031,9 +1031,29 @@
"desc": "time to window.load compared to the total loading time",
"unit": "%",
"module": "windowPerformance"
},
"performanceTimingConnect": {
"desc": "time it took to connect to the server before making the first HTTP request",
"unit": "ms",
"module": "windowPerformance"
},
"performanceTimingDNS": {
"desc": "time it took to resolve the domain before making the first HTTP request",
"unit": "ms",
"module": "windowPerformance"
},
"performanceTimingPageLoad": {
"desc": "time it took to fully load the page",
"unit": "ms",
"module": "windowPerformance"
},
"performanceTimingTTFB": {
"desc": "time it took to receive the first byte of the first HTTP response",
"unit": "ms",
"module": "windowPerformance"
}
},
"metricsCount": 173,
"metricsCount": 177,
"modulesCount": 35,
"version": "1.11.0"
"version": "1.12.0"
}
35 changes: 32 additions & 3 deletions modules/windowPerformance/windowPerformance.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
*
* @see http://w3c-test.org/webperf/specs/NavigationTiming/#dom-performancetiming-domloading
* @see https://developers.google.com/web/fundamentals/performance/critical-rendering-path/measure-crp
*
* Use Resource Timing API to measure time spent on DNS and TCP connection
*
* @see http://www.w3.org/TR/navigation-timing/
* @see http://www.stevesouders.com/blog/2014/08/21/resource-timing-practical-tips/
*/
/* global document: true, window: true */
/* global document: true, window: true, PerformanceTiming: true */
'use strict';

exports.version = '1.0';
exports.version = '1.1';

exports.module = function(phantomas) {
// times below are calculated relative to performance.timing.responseEnd (#117)
Expand All @@ -20,6 +25,13 @@ exports.module = function(phantomas) {
phantomas.setMetric('timeBackend'); // @desc time to the first byte compared to the total loading time [%]
phantomas.setMetric('timeFrontend'); // @desc time to window.load compared to the total loading time [%]

// get values from Resource Timing API (issue #477)
// set initial values to -1 as we expect the worst to happen ;)
phantomas.setMetric('performanceTimingConnect', -1); // @desc time it took to connect to the server before making the first HTTP request
phantomas.setMetric('performanceTimingDNS', -1); // @desc time it took to resolve the domain before making the first HTTP request
phantomas.setMetric('performanceTimingPageLoad', -1); // @desc time it took to fully load the page
phantomas.setMetric('performanceTimingTTFB', -1); // @desc time it took to receive the first byte of the first HTTP response

// measure dom... metrics from the moment HTML response was fully received
var responseEndTime = Date.now();

Expand Down Expand Up @@ -114,7 +126,8 @@ exports.module = function(phantomas) {
var backendTime = parseInt(phantomas.getMetric('timeToFirstByte'), 10),
frontendTime = parseInt(phantomas.getMetric('domComplete'), 10),
totalTime = backendTime + frontendTime,
backendTimePercentage;
backendTimePercentage,
timing;

if (totalTime === 0) {
return;
Expand All @@ -126,5 +139,21 @@ exports.module = function(phantomas) {
phantomas.setMetric('timeFrontend', 100 - backendTimePercentage);

phantomas.log('Performance timing: backend vs frontend time - %d% / %d%', backendTimePercentage, 100 - backendTimePercentage);

// try to take performance metrics from PerformanceTiming (issue #477)
if (!window.performance || ! (window.performance.timing instanceof PerformanceTiming) ) {
phantomas.log('performanceTiming: PerformanceTiming API not available!');
return;
}

phantomas.log('performanceTiming: data %j', window.performance.timing);

// @see http://www.stevesouders.com/blog/2014/08/21/resource-timing-practical-tips/
timing = window.performance.timing;

phantomas.setMetric('performanceTimingConnect', timing.connectEnd - timing.connectStart);
phantomas.setMetric('performanceTimingDNS', timing.domainLookupEnd - timing.domainLookupStart);
phantomas.setMetric('performanceTimingPageLoad', timing.loadEventStart - timing.navigationStart);
phantomas.setMetric('performanceTimingTTFB', timing.responseStart - timing.navigationStart);
});
};

0 comments on commit 006419e

Please sign in to comment.