Skip to content

Instantly share code, notes, and snippets.

@geNAZt
Created February 7, 2013 14:46
Show Gist options
  • Save geNAZt/4731353 to your computer and use it in GitHub Desktop.
Save geNAZt/4731353 to your computer and use it in GitHub Desktop.
var express = require('express'),
plotter = require('plotter'),
vm = require('vm'),
app = express(),
share = {},
memoryHeapUsage = [],
memoryRSS = [],
curReqs = 0,
reqs = [],
responsetimes = [],
responseAvg = [],
vhosts = {
"www.house-of-code.org": vm.createContext({app: app, require: require, share: share}),
"test.house-of-code.org": vm.createContext({app: app, require: require, share: share})
};
var vhostWWW = vm.createScript("var express = require('express')," +
" vhostApp = express();" +
" share.wwwReqs = 0;" +
"vhostApp.get('/', function(req, res) {" +
" res.end('WWW'); share.wwwReqs += 1;" +
"});" +
"app.use(express.vhost('www.house-of-code.org', vhostApp));");
vhostWWW.runInContext(vhosts["www.house-of-code.org"]);
var vhostTest = vm.createScript("var express = require('express')," +
" vhostApp = express();" +
"vhostApp.get('/', function(req, res) {" +
" res.end(share.wwwReqs.toString());" +
"});" +
"app.use(express.vhost('test.house-of-code.org', vhostApp));");
vhostTest.runInContext(vhosts["test.house-of-code.org"]);
setInterval(function () {
"use strict";
if (memoryHeapUsage.length > 600) {
memoryHeapUsage.splice(0, 1);
}
memoryHeapUsage.push((process.memoryUsage()['heapUsed'] / (1024 * 1024)));
if (memoryRSS.length > 600) {
memoryRSS.splice(0, 1);
}
memoryRSS.push((process.memoryUsage()['rss'] / (1024 * 1024)));
if (reqs.length > 600) {
reqs.splice(0, 1);
}
reqs.push(curReqs);
curReqs = 0;
var sum = 0;
for(var i = 0; i < responsetimes.length; i++){
sum += parseInt(responsetimes[i]);
}
var avg = sum/responsetimes.length;
responsetimes = [];
if (responseAvg.length > 600) {
responseAvg.splice(0, 1);
}
responseAvg.push(avg);
}, 1000);
setInterval(function () {
"use strict";
plotter.plot({
data: { 'rss': memoryRSS, 'heapUsage' : memoryHeapUsage, 'reqs' : reqs, 'respAvg': responseAvg },
filename: 'output.pdf',
style: 'linespoints',
title: 'Testcase with VM Code',
logscale: true,
xlabel: 'Time',
ylabel: 'MegaBytes / Reqs / ms'
});
}, 60000);
app.get("/", function (req, res) {
"use strict";
res.sendfile("output.pdf");
});
app.listen(1234).on('connection', function(sock) {
curReqs += 1;
sock.start = (new Date()).getTime();
var resEnd = sock.end;
sock.end = function(data) {
responsetimes.push((new Date()).getTime() - sock.start);
resEnd(data);
}
});
setTimeout(function() {
process.exit();
}, 602*1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment