Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added benchmark

  • Loading branch information...
commit 92d771065c78a3ad63dd83bd9b4771b5080e35c0 1 parent 0126563
@einaros einaros authored
View
3  Makefile
@@ -19,6 +19,9 @@ test:
integrationtest:
@$(MAKE) NODE_PATH=lib TESTS="$(ALL_INTEGRATION)" run-integrationtests
+benchmark:
+ @node bench/WebSocket.benchmark.js
+
autobahn:
@NODE_PATH=lib node test/autobahn.js
View
117 bench/WebSocket.benchmark.js
@@ -0,0 +1,117 @@
+/**
+ * Benchmark dependencies.
+ */
+
+var benchmark = require('benchmark')
+ , Receiver = require('../').Receiver
+ , suite = new benchmark.Suite('Receiver');
+require('tinycolor');
+require('./util');
+
+/**
+ * Setup receiver.
+ */
+
+suite.on('start', function () {
+ receiver = new Receiver();
+ receiver.on('error', function() { console.log(arguments); });
+});
+
+suite.on('cycle', function () {
+ receiver = new Receiver();
+ receiver.on('error', function() { console.log(arguments); });
+});
+
+/**
+ * Benchmarks.
+ */
+
+var pingMessage = 'Hello'
+ , pingPacket1 = getBufferFromHexString('89 ' + (pack(2, 0x80 | pingMessage.length)) +
+ ' 34 83 a8 68 '+ getHexStringFromBuffer(mask(pingMessage, '34 83 a8 68')));
+suite.add('ping message', function () {
+ receiver.add(pingPacket1);
+});
+
+var pingPacket2 = getBufferFromHexString('89 00')
+suite.add('ping with no data', function () {
+ receiver.add(pingPacket2);
+});
+
+var closePacket = getBufferFromHexString('88 00');
+suite.add('close message', function () {
+ receiver.add(closePacket);
+ receiver.endPacket();
+});
+
+var maskedTextPacket = getBufferFromHexString('81 93 34 83 a8 68 01 b9 92 52 4f a1 c6 09 59 e6 8a 52 16 e6 cb 00 5b a1 d5');
+suite.add('masked text message', function () {
+ receiver.add(maskedTextPacket);
+});
+
+binaryDataPacket = (function() {
+ var length = 125
+ , message = new Buffer(length)
+ for (var i = 0; i < length; ++i) {
+ message[i] = 61;
+ }
+ return getBufferFromHexString('82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 '
+ + getHexStringFromBuffer(mask(message), '34 83 a8 68'));
+})();
+suite.add('binary data', function () {
+ try {
+ receiver.add(binaryDataPacket);
+
+ }
+ catch(e) {console.log(e)}
+});
+
+binaryDataPacket2 = (function() {
+ var length = 65535
+ , message = new Buffer(length)
+ for (var i = 0; i < length; ++i) {
+ message[i] = i % 10;
+ }
+ return getBufferFromHexString('82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 '
+ + getHexStringFromBuffer(mask(message), '34 83 a8 68'));
+})();
+suite.add('binary data (65535 bytes long)', function () {
+ receiver.add(binaryDataPacket2);
+});
+
+binaryDataPacket3 = (function() {
+ var length = 200*1024
+ , message = new Buffer(length)
+ for (var i = 0; i < length; ++i) {
+ message[i] = i % 10;
+ }
+ return getBufferFromHexString('82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 '
+ + getHexStringFromBuffer(mask(message), '34 83 a8 68'));
+})();
+suite.add('binary data (200kB long)', function () {
+ receiver.add(binaryDataPacket3);
+});
+
+/**
+ * Output progress.
+ */
+
+suite.on('cycle', function (bench, details) {
+ console.log('\n ' + suite.name.grey, details.name.white.bold);
+ console.log(' ' + [
+ details.hz.toFixed(2).cyan + ' ops/sec'.grey
+ , details.count.toString().white + ' times executed'.grey
+ , 'benchmark took '.grey + details.times.elapsed.toString().white + ' sec.'.grey
+ ,
+ ].join(', '.grey));
+});
+
+/**
+ * Run/export benchmarks.
+ */
+
+if (!module.parent) {
+ suite.run();
+} else {
+ module.exports = suite;
+}
View
99 bench/util.js
@@ -0,0 +1,99 @@
+/**
+ * Returns a Buffer from a "ff 00 ff"-type hex string.
+ */
+
+getBufferFromHexString = function(byteStr) {
+ var bytes = byteStr.split(' ');
+ var buf = new Buffer(bytes.length);
+ for (var i = 0; i < bytes.length; ++i) {
+ buf[i] = parseInt(bytes[i], 16);
+ }
+ return buf;
+}
+
+/**
+ * Returns a hex string from a Buffer.
+ */
+
+getHexStringFromBuffer = function(data) {
+ var s = '';
+ for (var i = 0; i < data.length; ++i) {
+ s += padl(data[i].toString(16), 2, '0') + ' ';
+ }
+ return s.trim();
+}
+
+/**
+ * Splits a buffer in two parts.
+ */
+
+splitBuffer = function(buffer) {
+ var b1 = new Buffer(Math.ceil(buffer.length / 2));
+ buffer.copy(b1, 0, 0, b1.length);
+ var b2 = new Buffer(Math.floor(buffer.length / 2));
+ buffer.copy(b2, 0, b1.length, b1.length + b2.length);
+ return [b1, b2];
+}
+
+/**
+ * Performs hybi07+ type masking on a hex string or buffer.
+ */
+
+mask = function(buf, maskString) {
+ if (typeof buf == 'string') buf = new Buffer(buf);
+ var mask = getBufferFromHexString(maskString || '34 83 a8 68');
+ for (var i = 0; i < buf.length; ++i) {
+ buf[i] ^= mask[i % 4];
+ }
+ return buf;
+}
+
+/**
+ * Returns a hex string representing the length of a message
+ */
+
+getHybiLengthAsHexString = function(len, masked) {
+ if (len < 126) {
+ var buf = new Buffer(1);
+ buf[0] = (masked ? 0x80 : 0) | len;
+ }
+ else if (len < 65536) {
+ var buf = new Buffer(3);
+ buf[0] = (masked ? 0x80 : 0) | 126;
+ getBufferFromHexString(pack(4, len)).copy(buf, 1);
+ }
+ else {
+ var buf = new Buffer(9);
+ buf[0] = (masked ? 0x80 : 0) | 127;
+ getBufferFromHexString(pack(16, len)).copy(buf, 1);
+ }
+ return getHexStringFromBuffer(buf);
+}
+
+/**
+ * Unpacks a Buffer into a number.
+ */
+
+unpack = function(buffer) {
+ var n = 0;
+ for (var i = 0; i < buffer.length; ++i) {
+ n = (i == 0) ? buffer[i] : (n * 256) + buffer[i];
+ }
+ return n;
+}
+
+/**
+ * Returns a hex string, representing a specific byte count 'length', from a number.
+ */
+
+pack = function(length, number) {
+ return padl(number.toString(16), length, '0').replace(/([0-9a-f][0-9a-f])/gi, '$1 ').trim();
+}
+
+/**
+ * Left pads the string 's' to a total length of 'n' with char 'c'.
+ */
+
+padl = function(s, n, c) {
+ return new Array(1 + n - s.length).join(c) + s;
+}
View
2  bin/wscat
@@ -81,7 +81,7 @@ Console.prototype.restore = function() {
*/
program
- .version('0.3.1')
+ .version('0.3.2-pre')
.usage('[options] <url>')
.option('-l, --listen <port>', 'listen on port')
.option('-c, --connect <url>', 'connect to a websocket server')
View
2  package.json
@@ -2,7 +2,7 @@
"author": "Einar Otto Stangvik <einaros@gmail.com> (http://2x.io)",
"name": "ws",
"description": "simple and very fast websocket protocol client for node.js",
- "version": "0.3.1",
+ "version": "0.3.2-pre",
"repository": {
"type": "git",
"url": "git://github.com/einaros/ws.git"
View
2  wscript
@@ -1,6 +1,6 @@
srcdir = '.'
blddir = 'build'
-VERSION = '0.3.1'
+VERSION = '0.3.2-pre'
def set_options(opt):
opt.tool_options('compiler_cxx')
Please sign in to comment.
Something went wrong with that request. Please try again.