File: parse-100k-blog-rows.js

package info (click to toggle)
node-mysql 2.18.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,740 kB
  • sloc: makefile: 4
file content (115 lines) | stat: -rw-r--r-- 3,266 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var lib          = __dirname + '/../lib';
var Buffer       = require('safe-buffer').Buffer;
var Protocol     = require(lib + '/protocol/protocol');
var Packets      = require(lib + '/protocol/packets');
var PacketWriter = require(lib + '/protocol/PacketWriter');
var Parser       = require(lib + '/protocol/Parser');

var options = {
  rows       : 100000,
  bufferSize : 64 * 1024
};

var buffers = createBuffers();

console.error('Config:', options);
run();

function createBuffers() {
  var parser = new Parser();

  process.stderr.write('Creating row buffers ... ');

  var number = 1;
  var start  = Date.now();

  var buffers = [
    createPacketBuffer(parser, new Packets.ResultSetHeaderPacket({fieldCount: 2})),
    createPacketBuffer(parser, new Packets.FieldPacket({catalog: 'foo', name: 'id'})),
    createPacketBuffer(parser, new Packets.FieldPacket({catalog: 'foo', name: 'text'})),
    createPacketBuffer(parser, new Packets.EofPacket())
  ];

  for (var i = 0; i < options.rows; i++) {
    buffers.push(createRowDataPacketBuffer(parser, number++));
  }

  buffers.push(createPacketBuffer(parser, new Packets.EofPacket()));

  buffers = mergeBuffers(buffers);

  var bytes = buffers.reduce(function(bytes, buffer) {
    return bytes + buffer.length;
  }, 0);

  var mb = (bytes / 1024 / 1024).toFixed(2);

  console.error('%s buffers (%s mb) in %s ms', buffers.length, mb, (Date.now() - start));

  return buffers;
}

function createPacketBuffer(parser, packet) {
  var writer = new PacketWriter();
  packet.write(writer);
  return writer.toBuffer(parser);
}

function createRowDataPacketBuffer(parser) {
  var writer = new PacketWriter();

  writer.writeLengthCodedString(parser._nextPacketNumber);
  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');

  return writer.toBuffer(parser);
}

function mergeBuffers(buffers) {
  var mergeBuffer  = Buffer.alloc(options.bufferSize);
  var mergeBuffers = [];
  var offset       = 0;

  for (var i = 0; i < buffers.length; i++) {
    var buffer = buffers[i];

    var bytesRemaining = mergeBuffer.length - offset;
    if (buffer.length < bytesRemaining) {
      buffer.copy(mergeBuffer, offset);
      offset += buffer.length;
    } else {
      buffer.copy(mergeBuffer, offset, 0, bytesRemaining);
      mergeBuffers.push(mergeBuffer);

      mergeBuffer = Buffer.alloc(options.bufferSize);
      buffer.copy(mergeBuffer, 0, bytesRemaining);
      offset = buffer.length - bytesRemaining;
    }
  }

  if (offset > 0) {
    mergeBuffers.push(mergeBuffer.slice(0, offset));
  }

  return mergeBuffers;
}

function benchmark(buffers) {
  var protocol = new Protocol();
  protocol._handshakeInitializationPacket = true;
  protocol.query({typeCast: false, sql: 'SELECT ...'});

  var start = +new Date();

  for (var i = 0; i < buffers.length; i++) {
    protocol.write(buffers[i]);
  }

  var duration = Date.now() - start;
  var hz = Math.round(options.rows / (duration / 1000));
  console.log(hz);
}

function run() {
  benchmark(buffers);
  process.nextTick(run);
}