Skip to content

Commit

Permalink
Added decode/encode benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Oct 11, 2011
1 parent 175fe85 commit f8c7ff2
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ lib-cov
*.dat
*.out
*.pid
benchmarks/*.png
node_modules
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ test-cov:
test-leaks:
@ls test/leaks/* | xargs node --expose_debug_as=debug --expose_gc

.PHONY: test
bench:
@node benchmarks/encode \
&& node benchmarks/decode \
&& open benchmarks/{encode,decode}.png

.PHONY: test bench
52 changes: 52 additions & 0 deletions benchmarks/decode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

/**
* Module dependencies.
*/

var vbench = require('vbench')
, io = require('../')
, parser = io.parser;

console.log(' decode:');

var suite = vbench.createSuite({
path: __dirname + '/decode.png'
, min: 500
});

suite.bench('string', function(next){
parser.decodePacket('4:::"2"');
next();
});

suite.bench('event', function(next){
parser.decodePacket('5:::{"name":"woot"}');
next()
});

suite.bench('event+ack', function(next){
parser.decodePacket('5:1+::{"name":"tobi"}');
next();
});

suite.bench('event+data', function(next){
parser.decodePacket('5:::{"name":"edwald","args":[{"a": "b"},2,"3"]}');
next();
});

var payload = parser.encodePayload([
parser.encodePacket({ type: 'message', data: '5', endpoint: '' })
, parser.encodePacket({ type: 'message', data: '53d', endpoint: '' })
, parser.encodePacket({ type: 'message', data: 'foobar', endpoint: '' })
, parser.encodePacket({ type: 'message', data: 'foobarbaz', endpoint: '' })
, parser.encodePacket({ type: 'message', data: 'foobarbazfoobarbaz', endpoint: '' })
, parser.encodePacket({ type: 'message', data: 'foobarbaz', endpoint: '' })
, parser.encodePacket({ type: 'message', data: 'foobar', endpoint: '' })
]);

suite.bench('payload', function(next){
parser.decodePayload(payload);
next();
});

suite.run();
70 changes: 70 additions & 0 deletions benchmarks/encode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

/**
* Module dependencies.
*/

var vbench = require('vbench')
, io = require('../')
, parser = io.parser;

console.log('\n encode:');

var suite = vbench.createSuite({
path: __dirname + '/encode.png'
, min: 500
});

suite.bench('string', function(next){
parser.encodePacket({
type: 'json'
, endpoint: ''
, data: '2'
})
next();
});

suite.bench('event', function(next){
parser.encodePacket({
type: 'event'
, name: 'woot'
, endpoint: ''
, args: []
});
next();
});

suite.bench('event+ack', function(next){
parser.encodePacket({
type: 'json'
, id: 1
, ack: 'data'
, endpoint: ''
, data: { a: 'b' }
});
next();
});

suite.bench('event+data', function(next){
parser.encodePacket({
type: 'event'
, name: 'edwald'
, endpoint: ''
, args: [{a: 'b'}, 2, '3']
});
next();
});

suite.bench('payload', function(next){
parser.encodePayload([
parser.encodePacket({ type: 'message', data: '5', endpoint: '' })
, parser.encodePacket({ type: 'message', data: '53d', endpoint: '' })
, parser.encodePacket({ type: 'message', data: 'foobar', endpoint: '' })
, parser.encodePacket({ type: 'message', data: 'foobarbaz', endpoint: '' })
, parser.encodePacket({ type: 'message', data: 'foobarbazfoobarbaz', endpoint: '' })
, parser.encodePacket({ type: 'message', data: 'foobarbaz', endpoint: '' })
, parser.encodePacket({ type: 'message', data: 'foobar', endpoint: '' })
]);
next();
});

suite.run();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"expresso": "0.7.7"
, "should": "0.0.4"
, "assertvanish": "0.0.3-1"
, "vbench": "0.0.2"
}
, "main": "index"
, "engines": { "node": ">= 0.4.0" }
Expand Down

0 comments on commit f8c7ff2

Please sign in to comment.