index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var child = require('child_process');
  2. var fs = require('fs');
  3. function hrToStr(diff) {
  4. var ldateDiff = ( diff[0] * 1000000 + diff[1] / 1000 ) / 1000;
  5. var ldateDiffStr = "";
  6. if(ldateDiff > 1000)
  7. ldateDiffStr = (ldateDiff / 1000).toFixed(2) + "s";
  8. else {
  9. ldateDiffStr = ldateDiff.toFixed(2) +"ms";
  10. }
  11. return ldateDiffStr;
  12. }
  13. module.exports.bundleActivator = {
  14. // Couple of tests
  15. start(context) {
  16. context.manager.load(__dirname + "/http-test");
  17. this.context = context;
  18. context.channel('core-shell:cmd')
  19. .on('test',(req,res,e) => {
  20. var cmds = ["events","cpu","mem","ping","siege"];
  21. var [subcmd,...args] = req.args;
  22. if(cmds.indexOf(subcmd) == -1) {
  23. res.write("Wrong\n");
  24. res.write("Available tests:" + cmds + "\n");
  25. return;
  26. };
  27. this[subcmd](req,res,e);
  28. });
  29. var lastPing;
  30. context.on('stress-test:ping',(e) => {
  31. lastPing = process.hrtime();
  32. context.emit('pong')
  33. e.done();
  34. });
  35. context.on('stress-test:pong',(e) => {
  36. var now = hrToStr(process.hrtime(lastPing));
  37. console.log("Time taken to poing: " + now);
  38. });
  39. },
  40. ping(req,res,e) {
  41. e.wait();
  42. this.context.emit('ping',e);
  43. },
  44. events(req,res,e) {
  45. var count = 1000000;
  46. for(;count;count--) {
  47. this.context.on('stress-test:test:event',() => { var arr = new Array(5); });
  48. }
  49. },
  50. mem(req,res,e) {
  51. var context = this.context;
  52. var ntime = 1000; // 1 second
  53. var mark = new Date();
  54. var now;
  55. this.count=0;
  56. do {
  57. this.context.manager.load(__dirname + "/memory-test-unit");
  58. this.context.manager.unregister("memory-test-unit");
  59. this.count++;
  60. now = new Date();
  61. } while((now - mark)< ntime); // 500 ms
  62. res.write("Operations: " + this.count + "\n");
  63. // Loop with tick
  64. //
  65. var toggle = false;
  66. this.ci = context.setInterval(() => {
  67. if(toggle) {
  68. this.context.manager.load(__dirname + "/memory-test-unit");
  69. }else {
  70. this.context.manager.unregister("memory-test-unit");
  71. }
  72. toggle = !toggle;
  73. },5000);
  74. },
  75. siege(req,res,e) {
  76. e.wait();
  77. res.write("Doing 10s test!\n");
  78. var proc = child.exec('siege -t10s http://127.0.0.1:3500/test');
  79. res.write("\n");
  80. //proc.stdout.pipe(res);
  81. proc.stderr.pipe(process.stderr);
  82. proc.on('close',() => {
  83. res.write("cmd done\n");
  84. e.done();
  85. });
  86. }
  87. }