Authored by htoooth

add perf

var cookies = require('../cookie');
var reporter = require('./reporter');
var perfKey = '_perfLog';
var navigatorPerf = require('./perf/navigator'),
resourcePerf = require('./perf/resource'),
screenPerf = require('./perf/screen'),
timingPerf = require('./perf/timing');
var perfReporter = {
init: function() {
navigatorPerf.init();
resourcePerf.init();
screenPerf.init();
timingPerf.init();
this.report();
},
collect: function() {
var navPerfData = navigatorPerf.collect();
var screenPerfData = screenPerf.collect();
var timingPerfData = timingPerf.collect();
var resourcePerfData = resourcePerfData.collect();
this.write(navPerfData);
this.write(screenPerfData);
this.write(timingPerfData);
this.write(resourcePerfData);
},
write: function(item) {
var perfList = JSON.parse(cookies(perfKey) || '[]');
perfList.push(item);
cookies(perfKey, JSON.stringify(perfList));
if (perfList.length >= 5) {
this.report();
}
},
report: function() {
var self = this;
var perfList = JSON.parse(cookies(perfKey) || '[]');
var perfStr = reporter.stringify(perfList);
reporter.report(perfStr, function() {
self.clear();
});
},
clear: function() {
cookies(perfKey, '[]');
}
};
if (window._perfEnable_) {
perfReporter.init();
perfReporter.collect();
}
... ...
var navigatorReporter = {
init: function() {
if ( !('navigator' in window)) {
return;
}
},
collect: function() {
}
};
module.exports = navigatorReporter;
\ No newline at end of file
... ...
var resourceReporter = {
init: function() {
if (!('performance' in window) ||
!('getEntriesByType' in window.performance) ||
!(window.performance.getEntriesByType('resource') instanceof Array)
) {
document.getElementById('rt-unsupported').classList.remove('hidden');
} else {
window.addEventListener('load', function() {
var resources = window.performance.getEntriesByType('resource');
for (var obj in resources) {
var list = '';
for (var properties in resources[obj]) {
list += '<li>' + properties + ': <span class="value">' + resources[obj][properties] + '</span></li>';
}
document.getElementById(resources[obj].initiatorType + '-list').innerHTML = list;
}
});
}
},
collect: function() {
}
};
module.exports = resourceReporter;
... ...
var screenReporter = {
init: function() {
if ( !('screen' in window)) {
return;
}
},
collect: function() {
}
};
module.exports = screenReporter;
... ...
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
... ...
... ... @@ -10,7 +10,8 @@ const os = require('os');
module.exports = {
entry: {
'yas': './yas.js',
'reporter': './src/reporter/index.js'
'reporter': './src/reporter/index.js',
'perf': './src/reporter/perf-reporter.js'
},
output: {
path: path.join('dist', 'yas-jssdk', version), // absolute path
... ...