encode.bench.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Module dependencies.
  3. */
  4. var benchmark = require('benchmark')
  5. , colors = require('colors')
  6. , io = require('../')
  7. , parser = io.parser
  8. , suite = new benchmark.Suite('Encode packet');
  9. suite.add('string', function () {
  10. parser.encodePacket({
  11. type: 'json'
  12. , endpoint: ''
  13. , data: '2'
  14. });
  15. });
  16. suite.add('event', function () {
  17. parser.encodePacket({
  18. type: 'event'
  19. , name: 'woot'
  20. , endpoint: ''
  21. , args: []
  22. });
  23. });
  24. suite.add('event+ack', function () {
  25. parser.encodePacket({
  26. type: 'json'
  27. , id: 1
  28. , ack: 'data'
  29. , endpoint: ''
  30. , data: { a: 'b' }
  31. });
  32. });
  33. suite.add('event+data', function () {
  34. parser.encodePacket({
  35. type: 'event'
  36. , name: 'edwald'
  37. , endpoint: ''
  38. , args: [{a: 'b'}, 2, '3']
  39. });
  40. });
  41. suite.add('heartbeat', function () {
  42. parser.encodePacket({
  43. type: 'heartbeat'
  44. , endpoint: ''
  45. })
  46. });
  47. suite.add('error', function () {
  48. parser.encodePacket({
  49. type: 'error'
  50. , reason: 'unauthorized'
  51. , advice: 'reconnect'
  52. , endpoint: ''
  53. })
  54. })
  55. suite.add('payload', function () {
  56. parser.encodePayload([
  57. parser.encodePacket({ type: 'message', data: '5', endpoint: '' })
  58. , parser.encodePacket({ type: 'message', data: '53d', endpoint: '' })
  59. , parser.encodePacket({ type: 'message', data: 'foobar', endpoint: '' })
  60. , parser.encodePacket({ type: 'message', data: 'foobarbaz', endpoint: '' })
  61. , parser.encodePacket({ type: 'message', data: 'foobarbazfoobarbaz', endpoint: '' })
  62. , parser.encodePacket({ type: 'message', data: 'foobarbaz', endpoint: '' })
  63. , parser.encodePacket({ type: 'message', data: 'foobar', endpoint: '' })
  64. ]);
  65. });
  66. suite.on('cycle', function (bench, details) {
  67. console.log('\n' + suite.name.grey, details.name.white.bold);
  68. console.log([
  69. details.hz.toFixed(2).cyan + ' ops/sec'.grey
  70. , details.count.toString().white + ' times executed'.grey
  71. , 'benchmark took '.grey + details.times.elapsed.toString().white + ' sec.'.grey
  72. ,
  73. ].join(', '.grey));
  74. });
  75. if (!module.parent) {
  76. suite.run();
  77. } else {
  78. module.exports = suite;
  79. }