Skip to content

Commit f16eaa8

Browse files
committed
Merge branch 'benchmark'
2 parents 4e822a1 + ca5c10a commit f16eaa8

File tree

6 files changed

+223
-25
lines changed

6 files changed

+223
-25
lines changed

benchmark/835f71a76f.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
benchmark
2+
starting simple-query-parsing
3+
3703 ops/sec - (100/0.027)
4+
7299 ops/sec - (1000/0.137)
5+
8888 ops/sec - (10000/1.125)
6+
8733 ops/sec - (10000/1.145)
7+
8810 ops/sec - (10000/1.135)
8+
8771 ops/sec - (10000/1.14)
9+
starting prepared-statement-parsing
10+
3846 ops/sec - (100/0.026)
11+
7299 ops/sec - (1000/0.137)
12+
7225 ops/sec - (10000/1.384)
13+
7288 ops/sec - (10000/1.372)
14+
7225 ops/sec - (10000/1.384)
15+
7457 ops/sec - (10000/1.341)
16+
done
17+

benchmark/df766c913.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
benchmark
2+
starting simple-query-parsing
3+
3571 ops/sec - (100/0.028)
4+
7299 ops/sec - (1000/0.137)
5+
8873 ops/sec - (10000/1.127)
6+
8536 ops/sec - (40000/4.686)
7+
8494 ops/sec - (40000/4.709)
8+
7695 ops/sec - (40000/5.198)
9+
starting prepared-statement-parsing
10+
4000 ops/sec - (100/0.025)
11+
6944 ops/sec - (1000/0.144)
12+
7153 ops/sec - (10000/1.398)
13+
7127 ops/sec - (40000/5.612)
14+
7208 ops/sec - (40000/5.549)
15+
6460 ops/sec - (40000/6.191)
16+
done
17+

benchmark/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var async = require('async');
2+
var max = 10000;
3+
var maxTimes = 3;
4+
var doLoops = function(bench, loops, times, cb) {
5+
var start = new Date();
6+
var count = 0;
7+
8+
var done = function() {
9+
var duration = (new Date() - start)
10+
var seconds = (duration / 1000);
11+
console.log("%d ops/sec - (%d/%d)", ~~(loops/seconds), loops, seconds);
12+
var next = loops * 10;
13+
if(next > max) {
14+
if(times > maxTimes) return cb();
15+
times++;
16+
next = max;
17+
}
18+
setTimeout(function() {
19+
doLoops(bench, next, times, cb);
20+
}, 100);
21+
}
22+
23+
var run = function() {
24+
if(count++ >= loops){
25+
return done();
26+
}
27+
bench(function() {
28+
setImmediate(run);
29+
});
30+
}
31+
run();
32+
}
33+
var bench = require(__dirname + '/simple-query-parsing');
34+
console.log();
35+
var benches = ['simple-query-parsing', 'prepared-statement-parsing'];
36+
async.forEachSeries(benches, function(name, cb) {
37+
var bench = require(__dirname + '/' + name)();
38+
console.log('starting ', name);
39+
doLoops(bench, 100, 1, cb);
40+
}, function(err, res) {
41+
console.log('done')
42+
})
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
var Client = require(__dirname + '/../lib/client');
2+
var buffers = require(__dirname + '/../test/test-buffers');
3+
require(__dirname + '/../test/unit/test-helper');
4+
5+
var stream = new MemoryStream();
6+
stream.readyState = 'open';
7+
var client = new Client({
8+
stream: stream
9+
});
10+
11+
var rowDescription = new buffers.rowDescription([{
12+
name: 'id',
13+
tableID: 1,
14+
attributeNumber: 1,
15+
dataTypeID: 23, //int4
16+
typeModifer: 0,
17+
formatCode: 0
18+
},{
19+
name: 'name',
20+
tableID: 1,
21+
attributeNumber: 2,
22+
dataTypeID: 25, //text
23+
typeModifer: 0,
24+
formatCode: 0 //text format
25+
}, {
26+
name: 'comment',
27+
tableID: 1,
28+
attributeNumber: 3,
29+
dataTypeID: 25, //text
30+
typeModifer: 0,
31+
formatCode: 0 //text format
32+
}]);
33+
var row1 = buffers.dataRow(['1', 'Brian', 'Something groovy']);
34+
var row2 = buffers.dataRow(['2', 'Bob', 'Testint test']);
35+
var row3 = buffers.dataRow(['3', 'The amazing power of the everlasting gobstopper', 'okay now']);
36+
var parseCompleteBuffer = buffers.parseComplete();
37+
var bindCompleteBuffer = buffers.bindComplete();
38+
var portalSuspendedBuffer = buffers.portalSuspended();
39+
var complete = buffers.commandComplete('SELECT 3');
40+
var ready = buffers.readyForQuery();
41+
var buffer = Buffer.concat([parseCompleteBuffer,
42+
bindCompleteBuffer,
43+
rowDescription,
44+
row1,
45+
row2,
46+
row3,
47+
portalSuspendedBuffer,
48+
row1,
49+
row2,
50+
row3,
51+
portalSuspendedBuffer,
52+
row1,
53+
row2,
54+
row3,
55+
portalSuspendedBuffer,
56+
complete, ready]);
57+
58+
var bufferSlice = require('buffer-slice');
59+
var buffers = bufferSlice(10, buffer);
60+
61+
client.connect(assert.calls(function() {
62+
client.connection.emit('readyForQuery');
63+
module.exports = function() {
64+
return function(done) {
65+
client.query('SELECT * FROM whatever WHERE this = "doesnt even matter"', ['whatever'], function(err, res) {
66+
assert.equal(res.rows.length, 9);
67+
done();
68+
});
69+
buffers.forEach(stream.emit.bind(stream, 'data'));
70+
};
71+
};
72+
}));
73+
client.connection.emit('readyForQuery');

