|
|
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 |
...
|
...
|
|