]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * @singleton\r | |
3 | * @private\r | |
4 | */\r | |
5 | Ext.define('Ext.perf.Monitor', {\r | |
6 | singleton: true,\r | |
7 | alternateClassName: 'Ext.Perf',\r | |
8 | \r | |
9 | requires: [\r | |
10 | 'Ext.perf.Accumulator'\r | |
11 | ],\r | |
12 | \r | |
13 | constructor: function () {\r | |
14 | this.accumulators = [];\r | |
15 | this.accumulatorsByName = {};\r | |
16 | },\r | |
17 | \r | |
18 | calibrate: function () {\r | |
19 | var accum = new Ext.perf.Accumulator('$'),\r | |
20 | total = accum.total,\r | |
21 | getTimestamp = Ext.perf.Accumulator.getTimestamp,\r | |
22 | count = 0,\r | |
23 | frame,\r | |
24 | endTime,\r | |
25 | startTime;\r | |
26 | \r | |
27 | startTime = getTimestamp();\r | |
28 | \r | |
29 | do {\r | |
30 | frame = accum.enter();\r | |
31 | frame.leave();\r | |
32 | ++count;\r | |
33 | } while (total.sum < 100);\r | |
34 | \r | |
35 | endTime = getTimestamp();\r | |
36 | \r | |
37 | return (endTime - startTime) / count;\r | |
38 | },\r | |
39 | \r | |
40 | get: function (name) {\r | |
41 | var me = this,\r | |
42 | accum = me.accumulatorsByName[name];\r | |
43 | \r | |
44 | if (!accum) {\r | |
45 | me.accumulatorsByName[name] = accum = new Ext.perf.Accumulator(name);\r | |
46 | me.accumulators.push(accum);\r | |
47 | }\r | |
48 | \r | |
49 | return accum;\r | |
50 | },\r | |
51 | \r | |
52 | enter: function (name) {\r | |
53 | return this.get(name).enter();\r | |
54 | },\r | |
55 | \r | |
56 | monitor: function (name, fn, scope) {\r | |
57 | this.get(name).monitor(fn, scope);\r | |
58 | },\r | |
59 | \r | |
60 | report: function () {\r | |
61 | var me = this,\r | |
62 | accumulators = me.accumulators,\r | |
63 | calibration = me.calibrate();\r | |
64 | \r | |
65 | accumulators.sort(function (a, b) {\r | |
66 | return (a.name < b.name) ? -1 : ((b.name < a.name) ? 1 : 0);\r | |
67 | });\r | |
68 | \r | |
69 | me.updateGC();\r | |
70 | \r | |
71 | Ext.log('Calibration: ' + Math.round(calibration * 100) / 100 + ' msec/sample');\r | |
72 | Ext.each(accumulators, function (accum) {\r | |
73 | Ext.log(accum.format(calibration));\r | |
74 | });\r | |
75 | },\r | |
76 | \r | |
77 | getData: function (all) {\r | |
78 | var ret = {},\r | |
79 | accumulators = this.accumulators;\r | |
80 | \r | |
81 | Ext.each(accumulators, function (accum) {\r | |
82 | if (all || accum.count) {\r | |
83 | ret[accum.name] = accum.getData();\r | |
84 | }\r | |
85 | });\r | |
86 | \r | |
87 | return ret;\r | |
88 | },\r | |
89 | \r | |
90 | reset: function(){\r | |
91 | Ext.each(this.accumulators, function(accum){\r | |
92 | var me = accum;\r | |
93 | me.count = me.childCount = me.depth = me.maxDepth = 0;\r | |
94 | me.pure = {\r | |
95 | min: Number.MAX_VALUE,\r | |
96 | max: 0,\r | |
97 | sum: 0\r | |
98 | };\r | |
99 | me.total = {\r | |
100 | min: Number.MAX_VALUE,\r | |
101 | max: 0,\r | |
102 | sum: 0\r | |
103 | };\r | |
104 | });\r | |
105 | },\r | |
106 | \r | |
107 | updateGC: function () {\r | |
108 | var accumGC = this.accumulatorsByName.GC,\r | |
109 | toolbox = Ext.senchaToolbox,\r | |
110 | bucket;\r | |
111 | \r | |
112 | if (accumGC) {\r | |
113 | accumGC.count = toolbox.garbageCollectionCounter || 0;\r | |
114 | \r | |
115 | if (accumGC.count) {\r | |
116 | bucket = accumGC.pure;\r | |
117 | accumGC.total.sum = bucket.sum = toolbox.garbageCollectionMilliseconds;\r | |
118 | bucket.min = bucket.max = bucket.sum / accumGC.count;\r | |
119 | bucket = accumGC.total;\r | |
120 | bucket.min = bucket.max = bucket.sum / accumGC.count;\r | |
121 | }\r | |
122 | }\r | |
123 | },\r | |
124 | \r | |
125 | watchGC: function () {\r | |
126 | Ext.perf.getTimestamp(); // initializes SenchaToolbox (if available)\r | |
127 | \r | |
128 | var toolbox = Ext.senchaToolbox;\r | |
129 | \r | |
130 | if (toolbox) {\r | |
131 | this.get("GC");\r | |
132 | toolbox.watchGarbageCollector(false); // no logging, just totals\r | |
133 | }\r | |
134 | },\r | |
135 | \r | |
136 | setup: function (config) {\r | |
137 | if (!config) {\r | |
138 | config = {\r | |
139 | /*insertHtml: {\r | |
140 | 'Ext.dom.Helper': 'insertHtml'\r | |
141 | },*/\r | |
142 | /*xtplCompile: {\r | |
143 | 'Ext.XTemplateCompiler': 'compile'\r | |
144 | },*/\r | |
145 | // doInsert: {\r | |
146 | // 'Ext.Template': 'doInsert'\r | |
147 | // },\r | |
148 | // applyOut: {\r | |
149 | // 'Ext.XTemplate': 'applyOut'\r | |
150 | // },\r | |
151 | render: {\r | |
152 | 'Ext.Component': 'render'\r | |
153 | },\r | |
154 | // fnishRender: {\r | |
155 | // 'Ext.Component': 'finishRender'\r | |
156 | // },\r | |
157 | // renderSelectors: {\r | |
158 | // 'Ext.Component': 'applyRenderSelectors'\r | |
159 | // },\r | |
160 | // compAddCls: {\r | |
161 | // 'Ext.Component': 'addCls'\r | |
162 | // },\r | |
163 | // compRemoveCls: {\r | |
164 | // 'Ext.Component': 'removeCls'\r | |
165 | // },\r | |
166 | // getStyle: {\r | |
167 | // 'Ext.core.Element': 'getStyle'\r | |
168 | // },\r | |
169 | // setStyle: {\r | |
170 | // 'Ext.core.Element': 'setStyle'\r | |
171 | // },\r | |
172 | // addCls: {\r | |
173 | // 'Ext.core.Element': 'addCls'\r | |
174 | // },\r | |
175 | // removeCls: {\r | |
176 | // 'Ext.core.Element': 'removeCls'\r | |
177 | // },\r | |
178 | // measure: {\r | |
179 | // 'Ext.layout.component.Component': 'measureAutoDimensions'\r | |
180 | // },\r | |
181 | // moveItem: {\r | |
182 | // 'Ext.layout.Layout': 'moveItem'\r | |
183 | // },\r | |
184 | // layoutFlush: {\r | |
185 | // 'Ext.layout.Context': 'flush'\r | |
186 | // },\r | |
187 | layout: {\r | |
188 | 'Ext.layout.Context': 'run'\r | |
189 | }\r | |
190 | };\r | |
191 | }\r | |
192 | \r | |
193 | this.currentConfig = config;\r | |
194 | \r | |
195 | var key, prop,\r | |
196 | accum, className, methods;\r | |
197 | for (key in config) {\r | |
198 | if (config.hasOwnProperty(key)) {\r | |
199 | prop = config[key];\r | |
200 | accum = Ext.Perf.get(key);\r | |
201 | \r | |
202 | for (className in prop) {\r | |
203 | if (prop.hasOwnProperty(className)) {\r | |
204 | methods = prop[className];\r | |
205 | accum.tap(className, methods);\r | |
206 | }\r | |
207 | }\r | |
208 | }\r | |
209 | }\r | |
210 | \r | |
211 | this.watchGC();\r | |
212 | },\r | |
213 | \r | |
214 | // This is a quick hack for now\r | |
215 | setupLog: function(config) {\r | |
216 | var className, cls, methods, method, override;\r | |
217 | \r | |
218 | for (className in config) {\r | |
219 | if (config.hasOwnProperty(className)) {\r | |
220 | cls = Ext.ClassManager.get(className);\r | |
221 | \r | |
222 | if (cls) {\r | |
223 | methods = config[className];\r | |
224 | \r | |
225 | override = {};\r | |
226 | \r | |
227 | for (method in methods) {\r | |
228 | override[method] = (function(methodName, idProp) {\r | |
229 | return function() {\r | |
230 | var before, diff, id, idHolder, ret;\r | |
231 | \r | |
232 | before = +Date.now();\r | |
233 | ret = this.callParent(arguments);\r | |
234 | diff = +Date.now() - before;\r | |
235 | \r | |
236 | if (window.console && diff > 0) {\r | |
237 | idHolder = idProp === 'this' ? this\r | |
238 | : typeof idProp === 'string' ? this[idProp]\r | |
239 | : typeof idProp === 'number' ? arguments[idProp]\r | |
240 | : null\r | |
241 | ;\r | |
242 | \r | |
243 | if (idHolder) {\r | |
244 | id = idHolder.id;\r | |
245 | }\r | |
246 | \r | |
247 | if (id != null) {\r | |
248 | console.log(methodName + ' for ' + id + ': ' + diff + 'ms');\r | |
249 | }\r | |
250 | else {\r | |
251 | console.log(methodName + ' for unknown: ' + diff + 'ms');\r | |
252 | }\r | |
253 | \r | |
254 | if (console.trace) {\r | |
255 | console.trace();\r | |
256 | }\r | |
257 | }\r | |
258 | \r | |
259 | return ret;\r | |
260 | }\r | |
261 | })(method, methods[method]);\r | |
262 | }\r | |
263 | \r | |
264 | Ext.override(cls, override);\r | |
265 | }\r | |
266 | }\r | |
267 | }\r | |
268 | }\r | |
269 | });\r |