Created
April 2, 2014 02:17
-
-
Save bnolan/9926889 to your computer and use it in GitHub Desktop.
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
msgpack = require("msgpack"); | |
randomDouble = function(){ | |
return 0xFFFFFFFFFFFF * Math.random() / 0xFFFF; | |
} | |
randomByte = function(){ | |
return parseInt(0xFF * Math.random()); | |
} | |
randomPacket = function(){ | |
switch(parseInt(Math.random() * 0xFF % 10)){ | |
case 0: | |
// connect packet | |
return [0x01, randomByte()] | |
break; | |
case 3: | |
// disconnect packet | |
return [0x03, randomByte()] | |
break; | |
default: | |
// x,y,z position packet | |
return [0x02, randomByte(), randomDouble(), randomDouble(), randomDouble()] | |
break; | |
} | |
} | |
jsonTest = function(array){ | |
var r = JSON.stringify(array); | |
return JSON.parse(r) && r.length; | |
} | |
msgpackTest = function(array){ | |
var r = msgpack.pack(array); | |
return msgpack.unpack(r) && r.length; | |
} | |
customTest = function(array){ | |
var packetSize = 1 * 2 + 8 * 3, | |
buffer = new ArrayBuffer((array.length + 1) * packetSize), | |
view = new DataView(buffer), | |
offset = 0, | |
x; | |
for(x=0;x<array.length;x++){ | |
view.setUint8(offset += 1, array[x][0]); | |
view.setUint8(offset += 1, array[x][1]); | |
if(array[x][0]==0x02){ | |
view.setFloat64(offset += 8, array[x][2]); | |
view.setFloat64(offset += 8, array[x][3]); | |
view.setFloat64(offset += 8, array[x][4]); | |
} | |
} | |
offset = 0; | |
for(x=0;x<array.length;x++){ | |
result = [ | |
view.getUint8(offset += 1, true), | |
view.getUint8(offset += 1, true) | |
] | |
if(result[0]==0x02){ | |
result.concat([ | |
view.getFloat64(offset += 8, true), | |
view.getFloat64(offset += 8, true), | |
view.getFloat64(offset += 8, true) | |
]); | |
} | |
} | |
// parse.. | |
return offset; | |
} | |
var ITER = 10 * 10, | |
array = [], | |
x = 0; | |
for(x=0;x<5000;x++){ | |
array.push(randomPacket()); | |
} | |
var t = process.hrtime(), | |
iterations = ITER, | |
length = 0; | |
console.log("Json..."); | |
for(i=0;i<iterations;i++){ | |
length += jsonTest(array); | |
} | |
t2 = process.hrtime(t); | |
console.log(' * benchmark took %d seconds and %d ms', t2[0], parseInt(t2[1] / 1000000)); | |
console.log(' * average size of packet stream is %d', parseInt(length / iterations)); | |
var t = process.hrtime(), | |
iterations = ITER, | |
length = 0; | |
console.log("Msgpack..."); | |
for(i=0;i<iterations;i++){ | |
length += msgpackTest(array); | |
} | |
t2 = process.hrtime(t); | |
console.log(' * benchmark took %d seconds and %d ms', t2[0], parseInt(t2[1] / 1000000)); | |
console.log(' * average size of packet stream is %d', parseInt(length / iterations)); | |
var t = process.hrtime(), | |
iterations = ITER, | |
length = 0; | |
console.log("Custom..."); | |
for(i=0;i<iterations;i++){ | |
length += customTest(array); | |
} | |
t2 = process.hrtime(t); | |
console.log(' * benchmark took %d seconds and %d ms', t2[0], parseInt(t2[1] / 1000000)); | |
console.log(' * average size of packet stream is %d', parseInt(length / iterations)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment