perf-reporter.js
1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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();
}