benchmark/simple-query-parsing.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var Client = require(__dirname + '/../lib/client');
2+
var buffers = require(__dirname + '/../test/test-buffers');
3+
require(__dirname + '/../test/unit/test-helper');
4+
5+
var stream = new MemoryStream();
6+
stream.readyState = 'open';
7+
var client = new Client({
8+
stream: stream
9+
});
10+
11+
var rowDescription = new buffers.rowDescription([{
12+
name: 'id',
13+
tableID: 1,
14+
attributeNumber: 1,
15+
dataTypeID: 23, //int4
16+
typeModifer: 0,
17+
formatCode: 0
18+
},{
19+
name: 'name',
20+
tableID: 1,
21+
attributeNumber: 2,
22+
dataTypeID: 25, //text
23+
typeModifer: 0,
24+
formatCode: 0 //text format
25+
}, {
26+
name: 'comment',
27+
tableID: 1,
28+
attributeNumber: 3,
29+
dataTypeID: 25, //text
30+
typeModifer: 0,
31+
formatCode: 0 //text format
32+
}]);
33+
var row1 = buffers.dataRow(['1', 'Brian', 'Something groovy']);
34+
var row2 = buffers.dataRow(['2', 'Bob', 'Testint test']);
35+
var row3 = buffers.dataRow(['3', 'The amazing power of the everlasting gobstopper', 'okay now']);
36+
var complete = buffers.commandComplete('SELECT 3');
37+
var ready = buffers.readyForQuery();
38+
var buffer = Buffer.concat([
39+
rowDescription,
40+
row1, row2, row3,
41+
row1, row2, row3,
42+
row1, row2, row3,
43+
complete, ready]);
44+
var bufferSlice = require('buffer-slice');
45+
buffers = bufferSlice(10, buffer);
46+
47+
client.connect(assert.calls(function() {
48+
client.connection.emit('readyForQuery');
49+
module.exports = function() {
50+
return function(done) {
51+
client.query('SELECT * FROM whatever WHERE this = "doesnt even matter"', function(err, res) {
52+
assert.equal(res.rows.length, 9);
53+
done();
54+
});
55+
buffers.forEach(stream.emit.bind(stream, 'data'));
56+
};
57+
};
58+
}));
59+
client.connection.emit('readyForQuery');

lib/connection.js

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ Connection.prototype.parseT = function(msg) {
474474
msg.fieldCount = this.parseInt16();
475475
var fields = [];
476476
for(var i = 0; i < msg.fieldCount; i++){
477-
fields[i] = this.parseField();
477+
fields.push(this.parseField());
478478
}
479479
msg.fields = fields;
480480
return msg;
@@ -498,7 +498,11 @@ Connection.prototype.parseD = function(msg) {
498498
var fields = [];
499499
for(var i = 0; i < fieldCount; i++) {
500500
var length = this.parseInt32();
501-
fields[i] = (length === -1 ? null : this.readBytes(length));
501+
var value = null;
502+
if(length !== -1) {
503+
value = this.readBytes(length);
504+
}
505+
fields.push(value);
502506
}
503507
msg.fieldCount = fieldCount;
504508
msg.fields = fields;
@@ -553,49 +557,35 @@ Connection.prototype.parseA = function(msg) {
553557
};
554558

555559
Connection.prototype.parseGH = function (msg) {
556-
msg.binary = Boolean(this.parseInt8());
560+
var isBinary = this.buffer[this.offset] !== 0;
561+
this.offset++;
562+
msg.binary = isBinary;
557563
var columnCount = this.parseInt16();
558564
msg.columnTypes = [];
559565
for(var i = 0; i<columnCount; i++) {
560-
msg.columnTypes[i] = this.parseInt16();
566+
msg.columnTypes.push(this.parseInt16());
561567
}
562568
return msg;
563569
};
564570

565-
Connection.prototype.parseInt8 = function () {
566-
var value = Number(this.buffer[this.offset]);
567-
this.offset++;
568-
return value;
569-
};
570-
571571
Connection.prototype.readChar = function() {
572572
return Buffer([this.buffer[this.offset++]]).toString(this.encoding);
573573
};
574574

575575
Connection.prototype.parseInt32 = function() {
576-
var value = this.peekInt32();
576+
var value = this.buffer.readInt32BE(this.offset, true);
577577
this.offset += 4;
578578
return value;
579579
};
580580

581-
Connection.prototype.peekInt32 = function(offset) {
582-
offset = offset || this.offset;
583-
var buffer = this.buffer;
584-
return ((buffer[offset++] << 24) +
585-
(buffer[offset++] << 16) +
586-
(buffer[offset++] << 8) +
587-
buffer[offset++]);
588-
};
589-
590-
591581
Connection.prototype.parseInt16 = function() {
592-
return ((this.buffer[this.offset++] << 8) +
593-
(this.buffer[this.offset++] << 0));
582+
var value = this.buffer.readInt16BE(this.offset, true);
583+
this.offset += 2;
584+
return value;
594585
};
595586

596587
Connection.prototype.readString = function(length) {
597-
return this.buffer.toString(this.encoding, this.offset,
598-
(this.offset += length));
588+
return this.buffer.toString(this.encoding, this.offset, (this.offset += length));
599589
};
600590

601591
Connection.prototype.readBytes = function(length) {

0 commit comments

Comments
 (0)