Comparing version 0.0.1 to 1.0.0
/*! | ||
* B - Benchmarks for Node.js. | ||
* | ||
* | ||
* Veselin Todorov <hi@vesln.com> | ||
@@ -8,24 +8,21 @@ * MIT License. | ||
var benchmark = null; | ||
var benchmark = Benchmark = require('../'); | ||
var b = benchmark('Simple bench.'); | ||
/** | ||
* B | ||
* | ||
* @type {Object} | ||
*/ | ||
var b = require('../'); | ||
b.start(); | ||
for (var i = -1; ++i < 10000000;) var foo = 3; | ||
b.end(); | ||
/** | ||
* Synchronous | ||
*/ | ||
b('Synchronous benchmark').run(100, function() { | ||
for (var i = 0, len = 1000000; ++i < len;) {} | ||
}); | ||
benchmark(function(done) { | ||
for (var i = -1; ++i < 10000000;) var foo = 3; | ||
done(); | ||
/** | ||
* Asynchronous | ||
*/ | ||
b('Asynchronous benchmark').run(10, function(done) { | ||
setTimeout(done, 10); | ||
}); | ||
b = new Benchmark('Testing cool stuff', process.stdout, function(done) { | ||
for (var i = -1; ++i < 10000000;) var foo = 3; | ||
done(); | ||
}).run(); | ||
var bench = new Benchmark('Test', function(done) { | ||
for (var i = -1; ++i < 10000000;) var foo = 3; | ||
done(); | ||
}).run(); |
1.0.0 / 2012-05-17 | ||
================== | ||
* Split benchmarking and reporting | ||
* Brand new design | ||
* Use chai | ||
0.0.1 / 2012-01-27 | ||
@@ -3,0 +10,0 @@ ================== |
133
lib/b.js
/*! | ||
* B - Benchmarks for Node.js. | ||
* | ||
* | ||
* Veselin Todorov <hi@vesln.com> | ||
@@ -9,129 +9,34 @@ * MIT License. | ||
/** | ||
* Benchmark constructor. | ||
* | ||
* Examples: | ||
* | ||
* Async: | ||
* | ||
* var benchmark = new Benchmark('Benchmarking a thing', proccess.stdout, function(done) { | ||
* // do stuff | ||
* done(); | ||
* }); | ||
* | ||
* benchmark.run(); | ||
* | ||
* Sync: | ||
* | ||
* var benchmark = new Benchmark; | ||
* benchmark.start(); | ||
* // do stuff | ||
* benchmark.end(); | ||
* | ||
* You can also omit stream and name. | ||
* | ||
* var benchmark = new Benchmark(function(done) { // do stuff; done() }); | ||
* | ||
* @param {String} name [optional] | ||
* @param {Object} writable stream [optional] | ||
* @param {Function} functionality to benchmark [optional] | ||
* Benchmark class. | ||
* | ||
* @type {Function} | ||
*/ | ||
function Benchmark(name, stream, cb) { | ||
var run = false; | ||
if ('function' === typeof name) { | ||
cb = name; | ||
name = null; | ||
run = true; | ||
} | ||
if ('function' === typeof stream) { | ||
cb = stream; | ||
stream = null; | ||
} | ||
this.name = name; | ||
this.stream = stream || process.stdout; | ||
this.cb = cb; | ||
this._start = null; | ||
this._end = null; | ||
this._time = null; | ||
// Checks if the only one passed argument is callback and if so | ||
// it assumes that the module is used as that: | ||
// | ||
// benchmark(function() { }); | ||
// | ||
// So it will run the benchmark automatically. | ||
if (run) this.run(); | ||
}; | ||
var Benchmark = require('./benchmark'); | ||
/** | ||
* Runs the benchmark. Usually used with when testing | ||
* asynchronous code. | ||
* | ||
* @returns `this` | ||
* @api public | ||
* Default reporter. | ||
* | ||
* @type {Function} | ||
*/ | ||
Benchmark.prototype.run = function() { | ||
var self = this; | ||
var done = function() { | ||
self.end().done(); | ||
}; | ||
this.start().cb(done); | ||
}; | ||
var Reporter = require('./reporter'); | ||
/** | ||
* Called when a benchmark is done. | ||
* | ||
* Benchmark factory. | ||
* | ||
* @param {String} name [optional] | ||
* @returns {Benchmark} | ||
* @api public | ||
*/ | ||
Benchmark.prototype.done = function() { | ||
this.time = this._end - this._start; | ||
this.print(); | ||
return this; | ||
module.exports = function(name) { | ||
return new Benchmark(name).reporter(new Reporter); | ||
}; | ||
/** | ||
* Prints the results from the benchmark. | ||
* | ||
* @returns `this` | ||
* @api private | ||
* Expose `Benchmark`. | ||
*/ | ||
Benchmark.prototype.print = function() { | ||
var out = ''; | ||
if (this.name) out += this.name + ' '; | ||
out += this.time + 'ms\n'; | ||
this.stream.write(out); | ||
return this; | ||
}; | ||
module.exports.Benchmark = Benchmark; | ||
/** | ||
* Starts the timer. | ||
* | ||
* @returns `this` | ||
* @api public | ||
* Expose `Reporter`. | ||
*/ | ||
Benchmark.prototype.start = function(name) { | ||
this._start = Date.now(); | ||
return this; | ||
}; | ||
/** | ||
* Stops the timer. | ||
* | ||
* @returns `this` | ||
* @api public | ||
*/ | ||
Benchmark.prototype.end = function() { | ||
this._end = Date.now(); | ||
// If benchmarking syncrhonous code calls done and otuputs the results. | ||
if ('function' !== typeof this.cb) return this.done(); | ||
return this; | ||
}; | ||
/** | ||
* Expose `Benchmark`. | ||
*/ | ||
module.exports = Benchmark; | ||
module.exports.Reporter = Reporter; |
{ | ||
"name": "b" | ||
, "version": "0.0.1" | ||
, "version": "1.0.0" | ||
, "description": "Benchmarks for Node.js." | ||
@@ -8,5 +8,4 @@ , "keywords": ["benchmarks", "benchmarking"] | ||
, "devDependencies": { | ||
"mocha": "0.3.3" | ||
, "should": "0.3.2" | ||
, "super": "0.0.1" | ||
"mocha": "*" | ||
, "chai": "*" | ||
} | ||
@@ -21,6 +20,6 @@ , "repository" : { | ||
} | ||
, "main": "./lib/index" | ||
, "engines": { | ||
"node": ">= 0.6.0 < 0.7.0" | ||
, "main": "./lib/b" | ||
, "engines": { | ||
"node": ">= 0.6.0" | ||
} | ||
} | ||
} |
@@ -11,39 +11,45 @@ [![Build Status](https://secure.travis-ci.org/vesln/b.png)](http://travis-ci.org/vesln/b) | ||
- Async & sync benchmarks. | ||
- Custom streams. | ||
- Benchmark names. Useful when running multiple benchmarks at the same time. | ||
- Async & sync benchmarks | ||
- Streams | ||
- Reporters | ||
## Synopsis | ||
### Synchronous | ||
```js | ||
var b = require('b'); | ||
var benchmark = require('b'); | ||
benchmark(function(done) { | ||
for (var i = -1; ++i < 10000000;) var foo = 3; | ||
done(); | ||
b('Synchronous benchmark').run(100, function() { | ||
for (var i = 0, len = 1000000; ++i < len;) { | ||
// do stuff | ||
} | ||
}); | ||
``` | ||
### Asynchronous | ||
```js | ||
var b = require('b'); | ||
var benchmark = require('b'); | ||
var b = benchmark('Simple bench.'); | ||
b('Asynchronous benchmark').run(10, function(done) { | ||
// do stuff | ||
done(); | ||
}); | ||
``` | ||
b.start(); | ||
for (var i = -1; ++i < 10000000;) var foo = 3; | ||
b.end(); | ||
### Build your reporter | ||
``` | ||
```js | ||
function Reporter() {}; | ||
var benchmark = require('b').Benchmark; | ||
Reporter.prototype.report = function(name, result, iterations) { | ||
// report it | ||
}; | ||
var b = new Benchmark('Testing cool stuff', process.stdout, function(done) { | ||
for (var i = -1; ++i < 10000000;) var foo = 3; | ||
done(); | ||
}).run(); | ||
b('Custom reporter') | ||
.reporter(new Reporter) | ||
.run(10, function() { | ||
// benchmark stuff | ||
}); | ||
``` | ||
@@ -91,2 +97,2 @@ | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
SOFTWARE. |
/*! | ||
* B - Benchmarks for Node.js. | ||
* | ||
* | ||
* Veselin Todorov <hi@vesln.com> | ||
@@ -9,54 +9,47 @@ * MIT License. | ||
/** | ||
* Test dependencies. | ||
* Support | ||
*/ | ||
var inherit = require('super'); | ||
var should = require('chai').should(); | ||
/** | ||
* The tested class. | ||
* | ||
* Benchmark | ||
* | ||
* @type {Function} | ||
*/ | ||
var Benchmark = require('../lib/b'); | ||
var Benchmark = require('../lib/benchmark'); | ||
/** | ||
* Mock stream. | ||
* | ||
* @type {Object} | ||
* Reporter | ||
* | ||
* @type {Function} | ||
*/ | ||
var stream = { | ||
/** | ||
* Write mock. | ||
* | ||
* @param {String} Text. | ||
* @api public | ||
*/ | ||
write: function(text) { | ||
this.text = text; | ||
} | ||
}; | ||
var Reporter = require('../lib/reporter'); | ||
describe('Benchmark', function() { | ||
describe('asynchronous code', function() { | ||
it('should run benchmarks asynchronously', function() { | ||
var s = inherit(stream, {}); | ||
var bench = function(done) { | ||
for (var i = -1; ++i < 10000000;) var foo = 3; | ||
done(); | ||
}; | ||
var b = new Benchmark('Async for bench.', s, bench).run(); | ||
stream.text.should.match(/^Async for bench. [0-9]+ms/); | ||
}); | ||
/** | ||
* Subject | ||
* | ||
* @type {Function} | ||
*/ | ||
var b = require('..'); | ||
describe('b', function() { | ||
it('exposes Benchmark', function() { | ||
b.Benchmark.should.eq(Benchmark); | ||
}); | ||
describe('synchronous code', function() { | ||
it('should run benchmarks synchronously', function() { | ||
var s = inherit(stream, {}); | ||
var b = new Benchmark('For benchmark.', s); | ||
b.start(); | ||
for (var i = -1; ++i < 10000000;) var foo = 3; | ||
b.end(); | ||
stream.text.should.match(/^For benchmark. [0-9]+ms/); | ||
}); | ||
it('exposes Reporter', function() { | ||
b.Reporter.should.eq(Reporter); | ||
}); | ||
}); | ||
it('returns a new benchmark', function() { | ||
b().should.be.an.instanceof(Benchmark); | ||
}); | ||
it('sets the supplied benchmark description', function() { | ||
b('Test')._name.should.eq('Test'); | ||
}); | ||
it('sets the default reporter', function() { | ||
b()._reporter.should.be.an.instanceof(Reporter); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11872
2
15
458
0
97