Permalink
Browse files

Add `Benchmark.Suite#score` and `Benchmark.options.reference`.

  • Loading branch information...
jdalton committed Oct 18, 2012
1 parent 56d51ab commit 8bfa2f56cb1e4281cab1418757606f9454e5f262
Showing with 81 additions and 3 deletions.
  1. +38 −3 benchmark.js
  2. +43 −0 test/test.js
View
@@ -320,6 +320,7 @@
/** Math shortcuts */
var abs = Math.abs,
floor = Math.floor,
+ log = Math.log,
max = Math.max,
min = Math.min,
pow = Math.pow,
@@ -945,6 +946,20 @@
(/^[\s(]*function[^(]*\(([^\s,)]+)/.exec(fn) || 0)[1]) || '';
}
+ /**
+ * Computes the geometric mean (log-average) of a sample.
+ * See http://en.wikipedia.org/wiki/Geometric_mean#Relationship_with_arithmetic_mean_of_logarithms.
+ *
+ * @private
+ * @param {Array} sample The sample.
+ * @returns {Number} The geometric mean.
+ */
+ function getGeometricMean(sample) {
+ return pow(Math.E, reduce(sample, function(sum, x) {
+ return sum + log(x);
+ }) / sample.length) || 0;
+ }
+
/**
* Computes the arithmetic mean of a sample.
*
@@ -953,9 +968,9 @@
* @returns {Number} The mean.
*/
function getMean(sample) {
- return reduce(sample, function(sum, x) {
+ return (reduce(sample, function(sum, x) {
return sum + x;
- }) / sample.length || 0;
+ }) / sample.length) || 0;
}
/**
@@ -2076,6 +2091,10 @@
event.aborted = me.aborted;
},
'onComplete': function(event) {
+ me.score = getGeometricMean(map(me, function(bench) {
+ return bench.reference / (bench.times.period * 1e6);
+ })) || 0;
+
me.running = false;
me.emit(event);
}
@@ -3169,7 +3188,15 @@
* @memberOf Benchmark.options
* @type Function
*/
- 'onStart': undefined
+ 'onStart': undefined,
+
+ /**
+ * The reference time taken to execute the test once (usecs).
+ *
+ * @memberOf Benchmark.options
+ * @type Number
+ */
+ 'reference': 0
},
/**
@@ -3726,6 +3753,14 @@
*/
'length': 0,
+ /**
+ * A score computed using the normalized result of each benchmark in the suite.
+ *
+ * @memberOf Benchmark.Suite
+ * @type Number
+ */
+ 'score': 0,
+
/**
* A flag to indicate if the suite is aborted.
*
View
@@ -1476,6 +1476,49 @@
/*--------------------------------------------------------------------------*/
+ QUnit.module('Benchmark.Suite#score');
+
+ (function() {
+ asyncTest('should compute a score', function() {
+ Benchmark.Suite()
+ .add('a', {
+ 'reference': 120.35,
+ 'fn': function() {
+ var array = [];
+ for (var index = 0; index < 1e3; index++) {
+ array.push(index % 2 ? String(index) : index);
+ }
+ }
+ })
+ .add('b', {
+ 'reference': 360.95,
+ 'fn': function() {
+ var array = [];
+ for (var index = 0; index < 3e3; index++) {
+ array.push(index % 2 ? String(index) : index);
+ }
+ }
+ })
+ .add('c', {
+ 'reference': 720.10,
+ 'fn': function() {
+ var array = [];
+ for (var index = 0; index < 6e3; index++) {
+ array.push(index % 2 ? String(index) : index);
+ }
+ }
+ })
+ .on('complete', function() {
+ var score = this.score;
+ ok(score && typeof score == 'number');
+ QUnit.start();
+ })
+ .run({ 'async': true });
+ });
+ }());
+
+ /*--------------------------------------------------------------------------*/
+
QUnit.module('Benchmark.Suite#concat');
(function() {

0 comments on commit 8bfa2f5

Please sign in to comment.