var Profiler function component Profiler data Profiler index this comp

 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
var Profiler = function(component) {
Profiler.data[++Profiler.index] = {};
this.component = component;
this.timers = {};
this.index = {};
for (var key in this.component) {
if (typeof this.component[key] == 'function') {
var that = this;
(function(methodName) {
that[methodName] = function() {
that.startTimer(methodName);
var returnValue;
try {
returnValue = that.component[methodName].apply(that.component, arguments);
} catch(e){
console.log(e);
}
var time = that.getElapsedTime(methodName);
that.saveTime(methodName, time);
return returnValue;
};
})(key);
}
}
}
Profiler.prototype = {
startTimer: function(methodName) {
this.timers[methodName] = (new Date()).getTime();
},
getElapsedTime: function(methodName) {
return ((new Date()).getTime() - this.timers[methodName]);
},
saveTime: function(methodName, time) {
this.index[methodName] || (this.index[methodName] = {
time: [],
count: 0
});
this.index[methodName].count++;
this.index[methodName].time.push(time);
Profiler.data[Profiler.index][methodName] = this.index[methodName];
}
}
Profiler.win = null;
Profiler.data = [];
Profiler.index = -1;
Profiler.show = function() {
function td(s) {return "<td>" + s + "</td>";}
if (!Profiler.win || Profiler.win.closed)
Profiler.win = window.open("about:blank", "profiler", "status=0,title=0,dependent=yes,resizable=yes,scrollbars=yes,width=600,height=400", true);
var doc = Profiler.win.document, i = -1, data;
doc.write("<table border=1 cellpadding=3 cellspacing=5>");
doc.write("<tr><td width=200>Function</td><td>Time(ms)</td><td>Min(ms)</td><td>Max(ms)</td><td>Avg(ms)</td><td>Calls</td></tr>");
while (data = Profiler.data[++i]) {
var ttm = 0, ttmx = 0,tta = 0,tlt = 0, tc = 0;
for (var k in data) {
doc.write("<tr>");
var d = data[k], t = d.time, c = d.count;
var lt = t[t.length - 1];
var mt = t.min(), mxt = t.max(), avg = parseFloat(t.sum() / c, 2);
ttm += mt, ttmx += mxt, tta += avg, tlt += lt, tc += c;
doc.write(td(k) + td(lt) + td(mt) + td(mxt) + td(avg) + td(c));
doc.write("</tr>");
}
doc.write("<tr>");
doc.write(td('total') + td(tlt) + td(ttm) + td(ttmx) + td(tta) + td(tc));
doc.write("</tr><tr><td colspan=6 bgcolor='#aaaaaa'>- next -</td></tr>");
}
doc.write("</table>");
Profiler.data = [];
Profiler.win.onblur = function() {
this.close();
}
}