|
|
var timingReporter = function() {
|
|
|
var loggerPool = [],
|
|
|
_pTime = Date.now() - performance.timing.navigationStart || 0,
|
|
|
_redirTime = performance.timing.redirectEnd - performance.timing.redirectStart || 0,
|
|
|
_cacheTime = performance.timing.domainLookupStart - performance.timing.fetchStart || 0,
|
|
|
_dnsTime = performance.timing.domainLookupEnd - performance.timing.domainLookupStart || 0,
|
|
|
_tcpTime = performance.timing.connectEnd - performance.timing.connectStart || 0,
|
|
|
_roundtripTime = performance.timing.responseEnd - performance.timing.connectStart || 0,
|
|
|
_renderTime = Date.now() - performance.timing.domLoading || 0;
|
|
|
|
|
|
function TestResults() {
|
|
|
};
|
|
|
TestResults.prototype.perceivedTime = _pTime;
|
|
|
TestResults.prototype.redirectTime = _redirTime;
|
|
|
TestResults.prototype.cacheTime = _cacheTime;
|
|
|
TestResults.prototype.dnsLookupTime = _dnsTime;
|
|
|
TestResults.prototype.tcpConnectionTime = _tcpTime;
|
|
|
TestResults.prototype.roundTripTime = _roundtripTime;
|
|
|
TestResults.prototype.pageRenderTime = _renderTime;
|
|
|
|
|
|
function jsonConcat(object1, object2) {
|
|
|
for (var key in object2) {
|
|
|
object1[key] = object2[key];
|
|
|
}
|
|
|
return object1;
|
|
|
}
|
|
|
|
|
|
function setResultsMetaData(id) {
|
|
|
loggerPool[id].url = encodeURIComponent(window.location.href);
|
|
|
loggerPool[id].useragent = encodeURIComponent(navigator.userAgent);
|
|
|
}
|
|
|
|
|
|
function drawToDebugScreen(id) {
|
|
|
var debug = document.getElementById("debug");
|
|
|
var output = formatDebugInfo(id);
|
|
|
if (!debug) {
|
|
|
var divTag = document.createElement("div");
|
|
|
divTag.id = "debug";
|
|
|
divTag.innerHTML = output;
|
|
|
document.body.appendChild(divTag);
|
|
|
} else {
|
|
|
debug.innerHTML += output;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
function formatDebugInfo(id) {
|
|
|
var debuginfo = "<p>";
|
|
|
debuginfo += "pref object : " + JSON.stringify(jsonConcat(loggerPool[id], TestResults.prototype)) + "<br/>";
|
|
|
|
|
|
debuginfo += "path: " + decodeURIComponent(loggerPool[id].url) + "<br/>";
|
|
|
debuginfo += "useragent: " + decodeURIComponent(loggerPool[id].useragent) + "<br/>";
|
|
|
debuginfo += "</p>";
|
|
|
return debuginfo
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
startTimeLogging: function(id) {
|
|
|
loggerPool[id] = new TestResults();
|
|
|
loggerPool[id].id = id;
|
|
|
loggerPool[id].startTime = performance.now();
|
|
|
},
|
|
|
|
|
|
stopTimeLogging: function(id) {
|
|
|
loggerPool[id].stopTime = performance.now();
|
|
|
setResultsMetaData(id);
|
|
|
},
|
|
|
|
|
|
collect: function() {
|
|
|
var id = Math.random();
|
|
|
|
|
|
this.id = id;
|
|
|
this.startTimeLogging(id);
|
|
|
this.stopTimeLogging(id);
|
|
|
return jsonConcat(loggerPool[id],TestResults.prototype);
|
|
|
},
|
|
|
|
|
|
debug: function() {
|
|
|
drawToDebugScreen(this.id);
|
|
|
},
|
|
|
|
|
|
//expose derived performance data
|
|
|
perceivedTime: function() {
|
|
|
return _pTime;
|
|
|
},
|
|
|
redirectTime: function() {
|
|
|
_redirTime;
|
|
|
},
|
|
|
cacheTime: function() {
|
|
|
return _cacheTime;
|
|
|
},
|
|
|
dnsLookupTime: function() {
|
|
|
return _dnsTime;
|
|
|
},
|
|
|
tcpConnectionTime: function() {
|
|
|
return _tcpTime;
|
|
|
},
|
|
|
roundTripTime: function() {
|
|
|
return _roundtripTime;
|
|
|
},
|
|
|
pageRenderTime: function() {
|
|
|
return _renderTime;
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
performance.now = (function() {
|
|
|
return performance.now ||
|
|
|
performance.mozNow ||
|
|
|
performance.msNow ||
|
|
|
performance.oNow ||
|
|
|
performance.webkitNow ||
|
|
|
function() {
|
|
|
return new Date().getTime();
|
|
|
};
|
|
|
})();
|
|
|
|
|
|
var reporter = {
|
|
|
init: function() {
|
|
|
this.enable = true;
|
|
|
if (!('performance' in window)) {
|
|
|
this.enable = false;
|
|
|
return;
|
|
|
}
|
|
|
},
|
|
|
collect: function(debug) {
|
|
|
if (!this.enable) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
var perf = timingReporter();
|
|
|
var data = perf.collect();
|
|
|
|
|
|
if (debug) {
|
|
|
perf.debug();
|
|
|
}
|
|
|
|
|
|
return data;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
module.exports = reporter; |
|
|
\ No newline at end of file |
...
|
...
|
|