Accumulator.html
8.21 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-perf-Accumulator'>/**
</span> * @class Ext.perf.Accumulator
* @private
*/
Ext.define('Ext.perf.Accumulator', (function () {
var currentFrame = null,
khrome = Ext.global['chrome'],
formatTpl,
// lazy init on first request for timestamp (avoids infobar in IE until needed)
// Also avoids kicking off Chrome's microsecond timer until first needed
getTimestamp = function () {
getTimestamp = function () {
return new Date().getTime();
};
var interval, toolbox;
// If Chrome is started with the --enable-benchmarking switch
if (Ext.isChrome && khrome && khrome.Interval) {
interval = new khrome.Interval();
interval.start();
getTimestamp = function () {
return interval.microseconds() / 1000;
};
} else if (window.ActiveXObject) {
try {
// the above technique is not very accurate for small intervals...
toolbox = new ActiveXObject('SenchaToolbox.Toolbox');
Ext.senchaToolbox = toolbox; // export for other uses
getTimestamp = function () {
return toolbox.milliseconds;
};
} catch (e) {
// ignore
}
} else if (Date.now) {
getTimestamp = Date.now;
}
Ext.perf.getTimestamp = Ext.perf.Accumulator.getTimestamp = getTimestamp;
return getTimestamp();
};
function adjustSet (set, time) {
set.sum += time;
set.min = Math.min(set.min, time);
set.max = Math.max(set.max, time);
}
function leaveFrame (time) {
var totalTime = time ? time : (getTimestamp() - this.time), // do this first
me = this, // me = frame
accum = me.accum;
++accum.count;
if (! --accum.depth) {
adjustSet(accum.total, totalTime);
}
adjustSet(accum.pure, totalTime - me.childTime);
currentFrame = me.parent;
if (currentFrame) {
++currentFrame.accum.childCount;
currentFrame.childTime += totalTime;
}
}
function makeSet () {
return {
min: Number.MAX_VALUE,
max: 0,
sum: 0
};
}
function makeTap (me, fn) {
return function () {
var frame = me.enter(),
ret = fn.apply(this, arguments);
frame.leave();
return ret;
};
}
function round (x) {
return Math.round(x * 100) / 100;
}
function setToJSON (count, childCount, calibration, set) {
var data = {
avg: 0,
min: set.min,
max: set.max,
sum: 0
};
if (count) {
calibration = calibration || 0;
data.sum = set.sum - childCount * calibration;
data.avg = data.sum / count;
// min and max cannot be easily corrected since we don't know the number of
// child calls for them.
}
return data;
}
return {
constructor: function (name) {
var me = this;
me.count = me.childCount = me.depth = me.maxDepth = 0;
me.pure = makeSet();
me.total = makeSet();
me.name = name;
},
statics: {
getTimestamp: getTimestamp
},
format: function (calibration) {
if (!formatTpl) {
formatTpl = new Ext.XTemplate([
'{name} - {count} call(s)',
'<tpl if="count">',
'<tpl if="childCount">',
' ({childCount} children)',
'</tpl>',
'<tpl if="depth - 1">',
' ({depth} deep)',
'</tpl>',
'<tpl for="times">',
', {type}: {[this.time(values.sum)]} msec (',
//'min={[this.time(values.min)]}, ',
'avg={[this.time(values.sum / parent.count)]}',
//', max={[this.time(values.max)]}',
')',
'</tpl>',
'</tpl>'
].join(''), {
time: function (t) {
return Math.round(t * 100) / 100;
}
});
}
var data = this.getData(calibration);
data.name = this.name;
data.pure.type = 'Pure';
data.total.type = 'Total';
data.times = [data.pure, data.total];
return formatTpl.apply(data);
},
getData: function (calibration) {
var me = this;
return {
count: me.count,
childCount: me.childCount,
depth: me.maxDepth,
pure: setToJSON(me.count, me.childCount, calibration, me.pure),
total: setToJSON(me.count, me.childCount, calibration, me.total)
};
},
enter: function () {
var me = this,
frame = {
accum: me,
leave: leaveFrame,
childTime: 0,
parent: currentFrame
};
++me.depth;
if (me.maxDepth < me.depth) {
me.maxDepth = me.depth;
}
currentFrame = frame;
frame.time = getTimestamp(); // do this last
return frame;
},
monitor: function (fn, scope, args) {
var frame = this.enter();
if (args) {
fn.apply(scope, args);
} else {
fn.call(scope);
}
frame.leave();
},
report: function () {
Ext.log(this.format());
},
tap: function (className, methodName) {
var me = this,
methods = typeof methodName == 'string' ? [methodName] : methodName,
klass, statik, i, parts, length, name, src,
tapFunc;
tapFunc = function(){
if (typeof className == 'string') {
klass = Ext.global;
parts = className.split('.');
for (i = 0, length = parts.length; i < length; ++i) {
klass = klass[parts[i]];
}
} else {
klass = className;
}
for (i = 0, length = methods.length; i < length; ++i) {
name = methods[i];
statik = name.charAt(0) == '!';
if (statik) {
name = name.substring(1);
} else {
statik = !(name in klass.prototype);
}
src = statik ? klass : klass.prototype;
src[name] = makeTap(me, src[name]);
}
};
Ext.ClassManager.onCreated(tapFunc, me, className);
return me;
}
};
}()),
function () {
Ext.perf.getTimestamp = this.getTimestamp;
});
</pre>
</body>
</html>