summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/dev/TimeTracker.js
blob: 15d09af537fd9a7dcad91b14b596bee49fa36cd3 (plain)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2004-2007 1&1 Internet AG, Germany, http://www.1and1.org

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     EPL: http://www.eclipse.org/org/documents/epl-v10.php
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Sebastian Werner (wpbasti)
     * Andreas Ecker (ecker)

************************************************************************ */

/* ************************************************************************

#module(dev)
#embed(qx.icontheme/16/actions/dialog-ok.png)

************************************************************************ */

qx.OO.defineClass("qx.dev.TimeTracker", qx.core.Object,
function()
{
  qx.core.Object.call(this);

  this._functions = qx.lang.Array.fromArguments(arguments);

  this.buttonSets();
});

qx.dev.TimeTracker.compare = function(a, b) {
  return a-b;
}






/*
---------------------------------------------------------------------------
  METHODS
---------------------------------------------------------------------------
*/

qx.Proto.buttonSets = function()
{
  var btnLayout = new qx.ui.layout.HorizontalBoxLayout;

  btnLayout.setLocation(20, 48);
  btnLayout.setSpacing(5);

  var loopLabel = new qx.ui.basic.Atom("Method Loops: ");
  loopLabel.setAllowStretchY(false);
  loopLabel.setVerticalAlign("middle");

  var loopInput = new qx.ui.form.TextField("100");
  loopInput.setAllowStretchY(false);
  loopInput.setWidth(50);
  loopInput.setVerticalAlign("middle");

  var repeatLabel = new qx.ui.basic.Atom("Repeat Number: ");
  repeatLabel.setAllowStretchY(false);
  repeatLabel.setVerticalAlign("middle");
  repeatLabel.setMarginLeft(30);

  var btnStart1 = new qx.ui.form.Button("Start 3x", "icon/16/actions/dialog-ok.png");
  var btnStart2 = new qx.ui.form.Button("Start 7x", "icon/16/actions/dialog-ok.png");
  var btnStart3 = new qx.ui.form.Button("Start 15x", "icon/16/actions/dialog-ok.png");
  var btnStart4 = new qx.ui.form.Button("Start 25x", "icon/16/actions/dialog-ok.png");

  btnStart1.addEventListener("execute", function() { this.start(3, parseInt(loopInput.getValue())); }, this);
  btnStart2.addEventListener("execute", function() { this.start(7, parseInt(loopInput.getValue())); }, this);
  btnStart3.addEventListener("execute", function() { this.start(15, parseInt(loopInput.getValue())); }, this);
  btnStart4.addEventListener("execute", function() { this.start(25, parseInt(loopInput.getValue())); }, this);

  var htmlOutput = this._output = new qx.ui.embed.HtmlEmbed();

  htmlOutput.setHtml("");
  htmlOutput.setLocation(20, 78);
  htmlOutput.setRight(335);
  htmlOutput.setBottom(48);
  htmlOutput.setBorder("1px solid black");
  htmlOutput.setBackgroundColor("white");
  htmlOutput.setPadding(10);
  htmlOutput.setOverflow("auto");
  htmlOutput.addToDocument();

  btnLayout.add(loopLabel, loopInput, repeatLabel, btnStart1, btnStart2, btnStart3, btnStart4);
  btnLayout.addToDocument();
}

qx.Proto.start = function(vRounds, vLoops)
{
  var vFuncs = this._functions;
  var vLength = vFuncs.length;
  var vStart;
  var vLocalTimes;
  var vAllTimes = [];
  var vHtmlMeasured = [];
  var vHtmlResults = [];
  var vCellWidth = Math.round(100 / (vLength+1)) + "%";

  vHtmlMeasured.push("<h3>Measured Values</h3>");

  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>");

  vHtmlMeasured.push("<table class='output'>");

  vHtmlMeasured.push("<thead>");

  vHtmlMeasured.push("<tr><td>&#160;</td>");

  for (var j=0; j<vLength; j++) {
    vHtmlMeasured.push("<td>Method " + (j+1) + "</td>");
  }

  vHtmlMeasured.push("</thead><tbody>");

  for (var i=0; i<vRounds; i++)
  {
    vLocalTimes = [];

    for (var j=0; j<vLength; j++)
    {
      vStart = (new Date).valueOf();

      vFuncs[j](vLoops);

      vLocalTimes.push((new Date).valueOf()-vStart);
    }

    vHtmlMeasured.push("<tr><th>Round " + i + "</th>");

    for (var j=0; j<vLocalTimes.length; j++) {
      vHtmlMeasured.push("<td>" + vLocalTimes[j] + "</td>");
    }

    vHtmlMeasured.push("</tr>");
    vAllTimes.push(vLocalTimes);
  }

  vHtmlMeasured.push("</tbody></table>");





  var vSum, vMeanValue, vMeanAll=[], vMeanMin=1e7, vMeanMax=0;

  for (var j=0; j<vLength; j++)
  {
    vSum = 0;

    for (var i=0; i<vRounds; i++)
    {
      vSum += vAllTimes[i][j];
    }

    vMeanValue = Math.round(vSum / vRounds);

    vMeanAll.push(vMeanValue);

    vMeanMin = Math.min(vMeanMin, vMeanValue);
    vMeanMax = Math.max(vMeanMax, vMeanValue);
  }



  var vMedian, vMedianValue, vMedianAll=[], vMedianMin=1e7, vMedianMax=0;

  for (var j=0; j<vLength; j++)
  {
    vMedian = [];

    for (var i=0; i<vRounds; i++)
    {
      vMedian.push(vAllTimes[i][j]);
    }

    vMedian.sort(qx.dev.TimeTracker.compare);
    vMedianValue = vMedian[Math.floor(vRounds / 2)].toString();

    vMedianAll.push(vMedianValue);

    vMedianMin = Math.min(vMedianValue, vMedianMin);
    vMedianMax = Math.max(vMedianValue, vMedianMax);
  }





  vHtmlResults.push("<h3>Results Summary</h3>");

  vHtmlResults.push("<table class='output'>");

  vHtmlResults.push("<thead>");

  vHtmlResults.push("<tr><td>&#160;</td>");

  for (var j=0; j<vLength; j++) {
    vHtmlResults.push("<td>Method " + (j+1) + "</td>");
  }

  vHtmlResults.push("</thead><tbody>");


  vHtmlResults.push("<tr>");

  vHtmlResults.push("<th>Median</th>");

  for (var j=0; j<vLength; j++) {
    vHtmlResults.push("<td>" + vMedianAll[j] + "</td>");
  }

  vHtmlResults.push("</tr>");



  vHtmlResults.push("<tr>");

  vHtmlResults.push("<th>Median Factor</th>");

  for (var j=0; j<vLength; j++)
  {
    vHtmlResults.push("<td>");
    vHtmlResults.push(vMedianMin > 0 ? Math.round(vMedianAll[j] / vMedianMin) : "1");
    vHtmlResults.push("x</td>");
  }

  vHtmlResults.push("</tr>");



  vHtmlResults.push("<tr>");

  vHtmlResults.push("<th>Mean</th>");

  for (var j=0; j<vLength; j++) {
    vHtmlResults.push("<td>" + vMeanAll[j] + "</td>");
  }

  vHtmlResults.push("</tr>");



  vHtmlResults.push("<tr>");

  vHtmlResults.push("<th>Mean Factor</th>");

  for (var j=0; j<vLength; j++)
  {
    vHtmlResults.push("<td>");
    vHtmlResults.push(vMeanMin > 0 ? Math.round(vMeanAll[j] / vMeanMin) : 1);
    vHtmlResults.push("x</td>");
  }

  vHtmlResults.push("</tr>");



  vHtmlResults.push("<tr>");

  vHtmlResults.push("<th>Winner</th>");

  for (var j=0; j<vLength; j++)
  {
    vHtmlResults.push("<td>");

    if (vMedianMin == vMedianAll[j] && vMeanMin == vMeanAll[j])
    {
      vHtmlResults.push("BOTH");
    }

    else if (vMedianMin == vMedianAll[j])
    {
      vHtmlResults.push("MEDIAN");
    }

    else if (vMeanMin == vMeanAll[j])
    {
      vHtmlResults.push("MEAN");
    }

    vHtmlResults.push("</td>");
  }

  vHtmlResults.push("</tr>");

  vHtmlResults.push("</tbody></table>");

  this._output.setHtml(vHtmlResults.join("") + vHtmlMeasured.join(""));
}






/*
---------------------------------------------------------------------------
  DISPOSER
---------------------------------------------------------------------------
*/

qx.Proto.dispose = function()
{
  if (this.getDisposed()) {
    return;
  }

  this._functions = null;

  return qx.core.Object.prototype.dispose.call(this);
}