Full-Stack

Stress Test in NodeJS

NodeJS

Make Stress Test with NodeJS

You have got some API you would like to test. Your language of choice is Node.js.

The Scheme

Asynchronously generate multiple workers. Each worker is a child process invoked with a command-line. Listen to worker stdout and worker exit.

The Code

Main


var async = require('async');
var numWorkers = 200;

// construct multiple workers
async.times(numWorkers, function(n, next) {

    // construct args
    createChild(n, args, next);

});

function createChild(n, args, next) {

    var commandLine = ['./test/worker.js', n, args]; 
        var child = spawn('node', commandLine); 

        child.stdout.on('data', (data) => {
          console.log(`stdout: ${data}`);
        });

        child.stderr.on('data', (data) => {
          console.log(`stderr: ${data}`);
        });

        child.on('close', (code) => {
          console.log(`child process exited with code ${code}`);
          next(code);
        }); 

}

Worker


// worker.js

var n = process.argv[2];
var args = process.argv[3];

// do something

Yoram Kornatzky

Yoram Kornatzky