How to do performance testing on NodeJS application

So you have developed some nodeJS application and now you want to do some performance testing on your application.

So in this article i can suggest you some free tools, by using that we can do some basic level of performance testing of our nodeJS application.

There is one tool called “siege” which is available as a package for Linux and also available as nodeJS module to do the performance testing in nodeJS application.

Following are the steps to use “Siege” for the performance testing

  • You can create a new folder or you can also use existing project folder
  • Now install a NPM moduleĀ in your new or existing folder. But as you know, before typing the following command, you should stay in the project directory and type the command.
1
npm install siege
  • Now create a new “JS” file called “performanceTest.js”
  • Inside “performanceTest.js” add the following lines of code
1
2
3
4
5
6
var siege = include('siege');
siege()
  .on(3000)
  .for(10000).times
  .get('/')
  .attack()

Now save the file and run it. Type the following command to run the file. I guess you already know this command

1
node performanceTest.js

But for your case, you might need to change the “Siege” code, as i am assuming in my code the port number if “3000”, so please change the port number to your desired port number before the testing. I can give you one clue. Support your application is running on port 8080, so you need to write following code in index.js

1
2
3
4
5
6
var siege = include('siege');
siege()
  .on(8080)
  .for(10000).times
  .get('/')
  .attack()

I should explain the meaning of this code as well as i should give you the tricks to test some advanced level of setting of “siege”

We are asking siege to attack of port “3000” and it will request “10,000” times with url as following http://localhost:3000/.

Lets say there is one routing available in your nodeJS or expressJS application, you want to test that URL, following are the the codes to test the same

1
2
3
4
5
6
var siege = include('siege');
siege()
  .on(8080)
  .for(10000).times
  .get('/user/list')
  .attack()

This will test the following URL http://localhost:3000/user/list for 10,000 times and it will show the result on console itself.

Support you want to test a post request with parameters supplied, following will be the code.

1
2
3
4
5
6
var siege = include('siege');
siege()
  .on(8080)
  .for(5000).times
  .post('/user/signup', {userName: 'bikash'})
  .attack()

This will test following url http://localhost:3000/user/signup with post request. But it will request for 5000 times. So before write such test cases, try to point your application to a staging database, so that you can remove all your test data from database

You can write 2 task at a time, to do that you need to write following type of test cases.

1
2
3
4
5
6
7
var siege = include('siege');
siege()
  .on(8080)
  .for(5000).times
  .get('/').for(2).seconds
  .post('/user/signup', {userName: 'bikash'}).for(3000).times
  .attack()

Happy coding :)