Skip to content

Instantly share code, notes, and snippets.

@mwurzberger
Last active August 29, 2015 14:07
Show Gist options
  • Save mwurzberger/77fdfb2f8df9c9baba57 to your computer and use it in GitHub Desktop.
Save mwurzberger/77fdfb2f8df9c9baba57 to your computer and use it in GitHub Desktop.
Performance Function
function mwurzbergerGetPerformanceData ( includeEntries, includeRaw )
{
includeEntries = includeEntries || false;
includeRaw = includeRaw || false;
var timing = window.performance.timing;
var entries = window.performance.getEntries();
var performanceRecord = {
'page': window.location.href || window.location || 'Undefined',
'navigation':
{
'appCodeName': navigator.appCodeName || 'undefined',
'appName': navigator.appName || 'undefined',
//'appVersion': navigator.appVersion || 'undefined',
'userAgent': navigator.userAgent || 'undefined',
'platform': navigator.platform || 'undefined'
}
};
/*
* See http://www.w3.org/TR/navigation-timing/#processing-model
*/
performanceRecord.pageTiming = {
'duration': parseInt(timing.loadEventEnd - timing.navigationStart),
'network': {
'startTime': timing.navigationStart,
'endTime': timing.connectEnd,
'duration': parseInt(timing.connectEnd - timing.navigationStart),
'dns': parseInt(timing.domainLookupEnd - timing.domainLookupStart),
'tcp': parseInt(timing.connectEnd - timing.connectStart)
},
'server': {
'startTime': timing.requestStart,
'endTime': timing.responseEnd,
'duration': parseInt(timing.responseEnd - timing.requestStart),
'request': parseInt(timing.responseStart - timing.requestStart),
'response': parseInt(timing.responseEnd - timing.responseStart)
},
'browser': {
'startTime': timing.unloadEventStart,
'endTime': timing.loadEventEnd,
'duration': parseInt(timing.loadEventEnd - timing.unloadEventStart),
'domLoading': parseInt(timing.domLoading - timing.responseEnd),
'domInteractive': parseInt(timing.domInteractive - timing.domLoading),
'domContentLoad': parseInt(timing.domContentLoadedEventEnd - timing.domContentLoadedEventStart),
'domComplete': parseInt(timing.domComplete - timing.domContentLoadedEventEnd),
'loadEvent': parseInt(timing.loadEventEnd - timing.domComplete)
}
};
if( includeRaw ){
performanceRecord.pageTiming.raw = timing;
}
if( includeEntries ){
/*
* See explanation here http://googledevelopers.blogspot.com/2013/12/measuring-network-performance-with.html
*/
performanceRecord.entries = [];
for( var x = 0; x < entries.length; x++ ){
var entry = entries[x];
var newEntry = {
'url': entry.name,
'entryType': entry.entryType ,
'initiatorType': entry.initiatorType ,
'startTime': entry.startTime,
'endTime': entry.responseEnd,
'duration': parseInt(entry.responseEnd - entry.startTime),
'tcp': parseInt(entry.connectEnd - entry.connectStart),
'dns': parseInt(entry.domainLookupEnd - entry.domainLookupStart),
'ttfb': parseInt(entry.responseStart - entry.startTime),
'transfer': parseInt(entry.responseEnd - entry.responseStart)
};
performanceRecord.entries.push(newEntry);
}
}
return performanceRecord;
}
if( window.bookmarklet_77fdfb2f8df9c9baba57 ){
alert( JSON.stringify(mwurzbergerGetPerformanceData(), null, 2) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment