From df8c7b2b38a214b85432c177541e71682a4aa031 Mon Sep 17 00:00:00 2001 From: Ben Page Date: Mon, 8 Aug 2016 15:13:08 -0500 Subject: [PATCH] integrated load-test.js into test.js modified up load test to function as memory leak detection for acquire() --- test/load-test.js | 60 -------------------------------- test/test.js | 87 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 79 insertions(+), 68 deletions(-) delete mode 100644 test/load-test.js diff --git a/test/load-test.js b/test/load-test.js deleted file mode 100644 index be3b0b5..0000000 --- a/test/load-test.js +++ /dev/null @@ -1,60 +0,0 @@ -var ConnectionPool = require('../lib/connection-pool'); -var Request = require('tedious').Request; - -var poolConfig = {min: 20, max: 100}; -var pool = new ConnectionPool(poolConfig, { - userName: 'test', - password: 'test', - server: 'dev1' -}); - -var clients = 1000; -var connections = 1000; -var total = clients * connections; - -var c = 0; -var p = 0; - -var createRequest = function (err, connection) { - if (err) - console.error(err); - - if (c >= total) - return; - - var request = new Request('select 42', function () { - connection.release(); - - c++; - var m = Math.round(c / total * 100); - if (m > p) { - p = m; - console.log(p); - } - - //console.log(c); - if (c === total - 1) { - console.log('done'); - pool.drain(); - - setTimeout(function() { - process.exit(); - }, 10000); - return; - } - - setTimeout(function() { - pool.acquire(createRequest); - }, 0); - }); - - request.on('row', function (columns) { - //console.log(columns[0].value); - }); - - connection.execSql(request); -}; - -for (var i = 0; i < clients; i++) { - pool.acquire(createRequest); -} diff --git a/test/test.js b/test/test.js index a7bcdf4..8730c9e 100644 --- a/test/test.js +++ b/test/test.js @@ -380,7 +380,7 @@ describe('ConnectionPool', function () { describe('Load Test', function() { var statistics = require('simple-statistics'); - it('Memory Leak Detection', function(done) { + it('Memory Leak Detection - Connection Error', function(done) { this.timeout(60000); if (!global.gc) throw new Error('must run nodejs with --expose-gc'); @@ -389,24 +389,24 @@ describe('Load Test', function() { var groupCount = 20; var poolSize = 1000; var max = poolSize * groupCount; - + var pool = new ConnectionPool({ max: poolSize, min: poolSize, retryDelay: 1}, { userName: 'testLogin', password: 'wrongPassword', server: 'localhost' }); - + pool.on('error', function() { if ((++count % poolSize) !== 0) return; - + global.gc(); - + var heapUsedKB = Math.round(process.memoryUsage().heapUsed / 1024); mem.push([count, heapUsedKB]); - + console.log(count + ': ' + heapUsedKB + 'KB'); - + if (count === max) { var data = statistics.linearRegression(mem); //console.log(data.m); @@ -414,9 +414,80 @@ describe('Load Test', function() { done(new Error('Memory leak not detected.')); else done(); - + pool.drain(); } }); }); + + it('Memory Leak Detection - acquire() and Request', function(done) { + this.timeout(60000); + if (!global.gc) + throw new Error('must run nodejs with --expose-gc'); + + var poolConfig = {min: 67, max: 123}; + var pool = new ConnectionPool(poolConfig, { + userName: 'test', + password: 'test', + server: 'dev1' + }); + + var clients = 1000; + var connections = 100; + var max = clients * connections; + var mem = []; + + for (var i = 0; i < clients; i++) + createClient(); + + var count = 0; + + function end(err) { + done(err); + pool.drain(); + } + + function createClient() { + var clientCount = 0; + + function createRequest() { + pool.acquire(function(err, connection) { + if (err) + return end(err); + + var request = new Request('select 42', function (err) { + if (err) + return end(err); + + connection.release(); + + if (++clientCount < connections) + createRequest(); + + if ((++count % 1000) === 0) { + global.gc(); + + if (count === max) { + var data = statistics.linearRegression(mem); + //console.log(data.m); + if (data.m >= 0.025) + end(new Error('Memory leak not detected.')); + else + end(); + } else { + var heapUsedKB = Math.round(process.memoryUsage().heapUsed / 1024); + mem.push([count, heapUsedKB]); + + console.log(count + ': ' + heapUsedKB + 'KB'); + } + } + }); + + connection.execSql(request); + }); + } + + createRequest(); + } + }); });