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.

81 lines
1.9 KiB

  1. var Benchmark = require('benchmark');
  2. var parser = require('../index');
  3. function test(packet, deferred) {
  4. var encoder = new parser.Encoder();
  5. var decoder = new parser.Decoder();
  6. encoder.encode(packet, function(encodedPackets) {
  7. var decoder = new parser.Decoder();
  8. decoder.on('decoded', function(packet) {
  9. deferred.resolve();
  10. });
  11. decoder.add(encodedPackets[0]);
  12. });
  13. }
  14. var dataObject = {
  15. 'a': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
  16. 'b': 'xxxyyyzzzalsdfalskdjfalksdjfalksdjfalksdjfjjfjfjfjjfjfjfj',
  17. 'data': {
  18. 'is': 'cool',
  19. 'or': {
  20. 'is': {
  21. 'it': true
  22. }
  23. }
  24. }
  25. };
  26. var bigArray = [];
  27. for (var i = 0; i < 250; i++) {
  28. bigArray.push(dataObject);
  29. }
  30. module.exports = function(callback) {
  31. var suite = new Benchmark.Suite();
  32. suite.add('small json parse', {defer: true, fn: function(deferred) {
  33. var packet = {
  34. type: parser.EVENT,
  35. nsp: '/bench',
  36. data: dataObject
  37. };
  38. test(packet, deferred);
  39. }})
  40. .add('big json parse', {defer: true, fn: function(deferred) {
  41. var packet = {
  42. type: parser.EVENT,
  43. nsp: '/bench',
  44. data: bigArray
  45. };
  46. test(packet, deferred);
  47. }})
  48. .add('json with small binary parse', {defer: true, fn: function(deferred) {
  49. var packet = {
  50. type: parser.EVENT,
  51. nsp: '/bench',
  52. data: {'a': [1, 2, 3], 'b': 'xxxyyyzzz', 'data': new Buffer(1000)}
  53. };
  54. test(packet, deferred);
  55. }})
  56. .add('json with big binary parse', {defer: true, fn: function(deferred) {
  57. var bigBinaryData = {
  58. bin1: new Buffer(10000),
  59. arr: bigArray,
  60. bin2: new Buffer(10000),
  61. bin3: new Buffer(10000)
  62. };
  63. var packet = {
  64. type: parser.EVENT,
  65. nsp: '/bench',
  66. data: bigBinaryData
  67. };
  68. test(packet, deferred);
  69. }})
  70. .on('complete', function() {
  71. callback(this);
  72. })
  73. .run({'async': true});
  74. };