node-minecraft-protocol/test/benchmark.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2014-01-02 19:58:09 -05:00
var ITERATIONS = 100000;
2013-03-16 02:13:05 -04:00
var Client = require('../lib/client'),
EventEmitter = require('events').EventEmitter,
util = require('util'),
states = require('../lib/protocol').states;
2013-03-16 02:13:05 -04:00
var FakeSocket = function() {
EventEmitter.call(this);
};
util.inherits(FakeSocket, EventEmitter);
FakeSocket.prototype.write = function(){};
2013-03-16 02:13:05 -04:00
var client = new Client();
var socket = new FakeSocket();
client.setSocket(socket);
client.state = states.PLAY;
2013-03-16 02:13:05 -04:00
var testDataWrite = [
{id: 0x00, params: {keepAliveId: 957759560}},
{id: 0x01, params: {message: '<Bob> Hello World!'}},
{id: 0x06, params: {x: 6.5, y: 65.62, stance: 67.24, z: 7.5, yaw: 0, pitch: 0, onGround: true}},
2013-03-16 02:13:05 -04:00
// 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 < testDataWrite.length; j++) {
client.write(testDataWrite[j].id, testDataWrite[j].params);
2013-03-16 02:13:05 -04:00
}
}
console.log('Finished write test in ' + (Date.now() - start) / 1000 + ' seconds');
var testDataRead = [
{id: 0x00, params: {keepAliveId: 957759560}},
{id: 0x02, params: {message: '<Bob> Hello World!'}},
{id: 0x08, params: {x: 6.5, y: 65.62, stance: 67.24, z: 7.5, yaw: 0, pitch: 0, onGround: true}},
];
client.isServer = true;
2013-03-16 02:13:05 -04:00
var inputData = new Buffer(0);
socket.write = function(data) {
inputData = Buffer.concat([inputData, data]);
};
for(i = 0; i < testDataRead.length; i++) {
client.write(testDataRead[i].id, testDataRead[i].params);
2013-03-16 02:13:05 -04:00
}
client.isServer = false;
2013-03-16 02:13:05 -04:00
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');