Load Testing an nodejs API


Load Testing is quite a stream of its own all I did was smoothly sailed on top of it. With not much time in hand, the only weapon of choice was Jmeter, Well quite brilliant a tool!!



We had to test a facebook chat-bot built with nodejs. This app is a webhook for FB to push the chat messages down to our app.

This is how we achieved it

On Windows:
Download JMeter from here.

Now run the jmeter.bat, this will open up a java GUI where you can easily configure the endpoints you want to test.
  • Add a thread group and set the number of users/thread you want to spawn to test your API
  • Add an Http request sampler 
  • Fill in the intuitive fields like URL, request body, Method, etc.,
  • For setting up HTTP headers like content-type or authorization fields right click on HTTP request and add config element HTTP header manager, then the key values as you may
  • To see the results on all the requests that will spawn create add listener View results tree. 
  • You are ready to click on play button an check out the View results tree to verify the status.

There is a limitation to the number of threads that you can spawn in one machine, So I suggest couple of things 
  • use the command prompt to run this test and not the GUI, coz it may give up on you if the memory consumed.
  • Also if your machine does not have enough memory try running this same test from multiple machines.

So once you create the *.jmx file from the GUI.

Before you run the JMeter with multiple threads set the JVM memory to the max your system can take. Otherwise, you might end up with "java.lang.OutOfMemoryError" every time you cross the limit.

Open the JMeter.bat file in your fav editor and search for "set HEAP" then replace with

set  HEAP="-Xms512m -Xmx10480m"

*this number is used on my 16gb ram laptop and I was able to run 2k to 4k for threads.

 use the following command from the bin folder of JMeter

start /REALTIME java -jar ApacheJMeter.jar -n -t filename.jmx -l results1.jtl -j log1.log

Now there is a lot of ways you can get test data, the way we chose was CSV dataset. we had to add config element CSV data-set config on the http request. and make some minor changes on the request to pick the data from the CSV file. Use the csv header as variable in your json like below

{
    "sender": {"id": "${userid}"}
}

So userid is the column name or header which has to be specified in the CSV data-set config rather than CSV file itself. Each row in the CSV file will be considered as one use case so header cannot go in this file.
Here is s snapshot of the project structure



This is should be enough for a basic load test.

Happy load testing.

Comments

Popular posts from this blog

Launch .Net Core with VSCode for starters

Incognito mode in chrome

How to Run two .net core apps with debugging On using Visual Studio Code ?