var child = require('child_process'); var fs = require('fs'); function hrToStr(diff) { var ldateDiff = ( diff[0] * 1000000 + diff[1] / 1000 ) / 1000; var ldateDiffStr = ""; if(ldateDiff > 1000) ldateDiffStr = (ldateDiff / 1000).toFixed(2) + "s"; else { ldateDiffStr = ldateDiff.toFixed(2) +"ms"; } return ldateDiffStr; } module.exports.bundleActivator = { // Couple of tests start(context) { context.manager.load(__dirname + "/http-test"); this.context = context; context.prefix('core-shell:cmd') .on('test',(req,res,e) => { var cmds = ["events","cpu","mem","ping","siege"]; var [subcmd,...args] = req.args; if(cmds.indexOf(subcmd) == -1) { res.write("Wrong\n"); res.write("Available tests:" , cmds + "\n"); return; }; this[subcmd](req,res,e); }); var lastPing; context.on('stress-test:ping',() => { lastPing = process.hrtime(); context.emit('pong') }); context.on('stress-test:pong',() => { var now = hrToStr(process.hrtime(lastPing)); console.log("Time taken to poing: " + now); }); }, ping() { this.context.emit('ping'); }, events(req,res,e) { var count = 1000000; for(;count;count--) { this.context.on('stress-test:test:event',() => { var arr = new Array(5); }); } }, mem(req,res,e) { var context = this.context; var ntime = 1000; // 1 second var mark = new Date(); var now; this.count=0; do { this.context.manager.load(__dirname + "/memory-test-unit"); this.context.manager.unregister("memory-test-unit"); this.count++; now = new Date(); } while((now - mark)< ntime); // 500 ms res.write("Operations: " + this.count + "\n"); // Loop with tick // if(arg == "start") { var toggle = false; this.ci = context.setInterval(() => { if(toggle) { this.context.manager.load(__dirname + "/memory-test-unit"); }else { this.context.manager.unregister("memory-test-unit"); } },5000); } }, siege(req,res,e) { e.wait(); res.write("Doing 10s test!\n"); var proc = child.exec('siege -t10s http://127.0.0.1:3500/test'); res.write("\n"); //proc.stdout.pipe(res); proc.stderr.pipe(process.stderr); proc.on('close',() => { res.write("cmd done\n"); e.done(); }); } }