You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.0 KiB

  1. /*!
  2. * ws: a node.js websocket client
  3. * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Benchmark dependencies.
  8. */
  9. var benchmark = require('benchmark')
  10. , Receiver = require('../').Receiver
  11. , suite = new benchmark.Suite('Receiver');
  12. require('tinycolor');
  13. require('./util');
  14. /**
  15. * Setup receiver.
  16. */
  17. suite.on('start', function () {
  18. receiver = new Receiver();
  19. });
  20. suite.on('cycle', function () {
  21. receiver = new Receiver();
  22. });
  23. /**
  24. * Benchmarks.
  25. */
  26. var pingMessage = 'Hello'
  27. , pingPacket1 = getBufferFromHexString('89 ' + (pack(2, 0x80 | pingMessage.length)) +
  28. ' 34 83 a8 68 '+ getHexStringFromBuffer(mask(pingMessage, '34 83 a8 68')));
  29. suite.add('ping message', function () {
  30. receiver.add(pingPacket1);
  31. });
  32. var pingPacket2 = getBufferFromHexString('89 00')
  33. suite.add('ping with no data', function () {
  34. receiver.add(pingPacket2);
  35. });
  36. var closePacket = getBufferFromHexString('88 00');
  37. suite.add('close message', function () {
  38. receiver.add(closePacket);
  39. receiver.endPacket();
  40. });
  41. 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');
  42. suite.add('masked text message', function () {
  43. receiver.add(maskedTextPacket);
  44. });
  45. binaryDataPacket = (function() {
  46. var length = 125
  47. , message = new Buffer(length)
  48. for (var i = 0; i < length; ++i) message[i] = i % 10;
  49. return getBufferFromHexString('82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 '
  50. + getHexStringFromBuffer(mask(message), '34 83 a8 68'));
  51. })();
  52. suite.add('binary data (125 bytes)', function () {
  53. try {
  54. receiver.add(binaryDataPacket);
  55. }
  56. catch(e) {console.log(e)}
  57. });
  58. binaryDataPacket2 = (function() {
  59. var length = 65535
  60. , message = new Buffer(length)
  61. for (var i = 0; i < length; ++i) message[i] = i % 10;
  62. return getBufferFromHexString('82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 '
  63. + getHexStringFromBuffer(mask(message), '34 83 a8 68'));
  64. })();
  65. suite.add('binary data (65535 bytes)', function () {
  66. receiver.add(binaryDataPacket2);
  67. });
  68. binaryDataPacket3 = (function() {
  69. var length = 200*1024
  70. , message = new Buffer(length)
  71. for (var i = 0; i < length; ++i) message[i] = i % 10;
  72. return getBufferFromHexString('82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 '
  73. + getHexStringFromBuffer(mask(message), '34 83 a8 68'));
  74. })();
  75. suite.add('binary data (200 kB)', function () {
  76. receiver.add(binaryDataPacket3);
  77. });
  78. /**
  79. * Output progress.
  80. */
  81. suite.on('cycle', function (bench, details) {
  82. console.log('\n ' + suite.name.grey, details.name.white.bold);
  83. console.log(' ' + [
  84. details.hz.toFixed(2).cyan + ' ops/sec'.grey
  85. , details.count.toString().white + ' times executed'.grey
  86. , 'benchmark took '.grey + details.times.elapsed.toString().white + ' sec.'.grey
  87. ,
  88. ].join(', '.grey));
  89. });
  90. /**
  91. * Run/export benchmarks.
  92. */
  93. if (!module.parent) {
  94. suite.run();
  95. } else {
  96. module.exports = suite;
  97. }