Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const workerFarm = require('worker-farm')
- ...
- // Number of virtual clients/errors already launched/gathered
- let n = 0
- let nbErrors = 0
- const cpus = require('os').cpus().length
- // Perform some maths so that we get the closest possible of the concurrency with the same number of clients per CPU
- const options = {
- maxConcurrentWorkers: (concurrency < cpus ? concurrency : cpus),
- maxCallsPerWorker: Infinity,
- maxConcurrentCallsPerWorker: (concurrency < cpus ? 1 : Math.floor(concurrency / cpus)),
- maxConcurrentCalls: Infinity
- }
- // Total durations of client operations
- let durations = {}
- // Our farm of virtual clients
- const workers = workerFarm(options, require.resolve('./client'))
- for (var i = 0; i < total; i++) {
- // Default options include URL, client ID and the # of scenarios it has to execute
- let workerOptions = { url: program.url, index: i + 1, nbScenarios }
- // For the first/last clients include the ramp option as well
- if (i < concurrency) workerOptions.rampUp = ramp
- if (i > total - concurrency) workerOptions.rampDown = ramp
- workers(workerOptions, (err, client) => {
- n++
- if (!err) {
- Object.entries(client.durations).forEach(([key, value]) => {
- // Add client durations to total durations
- if (!durations[key]) durations[key] = 0
- durations[key] += client.durations[key]
- })
- } else {
- nbErrors++
- logger.error(err)
- }
- if (n === total) {
- workerFarm.end(workers)
- // Display total/averaged durations and error ratio
- Object.entries(durations).forEach(([key, value]) => {
- logger.info('Total ' + key + ' time = ' + durations[key].toFixed(2) + ' (s)')
- logger.info('Average ' + key + ' time = ' + (durations[key] / total).toFixed(2) + ' (s)')
- })
- logger.info('Error ratio = ' + (100 * nbErrors / total).toFixed(2) + ' %')
- }
- })
- }
Add Comment
Please, Sign In to add comment