Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

using uubench for autobenching

  • Loading branch information...
commit 2ef67e88851ab7e419e46ee71c31e7e2c2d4750a 1 parent e1b3776
Fil Maj authored February 19, 2013
82  benchmarks/autobench.html
@@ -28,42 +28,52 @@
28 28
     <title>Cordova Mobile Spec</title>
29 29
     <link rel="stylesheet" href="../master.css" type="text/css" media="screen" title="no title" charset="utf-8">
30 30
     <script type="text/javascript" charset="utf-8" src="../cordova.js"></script>      
  31
+    <script type="text/javascript" charset="utf-8" src="uubench.js"></script>      
31 32
       
32 33
 <script>
33  
-    var deviceReady, startTime, firstExec, counter = 0, calls = 0, lastExec;
34 34
     var exec = cordova.require('cordova/exec');
  35
+    function $(id) { return document.getElementById(id); }
35 36
 
36  
-    function bench() {
37  
-        var loop = true, stopped = false;
38  
-        startTime = new Date().getTime();
39  
-        setTimeout(function() {
40  
-            loop = false;
41  
-        }, 3000);
42  
-        while(loop) {
43  
-            counter++;
44  
-            exec('Echo', 'echo', function(str) {
45  
-                if (firstExec === undefined) {
46  
-                    firstExec = new Date().getTime();
47  
-                }
48  
-                if (!loop) {
49  
-                    if (!stopped) {
50  
-                        stopped = true;
51  
-                        calls = counter;
52  
-                    }
53  
-                    counter--;
54  
-                    if (counter === 0) {
55  
-                        lastExec = new Date().getTime();
56  
-                        console.log(calls + ' exec calls in 3000ms. first callback ' + (firstExec - startTime) + 'ms after start, last ' + (lastExec - startTime) + 'ms after start.');
57  
-                    }
58  
-                }
59  
-            }, function(err) {
60  
-            }, []);
  37
+    var suite = new uubench.Suite({
  38
+        start:function() {
  39
+            $('loading').innerHTML = "BENCHMARKING IN PROGRESS...";
  40
+        },
  41
+        result: function(name, stats) {
  42
+            var secs = stats.elapsed/1000;
  43
+            var itspersec = stats.iterations / secs;
  44
+            $('table-results').innerHTML += '<tr><td>' + name + '</td><td>' + itspersec + ' per second.</td></tr>';
  45
+            console.log(name + ' bench complete.');
  46
+            results[name] = stats;
  47
+        },
  48
+        min: 2000, // each benchmark should run for at least 2000ms
  49
+        done:function() { 
  50
+            $('loading').innerHTML = "Benchmarks complete.";
61 51
         }
62  
-    }
  52
+    });
  53
+    var results = {};
63 54
 
64 55
     document.addEventListener("deviceready", function() {
65 56
         deviceReady = true;
66  
-        setTimeout(bench, 250);
  57
+        setTimeout(function() {
  58
+            suite.bench("Echo exec callbacks", function(next) {
  59
+                exec(function() {
  60
+                    // win
  61
+                    next();
  62
+                }, function() {
  63
+                    // fail
  64
+                    next();
  65
+                }, "Echo", "echo", ["test"]);
  66
+            });
  67
+            suite.bench("Echo exec invocations", function(next) {
  68
+                exec(function() {
  69
+                    // win
  70
+                }, function() {
  71
+                    // fail
  72
+                }, "Echo", "echo", ["test"]);
  73
+                next();
  74
+            });
  75
+            suite.run();
  76
+        }, 25);
67 77
     }, false);
68 78
 
69 79
     window.onload = function() {
@@ -78,9 +88,17 @@
78 88
 
79 89
   </head>
80 90
   <body>
81  
-    - bench echo
82  
-      - how many exec calls can we accomplish in 3 seconds
83  
-      - how early the first exec call returns
84  
-      - how long til the last exec call returns
  91
+    <h1>Auto-Benchmarks</h1>
  92
+    <h2 id="loading"></h2>
  93
+    <table>
  94
+        <thead style="font-weight:bold;text-align:center;">
  95
+            <tr>
  96
+                <td>Name</td>
  97
+                <td>Results</td>
  98
+            </tr>
  99
+        </thead>
  100
+        <tbody id="table-results">
  101
+        </tbody>
  102
+    </table>
85 103
   </body>
86 104
 </html>      
115  benchmarks/uubench.js
... ...
@@ -0,0 +1,115 @@
  1
+//
  2
+// uubench - Async Benchmarking v0.0.1
  3
+// http://github.com/akdubya/uubench
  4
+//
  5
+// Copyright (c) 2010, Aleksander Williams
  6
+// Released under the MIT License.
  7
+//
  8
+
  9
+(function(uubench){
  10
+
  11
+function Bench(id, test, options, callback) {
  12
+  this.id = id;
  13
+  this.options = options;
  14
+  this.test = test;
  15
+  this.loop = test.length > 1;
  16
+  this.callback = callback;
  17
+}
  18
+
  19
+Bench.prototype.run = function(iter) {
  20
+  var self = this, fn = self.test,
  21
+      checkfn = self.options.type === "adaptive" ? adaptive : fixed,
  22
+      i = iter, pend = i,
  23
+      min = self.options.min, start;
  24
+
  25
+  if (self.loop) {
  26
+    pend = 1;
  27
+    start = new Date();
  28
+    fn(checkfn, i);
  29
+  } else {
  30
+    start = new Date();
  31
+    while (i--) {
  32
+      fn(checkfn);
  33
+    }
  34
+  }
  35
+
  36
+  function fixed() {
  37
+    if (--pend === 0) {
  38
+      var elapsed = new Date() - start;
  39
+      self.callback({iterations: iter, elapsed: elapsed});
  40
+    }
  41
+  }
  42
+
  43
+  function adaptive() {
  44
+    if (--pend === 0) {
  45
+      var elapsed = new Date() - start;
  46
+      if (elapsed < min) {
  47
+        self.run(iter*2);
  48
+      } else {
  49
+        self.callback({iterations: iter, elapsed: elapsed});
  50
+      }
  51
+    }
  52
+  }
  53
+}
  54
+
  55
+uubench.Bench = Bench;
  56
+
  57
+uubench.defaults = {
  58
+  type:       "adaptive", // adaptive or fixed
  59
+  iterations: 10,         // starting iterations
  60
+  min:        100,        // minimum run time (ms) - adaptive only
  61
+  delay:      100         // delay between tests (ms)
  62
+}
  63
+
  64
+function Suite(opts) {
  65
+  for (var key in uubench.defaults) {
  66
+    if (opts[key] === undefined) {
  67
+      opts[key] = uubench.defaults[key];
  68
+    }
  69
+  }
  70
+  this.options = opts;
  71
+  this.tests = [];
  72
+}
  73
+
  74
+Suite.prototype.bench = function(name, fn) {
  75
+  var self = this;
  76
+  self.tests.push(new Bench(name, fn, this.options, function(stats) {
  77
+    self.emit("result", name, stats);
  78
+    self.pending--;
  79
+    self.check();
  80
+  }));
  81
+}
  82
+
  83
+Suite.prototype.run = function() {
  84
+  if (this.pending) return;
  85
+  var self = this, len = self.tests.length;
  86
+  self.emit("start", self.tests);
  87
+  self.start = new Date().getTime();
  88
+  self.pending = len;
  89
+  for (var i=0; i<len; i++) {
  90
+    self.runOne(i);
  91
+  }
  92
+}
  93
+
  94
+Suite.prototype.runOne = function(idx) {
  95
+  var self = this;
  96
+  setTimeout(function() {
  97
+    self.tests[idx].run(self.options.iterations);
  98
+  }, self.options.delay);
  99
+}
  100
+
  101
+Suite.prototype.check = function() {
  102
+  if (this.pending) return;
  103
+  this.emit("done", new Date().getTime() - this.start);
  104
+}
  105
+
  106
+Suite.prototype.emit = function(type) {
  107
+  var event = this.options[type];
  108
+  if (event) {
  109
+    event.apply(this, Array.prototype.slice.call(arguments, 1));
  110
+  }
  111
+}
  112
+
  113
+uubench.Suite = Suite;
  114
+
  115
+})(typeof exports !== 'undefined' ? exports : window.uubench = {});

0 notes on commit 2ef67e8

Please sign in to comment.
Something went wrong with that request. Please try again.