yahoo/mojito — pull request #849 by caridy (2012-12-10 16:59:14+00:00)
Title: prototype benchmark command

THIS IS AN EXPERIMENTAL COMMAND

This command enable us to write benchmark tests at the app level. It uses `benchmark` and `benchtable` in conjunction with `YUI` to facilitate such as tests. Here is an example of a benchmark:

```
YUI.add('strings-concat-benchmark', function (Y) {

var suite = Y.BenchmarkSuite;

suite.add('concat', function () {
var b = 'foo' + 'bar';
});

suite.add('array join', function () {
var b = ['foo', 'bar'].join('');
});

}, '0.1', {requires: ['mojito-benchmark']});
```

To execute the test, you can do this:

```
$ mojito benchmark benchmarks/strings-concat-benchmark.js
Benchmarking YUI module strings-concat-benchmark [/Users/caridy/repo/tmp/foo/benchmarks/strings-concat-benchmark.js]
Starting benchmarks.
concat x 168,013,961 ops/sec ±44.33% (28 runs sampled)
array join x 20,195,411 ops/sec ±2.09% (94 runs sampled)
⚠ Fastest is concat
```

Here is another example of the output after executing YUI Base benchmark (from YUI's source) to compare different base implementations:

```
$ mojito benchmark benchmarks/base-benchmark.js
Benchmarking YUI module base-benchmark [/Users/caridy/repo/tmp/foo/benchmarks/base-benchmark.js]
Starting benchmarks.
Base x 6,573 ops/sec ±4.01% (86 runs sampled)
MyBase x 5,893 ops/sec ±3.35% (89 runs sampled)
MyBase with 10 simple value attributes x 4,793 ops/sec ±2.09% (93 runs sampled)
MyBase with 20 varied attributes x 2,792 ops/sec ±1.84% (93 runs sampled)
BaseCore x 31,725 ops/sec ±3.09% (89 runs sampled)
MyBaseCore x 24,552 ops/sec ±6.80% (76 runs sampled)
MyBaseCore with 10 simple value attributes x 8,977 ops/sec ±3.97% (84 runs sampled)
MyBaseCore with 20 varied attributes x 4,606 ops/sec ±3.06% (90 runs sampled)
⚠ Fastest is BaseCore
```

When it comes to benchtable, you can use different datasets to benchmark a functionality with different data. Here is the output of a benchmark table testing strings concatenation with short and long strings:

```
$ mojito benchmark benchmarks/strings-concat-benchtable.js
Benchmarking YUI module strings-concat-benchtable [/Users/caridy/repo/tmp/foo/benchmarks/strings-concat-benchtable.js]
Starting benchmarks.
concat for params small strings (3 characters) x 12,273,020 ops/sec ±0.88% (88 runs sampled)
concat for params big strings (1025 characters) x 11,763,540 ops/sec ±1.10% (99 runs sampled)
array join for params small strings (3 characters) x 7,447,580 ops/sec ±2.63% (94 runs sampled)
array join for params big strings (1025 characters) x 958,773 ops/sec ±0.77% (100 runs sampled)
⚠ Fastest is concat for params small strings (3 characters)
+------------+------------------------------+-------------------------------+
| | small strings (3 characters) | big strings (1025 characters) |
+------------+------------------------------+-------------------------------+
| concat | 12,273,020 ops/sec | 11,763,540 ops/sec |
+------------+------------------------------+-------------------------------+
| array join | 7,447,580 ops/sec | 958,773 ops/sec |
+------------+------------------------------+-------------------------------+
```

As you can see you can test multiple dataset with each implementation.

More details about this here:

https://github.com/caridy/mojito-benchmark