r25048: From the archives (patch found in one of my old working trees):
[jelmer/samba4-debian.git] / webapps / qooxdoo-0.6.5-sdk / frontend / framework / source / class / qx / dev / TimeTracker.js
1 /* ************************************************************************
2
3    qooxdoo - the new era of web development
4
5    http://qooxdoo.org
6
7    Copyright:
8      2004-2007 1&1 Internet AG, Germany, http://www.1and1.org
9
10    License:
11      LGPL: http://www.gnu.org/licenses/lgpl.html
12      EPL: http://www.eclipse.org/org/documents/epl-v10.php
13      See the LICENSE file in the project's top-level directory for details.
14
15    Authors:
16      * Sebastian Werner (wpbasti)
17      * Andreas Ecker (ecker)
18
19 ************************************************************************ */
20
21 /* ************************************************************************
22
23 #module(dev)
24 #embed(qx.icontheme/16/actions/dialog-ok.png)
25
26 ************************************************************************ */
27
28 qx.OO.defineClass("qx.dev.TimeTracker", qx.core.Object,
29 function()
30 {
31   qx.core.Object.call(this);
32
33   this._functions = qx.lang.Array.fromArguments(arguments);
34
35   this.buttonSets();
36 });
37
38 qx.dev.TimeTracker.compare = function(a, b) {
39   return a-b;
40 }
41
42
43
44
45
46
47 /*
48 ---------------------------------------------------------------------------
49   METHODS
50 ---------------------------------------------------------------------------
51 */
52
53 qx.Proto.buttonSets = function()
54 {
55   var btnLayout = new qx.ui.layout.HorizontalBoxLayout;
56
57   btnLayout.setLocation(20, 48);
58   btnLayout.setSpacing(5);
59
60   var loopLabel = new qx.ui.basic.Atom("Method Loops: ");
61   loopLabel.setAllowStretchY(false);
62   loopLabel.setVerticalAlign("middle");
63
64   var loopInput = new qx.ui.form.TextField("100");
65   loopInput.setAllowStretchY(false);
66   loopInput.setWidth(50);
67   loopInput.setVerticalAlign("middle");
68
69   var repeatLabel = new qx.ui.basic.Atom("Repeat Number: ");
70   repeatLabel.setAllowStretchY(false);
71   repeatLabel.setVerticalAlign("middle");
72   repeatLabel.setMarginLeft(30);
73
74   var btnStart1 = new qx.ui.form.Button("Start 3x", "icon/16/actions/dialog-ok.png");
75   var btnStart2 = new qx.ui.form.Button("Start 7x", "icon/16/actions/dialog-ok.png");
76   var btnStart3 = new qx.ui.form.Button("Start 15x", "icon/16/actions/dialog-ok.png");
77   var btnStart4 = new qx.ui.form.Button("Start 25x", "icon/16/actions/dialog-ok.png");
78
79   btnStart1.addEventListener("execute", function() { this.start(3, parseInt(loopInput.getValue())); }, this);
80   btnStart2.addEventListener("execute", function() { this.start(7, parseInt(loopInput.getValue())); }, this);
81   btnStart3.addEventListener("execute", function() { this.start(15, parseInt(loopInput.getValue())); }, this);
82   btnStart4.addEventListener("execute", function() { this.start(25, parseInt(loopInput.getValue())); }, this);
83
84   var htmlOutput = this._output = new qx.ui.embed.HtmlEmbed();
85
86   htmlOutput.setHtml("");
87   htmlOutput.setLocation(20, 78);
88   htmlOutput.setRight(335);
89   htmlOutput.setBottom(48);
90   htmlOutput.setBorder("1px solid black");
91   htmlOutput.setBackgroundColor("white");
92   htmlOutput.setPadding(10);
93   htmlOutput.setOverflow("auto");
94   htmlOutput.addToDocument();
95
96   btnLayout.add(loopLabel, loopInput, repeatLabel, btnStart1, btnStart2, btnStart3, btnStart4);
97   btnLayout.addToDocument();
98 }
99
100 qx.Proto.start = function(vRounds, vLoops)
101 {
102   var vFuncs = this._functions;
103   var vLength = vFuncs.length;
104   var vStart;
105   var vLocalTimes;
106   var vAllTimes = [];
107   var vHtmlMeasured = [];
108   var vHtmlResults = [];
109   var vCellWidth = Math.round(100 / (vLength+1)) + "%";
110
111   vHtmlMeasured.push("<h3>Measured Values</h3>");
112
113   vHtmlMeasured.push("<style type='text/css'>.output{border: 1px solid black; width:100%; margin-bottom: 20px } .output thead{ font-weight: bold; } .output td, .output th{ text-align:left; width: " + vCellWidth + "; } .output td{padding:4px}</style>");
114
115   vHtmlMeasured.push("<table class='output'>");
116
117   vHtmlMeasured.push("<thead>");
118
119   vHtmlMeasured.push("<tr><td>&#160;</td>");
120
121   for (var j=0; j<vLength; j++) {
122     vHtmlMeasured.push("<td>Method " + (j+1) + "</td>");
123   }
124
125   vHtmlMeasured.push("</thead><tbody>");
126
127   for (var i=0; i<vRounds; i++)
128   {
129     vLocalTimes = [];
130
131     for (var j=0; j<vLength; j++)
132     {
133       vStart = (new Date).valueOf();
134
135       vFuncs[j](vLoops);
136
137       vLocalTimes.push((new Date).valueOf()-vStart);
138     }
139
140     vHtmlMeasured.push("<tr><th>Round " + i + "</th>");
141
142     for (var j=0; j<vLocalTimes.length; j++) {
143       vHtmlMeasured.push("<td>" + vLocalTimes[j] + "</td>");
144     }
145
146     vHtmlMeasured.push("</tr>");
147     vAllTimes.push(vLocalTimes);
148   }
149
150   vHtmlMeasured.push("</tbody></table>");
151
152
153
154
155
156   var vSum, vMeanValue, vMeanAll=[], vMeanMin=1e7, vMeanMax=0;
157
158   for (var j=0; j<vLength; j++)
159   {
160     vSum = 0;
161
162     for (var i=0; i<vRounds; i++)
163     {
164       vSum += vAllTimes[i][j];
165     }
166
167     vMeanValue = Math.round(vSum / vRounds);
168
169     vMeanAll.push(vMeanValue);
170
171     vMeanMin = Math.min(vMeanMin, vMeanValue);
172     vMeanMax = Math.max(vMeanMax, vMeanValue);
173   }
174
175
176
177   var vMedian, vMedianValue, vMedianAll=[], vMedianMin=1e7, vMedianMax=0;
178
179   for (var j=0; j<vLength; j++)
180   {
181     vMedian = [];
182
183     for (var i=0; i<vRounds; i++)
184     {
185       vMedian.push(vAllTimes[i][j]);
186     }
187
188     vMedian.sort(qx.dev.TimeTracker.compare);
189     vMedianValue = vMedian[Math.floor(vRounds / 2)].toString();
190
191     vMedianAll.push(vMedianValue);
192
193     vMedianMin = Math.min(vMedianValue, vMedianMin);
194     vMedianMax = Math.max(vMedianValue, vMedianMax);
195   }
196
197
198
199
200
201   vHtmlResults.push("<h3>Results Summary</h3>");
202
203   vHtmlResults.push("<table class='output'>");
204
205   vHtmlResults.push("<thead>");
206
207   vHtmlResults.push("<tr><td>&#160;</td>");
208
209   for (var j=0; j<vLength; j++) {
210     vHtmlResults.push("<td>Method " + (j+1) + "</td>");
211   }
212
213   vHtmlResults.push("</thead><tbody>");
214
215
216   vHtmlResults.push("<tr>");
217
218   vHtmlResults.push("<th>Median</th>");
219
220   for (var j=0; j<vLength; j++) {
221     vHtmlResults.push("<td>" + vMedianAll[j] + "</td>");
222   }
223
224   vHtmlResults.push("</tr>");
225
226
227
228   vHtmlResults.push("<tr>");
229
230   vHtmlResults.push("<th>Median Factor</th>");
231
232   for (var j=0; j<vLength; j++)
233   {
234     vHtmlResults.push("<td>");
235     vHtmlResults.push(vMedianMin > 0 ? Math.round(vMedianAll[j] / vMedianMin) : "1");
236     vHtmlResults.push("x</td>");
237   }
238
239   vHtmlResults.push("</tr>");
240
241
242
243   vHtmlResults.push("<tr>");
244
245   vHtmlResults.push("<th>Mean</th>");
246
247   for (var j=0; j<vLength; j++) {
248     vHtmlResults.push("<td>" + vMeanAll[j] + "</td>");
249   }
250
251   vHtmlResults.push("</tr>");
252
253
254
255   vHtmlResults.push("<tr>");
256
257   vHtmlResults.push("<th>Mean Factor</th>");
258
259   for (var j=0; j<vLength; j++)
260   {
261     vHtmlResults.push("<td>");
262     vHtmlResults.push(vMeanMin > 0 ? Math.round(vMeanAll[j] / vMeanMin) : 1);
263     vHtmlResults.push("x</td>");
264   }
265
266   vHtmlResults.push("</tr>");
267
268
269
270   vHtmlResults.push("<tr>");
271
272   vHtmlResults.push("<th>Winner</th>");
273
274   for (var j=0; j<vLength; j++)
275   {
276     vHtmlResults.push("<td>");
277
278     if (vMedianMin == vMedianAll[j] && vMeanMin == vMeanAll[j])
279     {
280       vHtmlResults.push("BOTH");
281     }
282
283     else if (vMedianMin == vMedianAll[j])
284     {
285       vHtmlResults.push("MEDIAN");
286     }
287
288     else if (vMeanMin == vMeanAll[j])
289     {
290       vHtmlResults.push("MEAN");
291     }
292
293     vHtmlResults.push("</td>");
294   }
295
296   vHtmlResults.push("</tr>");
297
298   vHtmlResults.push("</tbody></table>");
299
300   this._output.setHtml(vHtmlResults.join("") + vHtmlMeasured.join(""));
301 }
302
303
304
305
306
307
308 /*
309 ---------------------------------------------------------------------------
310   DISPOSER
311 ---------------------------------------------------------------------------
312 */
313
314 qx.Proto.dispose = function()
315 {
316   if (this.getDisposed()) {
317     return;
318   }
319
320   this._functions = null;
321
322   return qx.core.Object.prototype.dispose.call(this);
323 }