Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

using uubench for autobenching

  • Loading branch information...
commit 2ef67e88851ab7e419e46ee71c31e7e2c2d4750a 1 parent e1b3776
@filmaj filmaj authored
Showing with 165 additions and 32 deletions.
  1. +50 −32 benchmarks/autobench.html
  2. +115 −0 benchmarks/uubench.js
View
82 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>
View
115 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 = {});
Please sign in to comment.
Something went wrong with that request. Please try again.