Permalink
Browse files

using new benchmark lib

  • Loading branch information...
themasch committed Aug 6, 2013
1 parent c4a896b commit ee490b9caa438191105a9954264c69c99f4bfee5
Showing with 159 additions and 94 deletions.
  1. +17 −11 README.md
  2. +12 −0 benchmark/buffer-vs-string.js
  3. +44 −0 benchmark/decode.js
  4. +83 −0 benchmark/encode.js
  5. +0 −81 benchmark/index.js
  6. +3 −2 package.json
View
@@ -38,23 +38,29 @@ npm install bencode
### encode
```
-bencode x 10,933 ops/sec ±1.43% (90 runs sampled)
-bencoding x 7,864 ops/sec ±0.91% (92 runs sampled)
-dht-bencode x 7,028 ops/sec ±8.01% (72 runs sampled)
-bncode x 6,713 ops/sec ±2.62% (82 runs sampled)
-dht.js x 13,762 ops/sec ±1.17% (92 runs sampled)
+ 11,883 op/s ⨠ bencode
+ 5,457 op/s ⨠ bencoding
+ 6,550 op/s ⨠ dht_bencode
+ 5,405 op/s ⨠ bncode
+ 10,978 op/s ⨠ dht
```
### decode
```
-bencode x 34,142 ops/sec ±0.18% (101 runs sampled)
-bencoding x 37,041 ops/sec ±0.50% (99 runs sampled)
-dht-bencode x 29,211 ops/sec ±0.30% (99 runs sampled)
-bncode x 1,208 ops/sec ±0.84% (100 runs sampled)
-dht.js x 27,651 ops/sec ±0.78% (100 runs sampled)
+ 30,240 op/s ⨠ bencode
+ 25,293 op/s ⨠ bencoding
+ 26,220 op/s ⨠ dht_bencode
+ 824 op/s ⨠ bncode
+ 19,824 op/s ⨠ dht
```
-*Benchmarks run on an Intel Core i5-3427U CPU @ 2.29 GHz with node v0.10.4*
+*Benchmarks run on an Intel Core2 CPU T9550 @ 2.66GHz with node v0.10.5*
+
+To run the benchmarks simply use
+
+```
+npm run-script bench
+```
## Usage
@@ -0,0 +1,12 @@
+var bencode = require('../')
+var buf = require('fs').readFileSync(__dirname + '/test.torrent');
+var str = buf.toString('ascii');
+
+suite('buffer vs string', function() {
+ bench('buffer', function() {
+ bencode.decode(buf);
+ })
+ bench('string', function() {
+ bencode.decode(str);
+ })
+})
View
@@ -0,0 +1,44 @@
+var fs = require( 'fs' )
+
+var bencode = require( '../' )
+var bencoding = require( 'bencoding' )
+var dht_bencode = require( 'dht-bencode' )
+var bncode = require( 'bncode' )
+var dht = require( 'dht.js/lib/dht/bencode' )
+
+var buffer = fs.readFileSync( __dirname + '/test.torrent' )
+
+suite('decode to buffer', function() {
+ bench('bencode', function() {
+ bencode.decode( buffer )
+ })
+ bench('bencoding', function() {
+ var v = bencoding.decode( buffer )
+ })
+ bench('dht_bencode', function() {
+ dht_bencode.bdecode( buffer )
+ })
+ bench('bncode', function() {
+ bncode.decode( buffer )
+ })
+ bench('dht', function() {
+ dht.decode( buffer )
+ })
+})
+
+suite('decode to utf8', function() {
+ bench('bencode', function() {
+ bencode.decode( buffer, 'utf8' )
+ })
+})
+suite('decode to ascii', function() {
+ bench('bencode', function() {
+ bencode.decode( buffer, 'ascii' )
+ })
+})
+suite('decode to binary', function() {
+ bench('bencode', function() {
+ bencode.decode( buffer, 'ascii' )
+ })
+})
+
View
@@ -0,0 +1,83 @@
+var fs = require( 'fs' )
+
+var bencode = require( '../' )
+var bencoding = require( 'bencoding' )
+var dht_bencode = require( 'dht-bencode' )
+var bncode = require( 'bncode' )
+var dht = require( 'dht.js/lib/dht/bencode' )
+
+var buffer = fs.readFileSync( __dirname + '/test.torrent' )
+var object = bencode.decode( buffer )
+var object_utf8 = bencode.decode( buffer, 'utf8' )
+var object_ascii = bencode.decode( buffer, 'ascii' )
+var object_binary = bencode.decode( buffer, 'binary' )
+
+suite('encode buffer', function() {
+ bench('bencode', function() {
+ bencode.encode( object )
+ })
+ bench('bencoding', function() {
+ bencoding.encode( object )
+ })
+ bench('dht_bencode', function() {
+ dht_bencode.bencode( object )
+ })
+ bench('bncode', function() {
+ bncode.encode( object )
+ })
+ bench('dht', function() {
+ dht.encode( object )
+ })
+})
+
+suite('encode utf8', function() {
+ bench('bencode', function() {
+ bencode.encode( object_utf8 )
+ })
+ bench('bencoding', function() {
+ bencoding.encode( object_utf8 )
+ })
+ bench('dht_bencode', function() {
+ dht_bencode.bencode( object_utf8 )
+ })
+ bench('bncode', function() {
+ bncode.encode( object_utf8 )
+ })
+ bench('dht', function() {
+ dht.encode( object_utf8 )
+ })
+})
+suite('encode ascii', function() {
+ bench('bencode', function() {
+ bencode.encode( object_ascii )
+ })
+ bench('bencoding', function() {
+ bencoding.encode( object_ascii )
+ })
+ bench('dht_bencode', function() {
+ dht_bencode.bencode( object_ascii )
+ })
+ bench('bncode', function() {
+ bncode.encode( object_ascii )
+ })
+ bench('dht', function() {
+ dht.encode( object_ascii )
+ })
+})
+suite('encode binary', function() {
+ bench('bencode', function() {
+ bencode.encode( object_binary )
+ })
+ bench('bencoding', function() {
+ bencoding.encode( object_binary )
+ })
+ bench('dht_bencode', function() {
+ dht_bencode.bencode( object_binary )
+ })
+ bench('bncode', function() {
+ bncode.encode( object_binary )
+ })
+ bench('dht', function() {
+ dht.encode( object_binary )
+ })
+})
View
@@ -1,81 +0,0 @@
-
-var Benchmark = require( 'benchmark' )
-var fs = require( 'fs' )
-
-var bencode = require( '../' )
-var bencoding = require( 'bencoding' )
-var dht_bencode = require( 'dht-bencode' )
-var bncode = require( 'bncode' )
-var dht = require( 'dht.js/lib/dht/bencode' )
-
-var buffer = fs.readFileSync( __dirname + '/test.torrent' )
-var object = bencode.decode( buffer, 'ascii' )
-var doEncode = true,
- doDecode = true;
-if(process.argv.length > 2) {
- doEncode = (process.argv.indexOf('encode') !== -1);
- doDecode = (process.argv.indexOf('decode') !== -1);
-}
-// ////////////////////////////////////////////////////////////////////////////
-if(doEncode) {
-console.log( 'ENCODING\n' )
-var encoding = new Benchmark.Suite()
-
- .add( 'bencode', function () {
- bencode.encode( object )
- })
- .add( 'bencoding', function () {
- bencoding.encode( object )
- })
- .add( 'dht-bencode', function () {
- dht_bencode.bencode( object )
- })
- .add( 'bncode', function () {
- bncode.encode( object )
- })
- .add( 'dht.js', function () {
- dht.encode( object )
- })
-
- .on( 'cycle', function ( event ) {
- console.log( event.target.toString() )
- })
- .on( 'complete', function ( event, bench ) {
- console.log(
- '\nFastest is ' + this.filter( 'fastest' ).pluck( 'name' ) + '\n\n'
- )
- })
- .run()
-}
-
-// ////////////////////////////////////////////////////////////////////////////
-if(doDecode) {
-console.log( 'DECODING\n' )
-var decoding = new Benchmark.Suite()
-
- .add( 'bencode', function () {
- bencode.decode( buffer )
- })
- .add( 'bencoding', function () {
- bencoding.decode( buffer )
- })
- .add( 'dht-bencode', function () {
- dht_bencode.bdecode( buffer )
- })
- .add( 'bncode', function () {
- bncode.decode( buffer )
- })
- .add( 'dht.js', function () {
- dht.decode( buffer )
- })
-
- .on( 'cycle', function ( event, bench ) {
- console.log( event.target.toString() )
- })
- .on( 'complete', function ( event, bench ) {
- console.log(
- '\nFastest is ' + this.filter( 'fastest' ).pluck( 'name' ) + '\n'
- )
- })
- .run()
-}
View
@@ -21,15 +21,16 @@
"main": "bencode.js",
"devDependencies": {
- "benchmark": "",
+ "matcha": "",
"bencoding": "",
"dht-bencode": "",
"bncode": "",
"dht.js": "",
"mocha": ""
},
"scripts": {
- "test": "./node_modules/.bin/mocha"
+ "test": "./node_modules/.bin/mocha",
+ "bench": "./node_modules/.bin/matcha benchmark/*.js"
},
"repository": {

0 comments on commit ee490b9

Please sign in to comment.