load 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env node
  2. 'use strict';
  3. const async = require('async'),
  4. program = require('commander'),
  5. request = require('request');
  6. program
  7. .version('0.1.0')
  8. .usage('-n total -c concurrency')
  9. .option('-n, --total <total>', 'Total requests', parseInt)
  10. .option('-c, --concurrency <concurrent>', 'Concurrent requests', parseInt)
  11. .parse(process.argv);
  12. const totalRequests = program.total || 100, concurrency = program.concurrency || 10;
  13. console.log(`Making ${totalRequests} with a concurrency of ${concurrency}`);
  14. const coll = Array(totalRequests).fill().map((x,i)=>i);
  15. const startTime = new Date();
  16. var responses = [ ];
  17. function printStats() {
  18. console.log('Errored:', responses.filter((x) => x.error).length);
  19. console.log('OK:', responses.filter((x) => x.statusCode === 200).length);
  20. console.log(`Total time: ${(new Date() - startTime)/1000} seconds`);
  21. console.log('done', responses.length);
  22. }
  23. async.mapLimit(coll, concurrency, function makeRequest(i, iteratorDone) {
  24. request('http://test.smartserver.space', function (error, response, body) {
  25. responses[i] = { error: error, statusCode: response && response.statusCode };
  26. iteratorDone(null, responses[i]);
  27. });
  28. }, function (error, results) {
  29. printStats();
  30. });
  31. process.on('SIGINT', function () {
  32. printStats();
  33. process.exit();
  34. });