Skip to content

Commit

Permalink
Cleanup code & do memory test
Browse files Browse the repository at this point in the history
  • Loading branch information
brianc committed Sep 12, 2014
1 parent 39ad285 commit 04312d3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 22 deletions.
63 changes: 63 additions & 0 deletions bench/leaks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var Client = require('../');
var async = require('async');

var loop = function() {
var client = new Client();

var connect = function(cb) {
client.connect(cb);
};

var simpleQuery = function(cb) {
client.query('SELECT NOW()', cb);
};

var paramsQuery = function(cb) {
client.query('SELECT $1::text as name', ['Brian'], cb);
};

var prepared = function(cb) {
client.prepare('test', 'SELECT $1::text as name', 1, function(err) {
if(err) return cb(err);
client.execute('test', ['Brian'], cb);
});
};

var error = function(cb) {
client.query('SELECT ASLKJDASLKJD', function() {
cb();
});
};

var sync = function(cb) {
client.querySync('SELECT NOW()');
client.querySync('SELECT $1::text as name', ['Brian']);
client.prepareSync('boom', 'SELECT $1::text as name', 1);
client.executeSync('boom', ['Brian']);
setImmediate(cb);
};

var end = function(cb) {
client.end(cb);
};

var ops = [
connect,
simpleQuery,
paramsQuery,
prepared,
sync,
end
];

var start = Date.now();
async.series(ops, function(err) {
if(err) throw err;
console.log(Date.now() - start);
setImmediate(loop);
});
};

//on my machine this will consume memory up to about 50 megs of ram
//and then stabalize at that point
loop();
47 changes: 26 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,6 @@ var mapResults = function(pq, types) {
return rows;
};

var waitForDrain = function(pq, cb) {
var res = pq.flush();
if(res === 0) return cb();
if(res === -1) return cb(pq.errorMessage());
return pq.writable(function() {
waitForDrain(pq, cb);
});
};

var dispatchQuery = function(pq, fn, cb) {
var success = pq.setNonBlocking(true);
if(!success) return cb(new Error('Unable to set non-blocking to true'));
var sent = fn();
if(!sent) return cb(new Error(pq.errorMessage()));
return waitForDrain(pq, cb);
};

Client.prototype._awaitResult = function(cb) {
this._startReading();
var self = this;
Expand All @@ -169,15 +152,35 @@ Client.prototype._awaitResult = function(cb) {
this.once('result', onResult);
}

//wait for the writable socket to drain
var waitForDrain = function(pq, cb) {
var res = pq.flush();
if(res === 0) return cb();
if(res === -1) return cb(pq.errorMessage());
return pq.writable(function() {
waitForDrain(pq, cb);
});
};

//send an async query to libpq and wait for it to
//finish writing query text to the socket
var dispatchQuery = function(pq, fn, cb) {
var success = pq.setNonBlocking(true);
if(!success) return cb(new Error('Unable to set non-blocking to true'));
var sent = fn();
if(!sent) return cb(new Error(pq.errorMessage() || 'Something went wrong dispatching the query'));
return waitForDrain(pq, cb);
};

Client.prototype.query = function(text, values, cb) {
var queryFn;
var pq = this.pq
var types = this.types
if(typeof values == 'function') {
cb = values;
queryFn = pq.sendQuery.bind(pq, text);
queryFn = function() { return pq.sendQuery(text); };
} else {
queryFn = pq.sendQueryParams.bind(pq, text, values);
queryFn = function() { return pq.sendQueryParams(text, values); };
}

var self = this
Expand All @@ -190,9 +193,11 @@ Client.prototype.query = function(text, values, cb) {
};

Client.prototype.prepare = function(statementName, text, nParams, cb) {
var pq = this.pq;
var fn = pq.sendPrepare.bind(pq, statementName, text, nParams);
var self = this;
var pq = this.pq;
var fn = function() {
return pq.sendPrepare(statementName, text, nParams);
}
dispatchQuery(pq, fn, function(err) {
if(err) return cb(err);
self._awaitResult(cb);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"concat-stream": "^1.4.6",
"lodash": "^2.4.1",
"mocha": "^1.21.4",
"okay": "^0.3.0"
"okay": "^0.3.0",
"pg": "^3.4.2"
}
}

0 comments on commit 04312d3

Please sign in to comment.