Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 8bfa2f56cb1e4281cab1418757606f9454e5f262 1 parent 56d51ab
John-David Dalton authored October 18, 2012
41  benchmark.js
@@ -320,6 +320,7 @@
320 320
   /** Math shortcuts */
321 321
   var abs   = Math.abs,
322 322
       floor = Math.floor,
  323
+      log   = Math.log,
323 324
       max   = Math.max,
324 325
       min   = Math.min,
325 326
       pow   = Math.pow,
@@ -946,6 +947,20 @@
946 947
   }
947 948
 
948 949
   /**
  950
+   * Computes the geometric mean (log-average) of a sample.
  951
+   * See http://en.wikipedia.org/wiki/Geometric_mean#Relationship_with_arithmetic_mean_of_logarithms.
  952
+   *
  953
+   * @private
  954
+   * @param {Array} sample The sample.
  955
+   * @returns {Number} The geometric mean.
  956
+   */
  957
+  function getGeometricMean(sample) {
  958
+    return pow(Math.E, reduce(sample, function(sum, x) {
  959
+      return sum + log(x);
  960
+    }) / sample.length) || 0;
  961
+  }
  962
+
  963
+  /**
949 964
    * Computes the arithmetic mean of a sample.
950 965
    *
951 966
    * @private
@@ -953,9 +968,9 @@
953 968
    * @returns {Number} The mean.
954 969
    */
955 970
   function getMean(sample) {
956  
-    return reduce(sample, function(sum, x) {
  971
+    return (reduce(sample, function(sum, x) {
957 972
       return sum + x;
958  
-    }) / sample.length || 0;
  973
+    }) / sample.length) || 0;
959 974
   }
960 975
 
961 976
   /**
@@ -2076,6 +2091,10 @@
2076 2091
         event.aborted = me.aborted;
2077 2092
       },
2078 2093
       'onComplete': function(event) {
  2094
+        me.score = getGeometricMean(map(me, function(bench) {
  2095
+          return bench.reference / (bench.times.period * 1e6);
  2096
+        })) || 0;
  2097
+
2079 2098
         me.running = false;
2080 2099
         me.emit(event);
2081 2100
       }
@@ -3169,7 +3188,15 @@
3169 3188
        * @memberOf Benchmark.options
3170 3189
        * @type Function
3171 3190
        */
3172  
-      'onStart': undefined
  3191
+      'onStart': undefined,
  3192
+
  3193
+      /**
  3194
+       * The reference time taken to execute the test once (usecs).
  3195
+       *
  3196
+       * @memberOf Benchmark.options
  3197
+       * @type Number
  3198
+       */
  3199
+      'reference': 0
3173 3200
     },
3174 3201
 
3175 3202
     /**
@@ -3727,6 +3754,14 @@
3727 3754
     'length': 0,
3728 3755
 
3729 3756
     /**
  3757
+     * A score computed using the normalized result of each benchmark in the suite.
  3758
+     *
  3759
+     * @memberOf Benchmark.Suite
  3760
+     * @type Number
  3761
+     */
  3762
+    'score': 0,
  3763
+
  3764
+    /**
3730 3765
      * A flag to indicate if the suite is aborted.
3731 3766
      *
3732 3767
      * @memberOf Benchmark.Suite
43  test/test.js
@@ -1476,6 +1476,49 @@
1476 1476
 
1477 1477
   /*--------------------------------------------------------------------------*/
1478 1478
 
  1479
+  QUnit.module('Benchmark.Suite#score');
  1480
+
  1481
+  (function() {
  1482
+    asyncTest('should compute a score', function() {
  1483
+      Benchmark.Suite()
  1484
+        .add('a', {
  1485
+          'reference': 120.35,
  1486
+          'fn': function() {
  1487
+            var array = [];
  1488
+            for (var index = 0; index < 1e3; index++) {
  1489
+              array.push(index % 2 ? String(index) : index);
  1490
+            }
  1491
+          }
  1492
+        })
  1493
+        .add('b', {
  1494
+          'reference': 360.95,
  1495
+          'fn': function() {
  1496
+            var array = [];
  1497
+            for (var index = 0; index < 3e3; index++) {
  1498
+              array.push(index % 2 ? String(index) : index);
  1499
+            }
  1500
+          }
  1501
+        })
  1502
+        .add('c', {
  1503
+          'reference': 720.10,
  1504
+          'fn': function() {
  1505
+            var array = [];
  1506
+            for (var index = 0; index < 6e3; index++) {
  1507
+              array.push(index % 2 ? String(index) : index);
  1508
+            }
  1509
+          }
  1510
+        })
  1511
+        .on('complete', function() {
  1512
+          var score = this.score;
  1513
+          ok(score && typeof score == 'number');
  1514
+          QUnit.start();
  1515
+        })
  1516
+        .run({ 'async': true });
  1517
+    });
  1518
+  }());
  1519
+
  1520
+  /*--------------------------------------------------------------------------*/
  1521
+
1479 1522
   QUnit.module('Benchmark.Suite#concat');
1480 1523
 
1481 1524
   (function() {

0 notes on commit 8bfa2f5

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