Click here to Skip to main content
15,885,366 members
Articles / Web Development / Node.js

Node.Js And Stuff

Rate me:
Please Sign up or sign in to vote.
4.97/5 (55 votes)
11 Feb 2013CPOL23 min read 357.1K   2.3K   172  
Small demo app using Node.Js/Socket.IO/MongoDB/D3.Js and jQuery.
/*!
 * ws: a node.js websocket client
 * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
 * MIT Licensed
 */

/**
 * 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();
});

suite.on('cycle', function () {
  receiver = new Receiver();
});

/**
 * 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] = i % 10;
  return getBufferFromHexString('82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 '
       + getHexStringFromBuffer(mask(message), '34 83 a8 68'));
})();
suite.add('binary data (125 bytes)', 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)', 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 (200 kB)', 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;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions