You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2013/02/19 22:51:06 UTC

[2/2] spec commit: using uubench for autobenching

Updated Branches:
  refs/heads/autobench [created] 2ef67e888


using uubench for autobenching


Project: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/commit/2ef67e88
Tree: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/tree/2ef67e88
Diff: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/diff/2ef67e88

Branch: refs/heads/autobench
Commit: 2ef67e88851ab7e419e46ee71c31e7e2c2d4750a
Parents: e1b3776
Author: Fil Maj <ma...@gmail.com>
Authored: Tue Feb 19 13:48:56 2013 -0800
Committer: Fil Maj <ma...@gmail.com>
Committed: Tue Feb 19 13:48:56 2013 -0800

----------------------------------------------------------------------
 benchmarks/autobench.html |   82 +++++++++++++++++-----------
 benchmarks/uubench.js     |  115 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 165 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/blob/2ef67e88/benchmarks/autobench.html
----------------------------------------------------------------------
diff --git a/benchmarks/autobench.html b/benchmarks/autobench.html
index 9ccd281..5ab910b 100644
--- a/benchmarks/autobench.html
+++ b/benchmarks/autobench.html
@@ -28,42 +28,52 @@
     <title>Cordova Mobile Spec</title>
     <link rel="stylesheet" href="../master.css" type="text/css" media="screen" title="no title" charset="utf-8">
     <script type="text/javascript" charset="utf-8" src="../cordova.js"></script>      
+    <script type="text/javascript" charset="utf-8" src="uubench.js"></script>      
       
 <script>
-    var deviceReady, startTime, firstExec, counter = 0, calls = 0, lastExec;
     var exec = cordova.require('cordova/exec');
+    function $(id) { return document.getElementById(id); }
 
