From 5f602c404483ed366cfad6c898af9355091cf81b Mon Sep 17 00:00:00 2001 From: Ben Page Date: Mon, 8 Aug 2016 14:45:40 -0500 Subject: [PATCH] integrated memory-usage.js into test.js added linear regression test to detect memory leaks --- test/memory-usage.js | 29 ---------------------------- test/test.js | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 29 deletions(-) delete mode 100644 test/memory-usage.js diff --git a/test/memory-usage.js b/test/memory-usage.js deleted file mode 100644 index 21f756a..0000000 --- a/test/memory-usage.js +++ /dev/null @@ -1,29 +0,0 @@ -//test for memory leak after closing connections -var ConnectionPool = require('../lib/connection-pool'); -var fs = require('fs'); - -var count = 0; -var mem = []; -var groupCount = 20; -var poolSize = 1000; - -var pool = new ConnectionPool({ max: poolSize, min: poolSize, retryDelay: 1}, { - userName: 'testLogin', - password: 'wrongPassword', - server: 'localhost' -}); - -pool.on('error', function() { - var i = Math.floor(count++ / poolSize); - if (i === groupCount) { - var previous = 0; - for (var f = 0; f < groupCount; f++) { - var size = (mem[f] / poolSize); - fs.writeSync(1, ((f + 1) * poolSize) + ': ' + Math.round(mem[f] / poolSize * 100) + 'KB\n'); - global.gc(); - previous = size; - } - process.exit(0); - } - mem[i] = (mem[i] || 0) + (process.memoryUsage().heapUsed / 1000); //kilobytes -}); \ No newline at end of file diff --git a/test/test.js b/test/test.js index 642823a..a7bcdf4 100644 --- a/test/test.js +++ b/test/test.js @@ -1,3 +1,4 @@ +'use strict'; var assert = require('assert'); var Request = require('tedious').Request; var ConnectionPool = require('../lib/connection-pool'); @@ -375,3 +376,47 @@ describe('ConnectionPool', function () { }, 4); }); }); + +describe('Load Test', function() { + var statistics = require('simple-statistics'); + + it('Memory Leak Detection', function(done) { + this.timeout(60000); + if (!global.gc) + throw new Error('must run nodejs with --expose-gc'); + var count = 0; + var mem = []; + 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); + if (data.m >= 0.025) + done(new Error('Memory leak not detected.')); + else + done(); + + pool.drain(); + } + }); + }); +});