decode.bench.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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('Decode packet');
  9. suite.add('string', function () {
  10. parser.decodePacket('4:::"2"');
  11. });
  12. suite.add('event', function () {
  13. parser.decodePacket('5:::{"name":"woot"}');
  14. });
  15. suite.add('event+ack', function () {
  16. parser.decodePacket('5:1+::{"name":"tobi"}');
  17. });
  18. suite.add('event+data', function () {
  19. parser.decodePacket('5:::{"name":"edwald","args":[{"a": "b"},2,"3"]}');
  20. });
  21. suite.add('heartbeat', function () {
  22. parser.decodePacket('2:::');
  23. });
  24. suite.add('error', function () {
  25. parser.decodePacket('7:::2+0');
  26. });
  27. var payload = parser.encodePayload([
  28. parser.encodePacket({ type: 'message', data: '5', endpoint: '' })
  29. , parser.encodePacket({ type: 'message', data: '53d', endpoint: '' })
  30. , parser.encodePacket({ type: 'message', data: 'foobar', endpoint: '' })
  31. , parser.encodePacket({ type: 'message', data: 'foobarbaz', endpoint: '' })
  32. , parser.encodePacket({ type: 'message', data: 'foobarbazfoobarbaz', endpoint: '' })
  33. , parser.encodePacket({ type: 'message', data: 'foobarbaz', endpoint: '' })
  34. , parser.encodePacket({ type: 'message', data: 'foobar', endpoint: '' })
  35. ]);
  36. suite.add('payload', function () {
  37. parser.decodePayload(payload);
  38. });
  39. suite.on('cycle', function (bench, details) {
  40. console.log('\n' + suite.name.grey, details.name.white.bold);
  41. console.log([
  42. details.hz.toFixed(2).cyan + ' ops/sec'.grey
  43. , details.count.toString().white + ' times executed'.grey
  44. , 'benchmark took '.grey + details.times.elapsed.toString().white + ' sec.'.grey
  45. ,
  46. ].join(', '.grey));
  47. });
  48. if (!module.parent) {
  49. suite.run();
  50. } else {
  51. module.exports = suite;
  52. }