Created
October 13, 2011 09:55
-
-
Save 3rd-Eden/1283881 to your computer and use it in GitHub Desktop.
Benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var benchmark = require('benchmark') | |
, microtime = require('microtime') | |
, io = require('../') | |
, parser = io.parser; | |
var encoding = new benchmark.Suite | |
, decoding = new benchmark.Suite; | |
encoding.add('string', function () { | |
parser.encodePacket({ | |
type: 'json' | |
, endpoint: '' | |
, data: '2' | |
}) | |
}); | |
encoding.add('event+ack', function(){ | |
parser.encodePacket({ | |
type: 'json' | |
, id: 1 | |
, ack: 'data' | |
, endpoint: '' | |
, data: { a: 'b' } | |
}); | |
}); | |
encoding.add('event+data', function(){ | |
parser.encodePacket({ | |
type: 'event' | |
, name: 'edwald' | |
, endpoint: '' | |
, args: [{a: 'b'}, 2, '3'] | |
}); | |
}); | |
encoding.add('payload', function(){ | |
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: '' }) | |
]); | |
}); | |
encoding.on('cycle', function (bench, details) { | |
console.log('Executed benchmark encoding (%s)', details.name); | |
console.log('Count (%d), Cycles (%d), Elapsed (%d), Hz (%d)\n' | |
, details.count | |
, details.cycles | |
, details.times.elapsed | |
, details.hz | |
); | |
}); | |
encoding.on('complete', function () { | |
encoding.removeAllListeners('cycle'); | |
decoding.run(); | |
}) | |
decoding.add('string', function(){ | |
parser.decodePacket('4:::"2"'); | |
}); | |
decoding.add('event', function(){ | |
parser.decodePacket('5:::{"name":"woot"}'); | |
}); | |
decoding.add('event+ack', function(){ | |
parser.decodePacket('5:1+::{"name":"tobi"}'); | |
}); | |
decoding.add('event+data', function(){ | |
parser.decodePacket('5:::{"name":"edwald","args":[{"a": "b"},2,"3"]}'); | |
}); | |
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: '' }) | |
]); | |
decoding.add('payload', function(){ | |
parser.decodePayload(payload); | |
}); | |
decoding.on('cycle', function (bench, details) { | |
console.log('Executed benchmark decoding (%s)', details.name); | |
console.log('Count (%d), Cycles (%d), Elapsed (%d), Hz (%d)\n' | |
, details.count | |
, details.cycles | |
, details.times.elapsed | |
, details.hz | |
); | |
}); | |
encoding.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment