From 169fa1c9861a5f21dd277855d8155b5ee26321c1 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Fri, 15 Mar 2013 23:13:05 -0700 Subject: [PATCH] Added benchmark script --- test/benchmark.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/benchmark.js diff --git a/test/benchmark.js b/test/benchmark.js new file mode 100644 index 0000000..cf2205d --- /dev/null +++ b/test/benchmark.js @@ -0,0 +1,48 @@ +var ITERATIONS = 1000000; + +var Client = require('../lib/client'), + EventEmitter = require('events').EventEmitter, + util = require('util'); + +var FakeSocket = function() { + EventEmitter.call(this); +}; +util.inherits(FakeSocket, EventEmitter); +FakeSocket.prototype.write = function(){} + +var client = new Client(); +var socket = new FakeSocket(); +client.setSocket(socket); + +var testData = [ + {id: 0x0, params: {keepAliveId: 957759560}}, + {id: 0x3, params: {message: ' Hello World!'}}, + {id: 0xd, params: {x: 6.5, y: 65.62, stance: 67.24, z: 7.5, yaw: 0, pitch: 0, onGround: true}}, + {id: 0xe, params: {status: 1, x: 32, y: 64, z: 32, face: 3}} + // TODO: add more packets for better quality data +]; + +var start, i, j; +console.log('Beginning write test'); +start = Date.now(); +for(i = 0; i < ITERATIONS; i++) { + for(j = 0; j < testData.length; j++) { + client.write(testData[j].id, testData[j].params); + } +} +console.log('Finished write test in ' + (Date.now() - start) / 1000 + ' seconds'); + +var inputData = new Buffer(0); +socket.write = function(data) { + inputData = Buffer.concat([inputData, data]); +}; +for(i = 0; i < testData.length; i++) { + client.write(testData[i].id, testData[i].params); +} + +console.log('Beginning read test'); +start = Date.now(); +for(i = 0; i < ITERATIONS; i++) { + socket.emit('data', inputData); +} +console.log('Finished read test in ' + (Date.now() - start) / 1000 + ' seconds');