source: Dev/trunk/src/client/dojox/string/tests/PerfFun.html @ 513

Last change on this file since 513 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 7.6 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2  "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4  <head>
5    <title>Perf Tests</title>
6    <script type="text/javascript" src="../../../dojo/dojo.js" data-dojo-config="isDebug:true"></script>
7    <script type="text/javascript" src="lipsum.js"></script>
8    <script type="text/javascript">
9    dojo.addOnLoad(function(){
10      dojo.byId("run").disabled="";
11      dojo.connect(dojo.byId("run"),
12                   "onclick",
13                   function(evt) {
14                     setTimeout(function() {
15                       var words = parseInt(dojo.byId("numWords").value) || 10;
16                       var iters = parseInt(dojo.byId("numIters").value) || 1000;
17                       buildAndRunSet(words, iters);
18                      }, 0);
19                    });
20    });
21   
22    function element(tag, textOrChild) {
23      var e = document.createElement(tag);
24      if(dojo.isArray(textOrChild)){  dojo.forEach(textOrChild, function(c) { e.appendChild(c); }); }
25      if(dojo.isString(textOrChild)){
26        e.appendChild(document.createTextNode(textOrChild));
27      }else{
28        e.appendChild(textOrChild);
29      }
30      return e;
31    }
32   
33    function log(t) {
34      dojo.byId("mess").innerHTML = t;
35    }
36   
37    function reportRun(results){
38      var runs = results.runs;
39      var report = element("dl",
40                     element("dt",
41                             "Run with " + results.words + " words, " +
42                                           results.iterations + " iterations and overhead of " +
43                                           results.overhead));
44                         
45      runs.sort(function(a,b) { return a.time - b.time; });
46      dojo.forEach(runs, function(r) {
47        report.appendChild(element("dd", r.time + " - " + r.name));
48      });
49     
50      dojo.body().appendChild(report);
51    }
52   
53    function runTest(test, iterations, expected) {
54      var i;
55      if(expected != test()){ throw new Error("Test failed expecting " + expected + ", got " + test()); }
56      var start = new Date().getTime(), end;
57      for(i=0; i < iterations; i++){
58        test();
59      }
60      end = new Date().getTime();
61      return end-start;
62    }
63   
64    function runSet(set, iterations){
65     
66      var tests = set.tests.concat(); //copy tests
67      var resultSet = {};
68      resultSet.words = set.words.length;
69      resultSet.overhead = runTest(function(){}, iterations);
70      resultSet.iterations = iterations;
71      var runs = [];
72     
73      function _run() {
74        var t = tests.pop();
75        try {
76          log("Running " + t.name);
77          if(t && !t.skip){ runs.push({ name: t.name, time: runTest(t.test, iterations, set.expected)}); }
78        } catch(e) {
79          console.error("Error running " + t.name, e);
80        }
81        if(tests.length > 0) {
82          setTimeout(_run, 0);
83        }
84        else {
85          log("Done!");
86          resultSet.runs = runs;
87          reportRun(resultSet);
88        }
89      }
90      setTimeout(_run, 0);
91    }
92   
93    function buildTestSet(numWords) {
94      var words = [], i, wordsInLipsum = lipsum.length;
95      for(i = numWords; i > 0; i-=wordsInLipsum) {
96        if(i >= wordsInLipsum) { words = words.concat(lipsum); }
97        else { words = words.concat(lipsum.slice(-i)); }
98      }
99      if(words.length != numWords){ throw new Error("wrong number of words, got " + words.length + ", expected " + numWords); }
100
101      var expected = words.join("");
102     
103      //console.log(words);
104     
105      return {
106        tests: [
107          {
108            name: "dojoForEach",
109            test: function() {
110              var s = "";
111              dojo.forEach(words, function(w) { s+=w; });
112              return s;
113            }
114          },
115          {
116            name: "nativeForEach",
117            test: function() {
118              var s = "";
119              words.forEach(function(w) { s += w; });
120              return s;
121            },
122            skip: !words.forEach
123          },
124          {
125            name: "forLoop",
126            test: function() {
127              var s="",w=words; l=w.length;
128              for(var i = 0; i < l; i++) {
129                s += w[i];
130              }
131              return s;
132            }
133          },
134          {
135            name: "forLoopCallingInlineFunction",
136            test: function() {
137              var s="",w=words; l=w.length;
138              function fn(w) { s += w; }
139              for(var i = 0; i < l; i++) {
140                fn(w[i]);
141              }
142              return s;
143            }
144          },
145          {
146            name: "forLoopCallingExternalFunction",
147            test: function() {
148              var g_s="",w=words; l=w.length;
149              for(var i = 0; i < l; i++) {
150                externalAppend(w[i]);
151              }
152              return g_s;
153            }
154          },
155          {
156            name: "forLoopWithInCheck",
157            test: function() {
158              var s="",w=words; l=w.length;
159              for(var i = 0; i < l; i++) {
160                if(i in w){ s += w[i]; }
161              }
162              return s;
163            }
164          },
165          {
166            name: "emptyFor",
167            test: function() {
168              var w = words; l = w.length;
169              for(var i = 0; i < l; i++){ empty(w[i]); }
170              return expected;
171            }
172          },
173          {
174            name: "emptyForEach",
175            test: function() {
176              dojo.forEach(words, empty);
177              return expected;
178            }
179          } ,
180          {
181            name: "identFor",
182            test: function() {
183              var w = words; l = w.length;
184              for(var i = 0; i < l; i++){ ident(w[i]); }
185              return expected;
186            }
187          },
188          {
189            name: "identForEach",
190            test: function() {
191              dojo.forEach(words, ident);
192              return expected;
193            }
194          },
195          {
196            name: "addUsingFor",
197            test: function() {
198              var x=0;
199              for(var i=0;i<1000;i++){x=x+a[i];}
200              return expected; // fake
201            }
202          },
203          {
204            name: "addUsingForEach",
205            test: function() {
206              var x=0;
207              dojo.forEach(a, function(v,i){x=x+a[i];});
208              return expected; // fake
209            }
210          }
211        ],
212        words: words,
213        expected: expected
214      };
215    }
216   
217    function buildAndRunSet(words, times) {
218      runSet(buildTestSet(words), times);
219    }
220   
221    function ident(w) { return w; }
222    function empty() { }
223   
224    var g_s = "";
225    function externalAppend(w){ g_s += w; }
226   
227    var a = new Array(1000);
228    for(var i=0; i<1000;i++){a[i]=i;}
229   
230    </script>
231    <style type="text/css">
232    html {
233      font-family: Lucida Grande, Tahoma;
234    }
235    div { margin-bottom: 1em; }
236    #results {
237      border: 1px solid #999;
238      border-collapse: collapse;
239    }
240    #results caption {
241      font-size: medium;
242      font-weight: bold;
243    }
244    #results td, #results th {
245      text-align: right;
246      width: 10em;
247      font-size: small;
248      white-space: nowrap;
249    }
250    #wordsCol { background: yellow; }
251    td.max { color: red; font-weight: bold; }
252    td.min { color: green; font-weight: bold; }
253    </style>
254  </head>
255  <body>
256    <table>
257      <tr><td><label for="numWords">Words</label></td><td><input type="text" id="numWords" value="100"/></td></tr>
258      <tr><td><label for="numIters">Iterations</label></td><td><input type="text" id="numIters" value="1000"/></td></tr>
259      <tr><td></td><td><button id="run" disabled>Run Tests!</button></td></tr>
260    </table>
261    <div id="mess"></div>   
262  </body>
263</html>
Note: See TracBrowser for help on using the repository browser.