Authored by htoooth

feature/perf

{
"name": "yas-jssdk",
"version": "2.4.15",
"version": "2.4.16",
"description": "YOHO!采集系统的前端js的开发包",
"keywords": [
"YOHO!",
... ... @@ -42,7 +42,7 @@
"babel-runtime": "^6.20.0",
"css-loader": "^0.26.1",
"expect.js": "^0.3.1",
"gulp": "^3.9.1",
"gulp": "^3.8.11",
"gulp-ftp": "^1.0.3",
"gulp-jshint": "^1.9.2",
"gulp-util": "^3.0.4",
... ... @@ -58,6 +58,6 @@
"scripts": {
"build": "gulp build",
"cp": "gulp cp",
"start": "node server/app.js"
"start:dev": "node server/app.js"
}
}
... ...
... ... @@ -61,6 +61,6 @@ function error(err, req, res, next)
res.send('Internal Server Error');
}
app.listen(8080, function() {
console.log('testserver at 8080');
app.listen(8081, function() {
console.log('testserver at http://localhost:8081/');
});
\ No newline at end of file
... ...
... ... @@ -10,7 +10,7 @@ var perfReporter = {
init: function(debug) {
this.debug = debug;
this.dataList = [];
this._isRunning = false;
this.isRunning = false;
resourcePerf.init(this.debug);
timingPerf.init(this.debug);
... ... @@ -46,7 +46,6 @@ var perfReporter = {
getMeta: function() {
return {
pt: encodeURIComponent(window.location.href), // url
ua: encodeURIComponent(navigator.userAgent), // useragent
u: reporter.getUid(), // uid
ud: reporter.getUdid(), // udid
rid: reporter.getReqId(), // reqId
... ... @@ -62,10 +61,9 @@ var perfReporter = {
},
write: function(item) {
var perfList = JSON.parse(cookies(perfKey) || '[]');
this.jsonConcat(item, this.getMeta());
var perfList = JSON.parse(cookies(perfKey) || '[]');
perfList.push(item);
cookies(perfKey, JSON.stringify(perfList));
... ... @@ -92,11 +90,11 @@ var perfReporter = {
var self = this;
this.dataList.push(item);
if (this._isRunning) {
if (this.isRunning) {
return;
}
this._isRunning = true;
this.isRunning = true;
function _write() {
if (self.dataList.length === 0) {
... ... @@ -113,6 +111,4 @@ var perfReporter = {
}
};
if (window._perfEnable_) {
perfReporter.init(true);
}
perfReporter.init();
... ...
var resourceReporter = {
init: function(debug) {
init: function (debug) {
this.enable = true;
this.debug = debug;
... ... @@ -11,10 +11,10 @@ var resourceReporter = {
return;
}
this.whiteList = [/^https:\/\/img\d*.static.yhbimg.com/, /^https:\/\/cdn.yoho.cn/];
this.whiteList = [/\/\/img\d*.static.yhbimg.com/, /\/\/cdn.yoho.cn/];
},
print: function() {
print: function () {
var debug = document.getElementById("debug");
var output = this.formatDebugInfo();
... ... @@ -28,7 +28,7 @@ var resourceReporter = {
}
},
formatDebugInfo: function() {
formatDebugInfo: function () {
var debuginfo = "<p>resource object: <br/>";
var data = this.getData();
... ... @@ -39,7 +39,7 @@ var resourceReporter = {
return debuginfo
},
filter: function(url) {
filter: function (url) {
for (var i = 0; i < this.whiteList.length; i++) {
if (this.whiteList[i].test(url)) {
return true;
... ... @@ -49,9 +49,10 @@ var resourceReporter = {
return false;
},
getData: function() {
getData: function () {
var resources = window.performance.getEntriesByType('resource');
var resourceList = [];
var resourceMap = {};
for (var obj in resources) {
var domObj = resources[obj];
... ... @@ -60,24 +61,59 @@ var resourceReporter = {
continue;
}
resourceList.push({
var data = {
tp: 'resource', // 资源
dtp: domObj.entryType, // 资源
id: domObj.name || 'unknown',  // 资源 url
rtp: domObj.initiatorType || 'unknown', // 资源类型 img script
mtp: domObj.initiatorType || 'unknown', // mineType
id: domObj.name || 'unknown', // 资源 url
dt: Math.floor(domObj.domainLookupEnd - domObj.domainLookupStart || 0), // DNS 时间
tt: Math.floor(domObj.connectEnd - domObj.connectStart || 0), // tcp 时间
rt: Math.floor(domObj.responseEnd - domObj.responseStart || 0), // 下载 时间
rrt: Math.floor(domObj.responseEnd - domObj.fetchStart || 0) // 总时间
})
tt: Math.floor(domObj.connectEnd - domObj.connectStart || 0), // tcp 时间
rt: Math.floor(domObj.responseEnd - domObj.responseStart || 0), // 下载 时间
rrt: Math.floor(domObj.responseEnd - domObj.fetchStart || 0) // 总时间
};
if (domObj.initiatorType in resourceMap) {
resourceMap[domObj.initiatorType].push(data);
} else {
resourceMap[domObj.initiatorType] = [data];
}
}
for (var i in resourceMap) {
var typeList = resourceMap[i];
var count = typeList.length;
var avgObj = {
tp: 'rs', // 资源
mtp: i,
dt: 0,
tt: 0,
rt: 0,
rrt: 0
}
for (var t = 0; t < typeList.length; t++) {
var tObj = typeList[t];
avgObj.dt += tObj.dt;
avgObj.tt += tObj.tt;
avgObj.rt += tObj.rt;
avgObj.rrt += tObj.rrt;
}
avgObj.dt = Math.floor(avgObj.dt / count);
avgObj.tt = Math.floor(avgObj.tt / count);
avgObj.rt = Math.floor(avgObj.rt / count);
avgObj.rrt = Math.floor(avgObj.rrt / count);
resourceList.push(avgObj);
}
return resourceList;
},
collect: function() {
collect: function () {
if (!this.enable) {
return;
return [];
}
if (this.debug) {
... ... @@ -88,4 +124,4 @@ var resourceReporter = {
}
};
module.exports = resourceReporter;
module.exports = resourceReporter;
\ No newline at end of file
... ...
... ... @@ -9,17 +9,17 @@ var timingReporter = function() {
function getResults() {
return {
tp: 'timing',
dtp: 'html',
tp: 'tm',
mtp: 'html', // mineType
dt: Math.floor(_dnsTime), // DNS 时间
tt: Math.floor(_tcpTime), // TCP 建立时间
rt: Math.floor(_responseTime), // request 建立时间 // 下载时间
rt: Math.floor(_responseTime), // response 建立时间 // 下载时间
domt: Math.floor(_parseDomTime), // 解析 dom 树时间
et: Math.floor(_emptyScreenTime), // 白屏时间,用户可见, start render
ot: Math.floor(_domReadyTime), // 用户可交互时间, start interact
rtt: Math.floor(_roundtripTime), // 总时间, start page load
wd: screen.width || 1, // 屏幕宽
hg: screen.height || 1, // 屏幕高
sw: screen.width || 1, // 屏幕宽
sh: screen.height || 1, // 屏幕高
pf: navigator.platform || 'unknown' // 系统
};
}
... ...