index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.prefix('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',() => {
  31. lastPing = process.hrtime();
  32. context.emit('pong')
  33. });
  34. context.on('stress-test:pong',() => {
  35. var now = hrToStr(process.hrtime(lastPing));
  36. console.log("Time taken to poing: " + now);
  37. });
  38. },
  39. ping() {
  40. this.context.emit('ping');
  41. },
  42. events(req,res,e) {
  43. var count = 1000000;
  44. for(;count;count--) {
  45. this.context.on('stress-test:test:event',() => { var arr = new Array(5); });
  46. }
  47. },
  48. mem(req,res,e) {
  49. var context = this.context;
  50. var ntime = 1000; // 1 second
  51. var mark = new Date();
  52. var now;
  53. this.count=0;
  54. do {
  55. this.context.manager.load(__dirname + "/memory-test-unit");
  56. this.context.manager.unregister("memory-test-unit");
  57. this.count++;
  58. now = new Date();
  59. } while((now - mark)< ntime); // 500 ms
  60. res.write("Operations: " + this.count + "\n");
  61. // Loop with tick
  62. //
  63. if(arg == "start") {
  64. var toggle = false;
  65. this.ci = context.setInterval(() => {
  66. if(toggle) {
  67. this.context.manager.load(__dirname + "/memory-test-unit");
  68. }else {
  69. this.context.manager.unregister("memory-test-unit");
  70. }
  71. },5000);
  72. }
  73. },
  74. siege(req,res,e) {
  75. e.wait();
  76. res.write("Doing 10s test!\n");
  77. var proc = child.exec('siege -t10s http://127.0.0.1:3500/test');
  78. res.write("\n");
  79. //proc.stdout.pipe(res);
  80. proc.stderr.pipe(process.stderr);
  81. proc.on('close',() => {
  82. res.write("cmd done\n");
  83. e.done();
  84. });
  85. }
  86. }