diff --git a/.gitignore b/.gitignore index 43378d090d..a70d306a8a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ lib-cov *.dat *.out *.pid +benchmarks/*.png node_modules diff --git a/Makefile b/Makefile index 79817058c3..34c1d21b76 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/benchmarks/decode.js b/benchmarks/decode.js new file mode 100644 index 0000000000..1bf3e7bdc8 --- /dev/null +++ b/benchmarks/decode.js @@ -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(); \ No newline at end of file diff --git a/benchmarks/encode.js b/benchmarks/encode.js new file mode 100644 index 0000000000..20547cff4d --- /dev/null +++ b/benchmarks/encode.js @@ -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(); \ No newline at end of file diff --git a/package.json b/package.json index d685162c56..3a7238ffe9 100644 --- a/package.json +++ b/package.json @@ -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" }