-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplePerformance.js
111 lines (92 loc) · 3.32 KB
/
simplePerformance.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var Performance = function() {
var pub = {};
var pri = {};
//If we are running in some modern browsers we can test with performance.now() otherwise use node.js perf lib (npm install performance-now)
if (typeof performance != "undefined") {
pri.perfFunction = performance.now;
} else {
var now = require("performance-now");
pri.perfFunction = now;
}
pub.monitors = {};
pub.wrap = function(name, fxn) {
pri.startMonitor(name);
var ret = fxn();
pri.endMonitor(name);
return ret;
}
pri.startMonitor = function(name) {
//Create an object if it doesn't exist
if (typeof pub.monitors[name] == "undefined") pub.monitors[name] = {};
//Start log
pub.monitors[name].lastStart = pri.perfFunction();
}
pri.endMonitor = function(name) {
var mon = pub.monitors[name];
//Create array if it doesn't exist
if (typeof mon.times == "undefined") mon.times = [];
//End log
var lastTime = pri.perfFunction() - mon.lastStart;
mon.times.push(lastTime);
pri.calculateMonitor(mon);
//If performance seems odd, show user
var lastTimeFormat = (lastTime>10000) ? Math.round(lastTime)+" ms" : Math.round(lastTime*1000)+" μs";
var sway = name+": sway="+mon.lastSway+"% @ "+lastTimeFormat;
if (mon.lastSway > 150) {
console.warn(sway);
} else if (mon.lastSway > 400) {
console.warn(sway);
}
}
pri.mean = function(elmt) {
var sum = 0;
for( var i = 0, l = elmt.length; i<l; i++ ){
sum += elmt[i];
}
return sum/elmt.length;
}
pri.calculateMonitor = function(mon){
mon.avg = pri.mean(mon.times)*1000;
mon.min = Math.min.apply(null,mon.times)*1000;
mon.max = Math.max.apply(null,mon.times)*1000;
console.log(mon.times);
mon.sway = Math.round(((mon.max-mon.min)/mon.avg)*100);
var lastTime = mon.times[mon.times.length-1]*1000;
mon.lastSway = Math.round(((lastTime-mon.min)/mon.avg)*100);
}
pub.calculate = function(){
for (i in pub.monitors) {
var mon = pub.monitors[i];
pri.calculateMonitor(mon);
};
}
pub.display = function(){
pub.calculate();
console.log("------------------------------------");
console.log(" PERFORMANCE DATA ");
console.log("------------------------------------");
for (i in pub.monitors) {
var mon = pub.monitors[i];
console.log("------------------------------------");
console.log("Action: "+i);
var avgTime = (mon.avg>10000) ? Math.round(mon.avg/1000)+" ms" : Math.round(mon.avg)+" μs";
var minTime = (mon.min>10000) ? Math.round(mon.min/1000)+" ms" : Math.round(mon.min)+" μs";
var maxTime = (mon.max>10000) ? Math.round(mon.max/1000)+" ms" : Math.round(mon.max)+" μs";
var sway = "Sway: "+mon.sway+"%";
console.log("Avg Time: "+avgTime);
console.log("Min Time: "+minTime);
console.log("Max Time: "+maxTime);
if (mon.sway < 150) {
console.log(sway);
} else if (mon.sway < 400){
console.warn(sway);
} else {
console.error(sway);
}
console.log("Occurences: "+mon.times.length)
};
console.log("------------------------------------");
return null;
}
return pub;
}();