Created
May 21, 2017 21:25
-
-
Save anonymous/1e0faaa99b8bb4afe3749ff94e52c84f to your computer and use it in GitHub Desktop.
A small node.js program using the mysql package to show huge amount of memory consumption on large volumes of high frequency queries.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Mysql = require('mysql') | |
var fs = require('fs') | |
var MysqlOptions = { | |
//connectionLimit: 30, | |
host: 'localhost', | |
user: 'root', | |
password: 'mysqlroot123', | |
database: 'LeakyDB' | |
} | |
var logStream = fs.createWriteStream('console-file.log', {flags: 'w+', defaultEncoding: 'utf8', mode: 0o666, autoClose: true, fd: null}); | |
var logMemStream = fs.createWriteStream('memory.log', {flags: 'w+', defaultEncoding: 'utf8', mode: 0o666, autoClose: true, fd: null}); | |
function logln(str) { | |
logStream.write("[" + new Date().toString() + "] " + str + "\n"); | |
} | |
function logMem(str) { | |
logMemStream.write("[" + new Date().toString() + "] " + str + "\n"); | |
} | |
var leakTester = { | |
MysqlDB: Mysql.createConnection(MysqlOptions), | |
makeBadSqlQuery: function makeBadSqlQuery(scripCode, lastTradedPrice) { | |
var badCode = scripCode + ' 123' | |
var queryStatement = "UPDATE LeakyDB.live_market SET current_market_price=" + lastTradedPrice + " WHERE scrip_code=" + badCode; | |
var self = this; | |
this.MysqlDB.query(queryStatement, function mysqlDBQueryReturn(err, results, fields) { | |
if (err) { | |
logln("Query failed. Code : " + badCode + ' with error : ' + JSON.stringify(err)); | |
} else { | |
logln('Query succeed. results = ' + JSON.stringify(results)); | |
} | |
err = null; | |
results = null; | |
fields = null; | |
}); | |
queryStatement = null; | |
}, | |
syncToMysql: function syncToMysql() { | |
var heapUsed = process.memoryUsage().heapUsed; | |
logMem("Program is using " + heapUsed + " bytes of Heap."); | |
try { | |
for(i = 0; i < 10000; i++) { | |
try { | |
this.makeBadSqlQuery(i, i); | |
} catch(ex) { | |
logln('Error while updating scrip with code : ' + i + ' with error : ' + ex.message); | |
} | |
} | |
} catch(ex) { | |
logln('Exception while syncing : ' + ex.message); | |
} | |
} | |
}; | |
setInterval(function taskDoer() { | |
logln("Triggering sync"); | |
leakTester.syncToMysql(); | |
}, 2000); | |
logln("Process started"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment