Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
benchmark: Add unit tests, fix passing fn as a string, and update p…
Browse files Browse the repository at this point in the history
…latform.js submodule. [jddalton]
  • Loading branch information
jdalton committed Nov 2, 2011
1 parent 4c412ba commit e0155a0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
10 changes: 6 additions & 4 deletions benchmark.js
Expand Up @@ -188,8 +188,8 @@
* // benchmark name
* 'name': 'foo',
*
* // benchmark test function
* 'fn': fn
* // benchmark test as a string
* 'fn': '[1,2,3,4].sort()'
* });
*
* // a test's `this` binding is set to the benchmark instance
Expand Down Expand Up @@ -228,9 +228,11 @@
me.stats = extend({}, me.stats);
me.times = extend({}, me.times);

fn = me.fn || (me.fn = fn);
fn ? (me.fn = fn) : (fn = me.fn);
if (isClassOf(fn, 'String')) {
(me.fn = function() { }).toString = Function(fn);
(me.fn = me.options.fn = function() { }).toString = function() {
return fn;
};
}
}

Expand Down
74 changes: 74 additions & 0 deletions tests/tests.js
Expand Up @@ -42,6 +42,42 @@

/*--------------------------------------------------------------------------*/

QUnit.module('Benchmark constructor');

test('basic', function() {
var bench = Benchmark(function() { });
ok(bench instanceof Benchmark, 'creation without `new` operator');

bench = Benchmark({ 'name': 'foo', 'fn': function() { } });
ok(bench.fn && bench.name == 'foo', 'arguments (options)');

bench = Benchmark('foo', function() { });
ok(bench.fn && bench.name == 'foo', 'arguments (name, function)');

bench = Benchmark('foo', { 'fn': function() { } });
ok(bench.fn && bench.name == 'foo', 'arguments (name, options)');

bench = Benchmark('foo', function() { }, { 'id': 'bar' });
ok(bench.fn && bench.name == 'foo' && bench.id == 'bar', 'arguments (name, function, options)');
});

test('empty', function() {
var options = Benchmark.options,
maxTime = options.maxTime;

var bench = Benchmark(function() { });
bench.run();
ok(bench.error, 'test errored');

options.minTime && (options.maxTime = options.minTime * 5);
bench = Benchmark({ 'fn': '' });
bench.run();
ok(!bench.error, 'no error on explicitly empty');
options.maxTime = maxTime;
});

/*--------------------------------------------------------------------------*/

QUnit.module('Benchmark.each');

test('basic', function() {
Expand Down Expand Up @@ -392,6 +428,25 @@

/*--------------------------------------------------------------------------*/

QUnit.module('Benchmark.Suite');

test('basic', function() {
var options = Benchmark.options,
maxTime = options.maxTime;

options.minTime && (options.maxTime = options.minTime * 5);

var suite = Benchmark.Suite('foo')
.add('foo', function() { [3,1,5,2,4].sort(); })
.add('bar', function() { [3,1,5,2,4].sort(); })
.run();

ok(suite[0].hz && suite[0].hz == suite[1].hz, 'normalize like benchmarks');
options.maxTime = maxTime;
});

/*--------------------------------------------------------------------------*/

QUnit.module('Benchmark.Suite#abort');

test('basic', function() {
Expand Down Expand Up @@ -518,6 +573,25 @@

QUnit.module('Async tests');

asyncTest('Benchmark.options.defer', function() {
var options = Benchmark.options,
maxTime = options.maxTime;

options.minTime && (options.maxTime = options.minTime * 5);

Benchmark(function(deferred) {
setTimeout(function() { deferred.resolve(); }, 10);
}, {
'defer': true,
'onComplete': function() {
ok(this.cycles, 'deferred benchmark');
options.maxTime = maxTime;
QUnit.start();
}
})
.run();
});

asyncTest('Benchmark.filter', function() {
var count = 0,
options = Benchmark.options,
Expand Down
2 changes: 1 addition & 1 deletion vendor/platform.js
Submodule platform.js updated 1 files
+2 −2 platform.js

0 comments on commit e0155a0

Please sign in to comment.