bench.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // a simple benchmark utility comparing performance between the different builds.
  2. // Uint8Array observations (compared to node Buffers)
  3. // - seems to be pretty much equal for byte ops
  4. // DataView observations (compared to Uint8Array):
  5. // - allocation is about 2 times slower
  6. // - writing is about 5 times (int32) to 10 times (varint) slower
  7. // - reading is about 3 times slower
  8. // UTF8 encoding observations (compared to node's string/buffer API)
  9. // - the longer the string, the poorer is read/write performance
  10. // - either utfx doesn't cut it yet, or node's bindings simply outperform everything js here
  11. var ByteBuffer = require("../index.js"),
  12. prettyHrTime = require("pretty-hrtime");
  13. var impls = [
  14. { name: "ByteBufferNB", impl: ByteBuffer.ByteBufferNB },
  15. { name: "ByteBufferAB", impl: ByteBuffer.ByteBufferAB },
  16. { name: "ByteBufferAB_DataView", impl: ByteBuffer.ByteBufferAB_DataView }
  17. ];
  18. var bench = {};
  19. bench["allocate"] = function(ByteBuffer, n) {
  20. n = n || 10000;
  21. for (var i=0; i<n; ++i)
  22. new ByteBuffer();
  23. return n;
  24. };
  25. bench["writeInt32"] = function(ByteBuffer, n) {
  26. n = n || 1000000;
  27. var bb = new ByteBuffer(4);
  28. for (var i=0; i<n; ++i)
  29. bb.writeInt32(0x7fffffff, 0);
  30. return n;
  31. };
  32. bench["readInt32"] = function(ByteBuffer, n) {
  33. n = n || 1000000;
  34. var bb = new ByteBuffer(4).writeInt32(0x7fffffff).flip();
  35. for (var i=0; i<n; ++i)
  36. bb.readInt32(0);
  37. return n;
  38. };
  39. bench["writeVarint32"] = function(ByteBuffer, n) {
  40. n = n || 1000000;
  41. var bb = new ByteBuffer(6);
  42. for (var i=0; i<n; ++i)
  43. bb.writeVarint32(0x7fffffff, 0);
  44. return n;
  45. };
  46. bench["readVarint32"] = function(ByteBuffer, n) {
  47. n = n || 1000000;
  48. var bb = new ByteBuffer(6).writeInt32(0x7fffffff).flip();
  49. for (var i=0; i<n; ++i)
  50. bb.readVarint32(0);
  51. return n;
  52. };
  53. bench["writeString"] = function(ByteBuffer, n) {
  54. n = n || 100000;
  55. var bb = new ByteBuffer(26);
  56. for (var i=0; i<n; ++i)
  57. bb.writeString("abcdefghijklmnopqrstuvwxyz", 0);
  58. return n;
  59. };
  60. bench["readString"] = function(ByteBuffer, n) {
  61. n = n || 100000;
  62. var bb = new ByteBuffer(26).writeString("abcdefghijklmnopqrstuvwxyz").flip();
  63. for (var i=0; i<n; ++i)
  64. bb.readString(26, ByteBuffer.METRICS_BYTES, 0);
  65. return n;
  66. };
  67. bench["calculateString"] = function(ByteBuffer, n) {
  68. n = n || 100000;
  69. for (var i=0; i<n; ++i)
  70. ByteBuffer.calculateString("abcdefghijklmnopqrstuvwxyz");
  71. return n;
  72. };
  73. Object.keys(bench).forEach(function(key) {
  74. var func = bench[key];
  75. console.log(key);
  76. impls.forEach(function(impl) {
  77. var n, diff;
  78. var start = process.hrtime();
  79. n = func(impl.impl);
  80. diff = process.hrtime(start);
  81. console.log("- "+impl.name+": "+prettyHrTime(diff));
  82. });
  83. console.log();
  84. });