-    function bench() {
-        var loop = true, stopped = false;
-        startTime = new Date().getTime();
-        setTimeout(function() {
-            loop = false;
-        }, 3000);
-        while(loop) {
-            counter++;
-            exec('Echo', 'echo', function(str) {
-                if (firstExec === undefined) {
-                    firstExec = new Date().getTime();
-                }
-                if (!loop) {
-                    if (!stopped) {
-                        stopped = true;
-                        calls = counter;
-                    }
-                    counter--;
-                    if (counter === 0) {
-                        lastExec = new Date().getTime();
-                        console.log(calls + ' exec calls in 3000ms. first callback ' + (firstExec - startTime) + 'ms after start, last ' + (lastExec - startTime) + 'ms after start.');
-                    }
-                }
-            }, function(err) {
-            }, []);
+    var suite = new uubench.Suite({
+        start:function() {
+            $('loading').innerHTML = "BENCHMARKING IN PROGRESS...";
+        },
+        result: function(name, stats) {
+            var secs = stats.elapsed/1000;
+            var itspersec = stats.iterations / secs;
+            $('table-results').innerHTML += '<tr><td>' + name + '</td><td>' + itspersec + ' per second.</td></tr>';
+            console.log(name + ' bench complete.');
+            results[name] = stats;
+        },
+        min: 2000, // each benchmark should run for at least 2000ms
+        done:function() { 
+            $('loading').innerHTML = "Benchmarks complete.";
         }
-    }
+    });
+    var results = {};
 
     document.addEventListener("deviceready", function() {
         deviceReady = true;
-        setTimeout(bench, 250);
+        setTimeout(function() {
+            suite.bench("Echo exec callbacks", function(next) {
+                exec(function() {
+                    // win
+                    next();
+                }, function() {
+                    // fail
+                    next();
+                }, "Echo", "echo", ["test"]);
+            });
+            suite.bench("Echo exec invocations", function(next) {
+                exec(function() {
+                    // win
+                }, function() {
+                    // fail
+                }, "Echo", "echo", ["test"]);
+                next();
+            });
+            suite.run();
+        }, 25);
     }, false);
 
     window.onload = function() {
@@ -78,9 +88,17 @@
 
   </head>
   <body>
-    - bench echo
-      - how many exec calls can we accomplish in 3 seconds
-      - how early the first exec call returns
-      - how long til the last exec call returns
+    <h1>Auto-Benchmarks</h1>
+    <h2 id="loading"></h2>
+    <table>
+        <thead style="font-weight:bold;text-align:center;">
+            <tr>
+                <td>Name</td>
+                <td>Results</td>
+            </tr>
+        </thead>
+        <tbody id="table-results">
+        </tbody>
+    </table>
   </body>
 </html>      

http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/blob/2ef67e88/benchmarks/uubench.js
----------------------------------------------------------------------
diff --git a/benchmarks/uubench.js b/benchmarks/uubench.js
new file mode 100644
index 0000000..7337165
--- /dev/null
+++ b/benchmarks/uubench.js
@@ -0,0 +1,115 @@
+//
+// uubench - Async Benchmarking v0.0.1
+// http://github.com/akdubya/uubench
+//
+// Copyright (c) 2010, Aleksander Williams
+// Released under the MIT License.
+//
+
+(function(uubench){
+
+function Bench(id, test, options, callback) {
+  this.id = id;
+  this.options = options;
+  this.test = test;
+  this.loop = test.length > 1;
+  this.callback = callback;
+}
+
+Bench.prototype.run = function(iter) {
+  var self = this, fn = self.test,
+      checkfn = self.options.type === "adaptive" ? adaptive : fixed,
+      i = iter, pend = i,
+      min = self.options.min, start;
+
+  if (self.loop) {
+    pend = 1;
+    start = new Date();
+    fn(checkfn, i);
+  } else {
+    start = new Date();
+    while (i--) {
+      fn(checkfn);
+    }
+  }
+
+  function fixed() {
+    if (--pend === 0) {
+      var elapsed = new Date() - start;
+      self.callback({iterations: iter, elapsed: elapsed});
+    }
+  }
+
+  function adaptive() {
+    if (--pend === 0) {
+      var elapsed = new Date() - start;
+      if (elapsed < min) {
+        self.run(iter*2);
+      } else {
+        self.callback({iterations: iter, elapsed: elapsed});
+      }
+    }
+  }
+}
+
+uubench.Bench = Bench;
+
+uubench.defaults = {
+  type:       "adaptive", // adaptive or fixed
+  iterations: 10,         // starting iterations
+  min:        100,        // minimum run time (ms) - adaptive only
+  delay:      100         // delay between tests (ms)
+}
+
+function Suite(opts) {
+  for (var key in uubench.defaults) {
+    if (opts[key] === undefined) {
+      opts[key] = uubench.defaults[key];
+    }
+  }
+  this.options = opts;
+  this.tests = [];
+}
+
+Suite.prototype.bench = function(name, fn) {
+  var self = this;
+  self.tests.push(new Bench(name, fn, this.options, function(stats) {
+    self.emit("result", name, stats);
+    self.pending--;
+    self.check();
+  }));
+}
+
+Suite.prototype.run = function() {
+  if (this.pending) return;
+  var self = this, len = self.tests.length;
+  self.emit("start", self.tests);
+  self.start = new Date().getTime();
+  self.pending = len;
+  for (var i=0; i<len; i++) {
+    self.runOne(i);
+  }
+}
+
+Suite.prototype.runOne = function(idx) {
+  var self = this;
+  setTimeout(function() {
+    self.tests[idx].run(self.options.iterations);
+  }, self.options.delay);
+}
+
+Suite.prototype.check = function() {
+  if (this.pending) return;
+  this.emit("done", new Date().getTime() - this.start);
+}
+
+Suite.prototype.emit = function(type) {
+  var event = this.options[type];
+  if (event) {
+    event.apply(this, Array.prototype.slice.call(arguments, 1));
+  }
+}
+
+uubench.Suite = Suite;
+
+})(typeof exports !== 'undefined' ? exports : window.uubench = {});
\ No newline at end of file