Guest User

Untitled

a guest
Mar 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. const workerFarm = require('worker-farm')
  2. ...
  3.  
  4. // Number of virtual clients/errors already launched/gathered
  5. let n = 0
  6. let nbErrors = 0
  7. const cpus = require('os').cpus().length
  8. // Perform some maths so that we get the closest possible of the concurrency with the same number of clients per CPU
  9. const options = {
  10. maxConcurrentWorkers: (concurrency < cpus ? concurrency : cpus),
  11. maxCallsPerWorker: Infinity,
  12. maxConcurrentCallsPerWorker: (concurrency < cpus ? 1 : Math.floor(concurrency / cpus)),
  13. maxConcurrentCalls: Infinity
  14. }
  15. // Total durations of client operations
  16. let durations = {}
  17. // Our farm of virtual clients
  18. const workers = workerFarm(options, require.resolve('./client'))
  19.  
  20. for (var i = 0; i < total; i++) {
  21. // Default options include URL, client ID and the # of scenarios it has to execute
  22. let workerOptions = { url: program.url, index: i + 1, nbScenarios }
  23. // For the first/last clients include the ramp option as well
  24. if (i < concurrency) workerOptions.rampUp = ramp
  25. if (i > total - concurrency) workerOptions.rampDown = ramp
  26. workers(workerOptions, (err, client) => {
  27. n++
  28. if (!err) {
  29. Object.entries(client.durations).forEach(([key, value]) => {
  30. // Add client durations to total durations
  31. if (!durations[key]) durations[key] = 0
  32. durations[key] += client.durations[key]
  33. })
  34. } else {
  35. nbErrors++
  36. logger.error(err)
  37. }
  38. if (n === total) {
  39. workerFarm.end(workers)
  40. // Display total/averaged durations and error ratio
  41. Object.entries(durations).forEach(([key, value]) => {
  42. logger.info('Total ' + key + ' time = ' + durations[key].toFixed(2) + ' (s)')
  43. logger.info('Average ' + key + ' time = ' + (durations[key] / total).toFixed(2) + ' (s)')
  44. })
  45. logger.info('Error ratio = ' + (100 * nbErrors / total).toFixed(2) + ' %')
  46. }
  47. })
  48. }
Add Comment
Please, Sign In to add comment