Simulate several clients in nodejs

3

How can I simulate several clients using nodejs ? Is there a module?

    
asked by Kevin AB 01.07.2016 в 19:11
source

1 answer

2

I did it with mocha, for that you must install it globally with the following command.

npm install -g mocha

You must also have the next library.

npm install socket.io-client

After having these two elements, in your project you should create a folder called / test with a file called test.js, in my case this file contains the following.

var mocha = require('mocha');
var io    = require('socket.io-client');

describe("conexion", function () {

    var server,
        options ={
            transports: ['websocket'],
            'force new connection': true
        };

    var con_client = [];
    //conexiones de los usuarios simulados
    var num_client = 10;

    beforeEach(function (done) {
        // start the server
        // dirección de archivo a testear
        server = require('../app').server;

        done();

    });

    it("conexion de clientes", function (done) {
        //emulacion de clientes
        for (var i = num_client; i >= 0; i--) {
            con_client[i] = io.connect("http://localhost:3000", options);

            con_client[i].once("connection", function () {
                con_client.disconnect();
            });
        };

        //tiempo limite de ejecución de la prueba
        //colocar un tiempo menor al de ejecución por parte
        //del comando
        //cmd: mocha --timeout 15000
        setTimeout(done, 10000);

    });

});

In the variable var num_client = 10; we specify how many clients you want to simulate and in this line server = require('../app').server; we specify the path of the file to which we will perform the simulation of clients, setTimeout(done, 10000); this line determines the time of the simulation, it must be smaller to the execution of the mocha test that we will see later, if we do not do so, mocha will show us the following error.

  

Error: timeout of 10000ms exceeded. Ensure the done () callback is being called in this test.

To avoid that error, we execute the following command with 15 seconds.

mocha --timeout 15000

Where 15000 is the time that the test will be active.

Here is an image of the 10 clients that simulate, I hope it helps you.

    
answered by 20.07.2016 в 20:57