parse-100k-blog-rows.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. var lib = __dirname + '/../lib';
  2. var Protocol = require(lib + '/protocol/protocol');
  3. var Packets = require(lib + '/protocol/packets');
  4. var PacketWriter = require(lib + '/protocol/PacketWriter');
  5. var Parser = require(lib + '/protocol/Parser');
  6. var options = {
  7. rows : 100000,
  8. bufferSize : 64 * 1024,
  9. };
  10. console.error('Config:', options);
  11. function createBuffers() {
  12. var parser = new Parser();
  13. process.stderr.write('Creating row buffers ... ');
  14. var number = 1;
  15. var id = 0;
  16. var start = Date.now();
  17. var buffers = [
  18. createPacketBuffer(parser, new Packets.ResultSetHeaderPacket({fieldCount: 2})),
  19. createPacketBuffer(parser, new Packets.FieldPacket({catalog: 'foo', name: 'id'})),
  20. createPacketBuffer(parser, new Packets.FieldPacket({catalog: 'foo', name: 'text'})),
  21. createPacketBuffer(parser, new Packets.EofPacket()),
  22. ];
  23. for (var i = 0; i < options.rows; i++) {
  24. buffers.push(createRowDataPacketBuffer(parser, number++));
  25. }
  26. buffers.push(createPacketBuffer(parser, new Packets.EofPacket));
  27. buffers = mergeBuffers(buffers);
  28. var bytes = buffers.reduce(function(bytes, buffer) {
  29. return bytes + buffer.length;
  30. }, 0);
  31. var mb = (bytes / 1024 / 1024).toFixed(2)
  32. console.error('%s buffers (%s mb) in %s ms', buffers.length, mb, (Date.now() - start));
  33. return buffers;
  34. }
  35. function createPacketBuffer(parser, packet) {
  36. var writer = new PacketWriter();
  37. packet.write(writer);
  38. return writer.toBuffer(parser);
  39. }
  40. function createRowDataPacketBuffer(parser, number) {
  41. var writer = new PacketWriter();
  42. writer.writeLengthCodedString(parser._nextPacketNumber);
  43. writer.writeLengthCodedString('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has sur');
  44. return writer.toBuffer(parser);
  45. }
  46. function mergeBuffers(buffers) {
  47. var mergeBuffer = new Buffer(options.bufferSize);
  48. var mergeBuffers = [];
  49. var offset = 0;
  50. for (var i = 0; i < buffers.length; i++) {
  51. var buffer = buffers[i];
  52. var bytesRemaining = mergeBuffer.length - offset;
  53. if (buffer.length < bytesRemaining) {
  54. buffer.copy(mergeBuffer, offset);
  55. offset += buffer.length;
  56. } else {
  57. buffer.copy(mergeBuffer, offset, 0, bytesRemaining);
  58. mergeBuffers.push(mergeBuffer);
  59. mergeBuffer = new Buffer(options.bufferSize);
  60. buffer.copy(mergeBuffer, 0, bytesRemaining);
  61. offset = buffer.length - bytesRemaining;
  62. }
  63. }
  64. if (offset > 0) {
  65. mergeBuffers.push(mergeBuffer.slice(0, offset));
  66. }
  67. return mergeBuffers;
  68. }
  69. function benchmark(buffers) {
  70. var protocol = new Protocol();
  71. protocol._handshakeInitializationPacket = true;
  72. protocol.query({typeCast: false, sql: 'SELECT ...'});
  73. var start = +new Date;
  74. for (var i = 0; i < buffers.length; i++) {
  75. protocol.write(buffers[i]);
  76. }
  77. var duration = Date.now() - start;
  78. var hz = Math.round(options.rows / (duration / 1000));
  79. console.log(hz);
  80. }
  81. var buffers = createBuffers();
  82. while (true) {
  83. benchmark(buffers);
  84. }