JMeter 101: Load Test a Web API

One of the most crutial things for a Web API is to know how many resources you'll need to run it, this based on how much traffic you will (or expect to) receive, the response time of your API and the payload (the content that it will retrieve). And when I mention resources, I'm talking about how big/small your server should be, the good part right now is that we have the cloud, where we can experiment in minutes and just with a few clicks. But today, I'm not going to talk about the cloud, I'm going to focus on how to prepare our local environment to run this set of tests in our machines.

What will you need?

First thing first, we will need an API to test, for this we're going to create a simple one, actually let's use the one from the VS templates. We can do that by following these steps: 1. Open your Visual Studio (2015 or higher, try get up to date) 2. In the left pannel, choose: "Visual C#" and then ".NET Core" 3. In the middle pannel, choose: "Create a new ASP.NET Core Web Application (.NET Core)" 4. Put a name to your project (I put "HelloWorld") 5. Choose a location to save the all project files 6. Click "OK"

Easy, right? Here's an image of what I see:

Create Web API in .NET Core

Now, you need to download JMeter (you'll need Java to run it) here: http://jmeter.apache.org/download_jmeter.cgi. Extract it and go to the bin folder, you'll find a file called: "ApacheJMeter.jar", run it and the UI will appear.

JMeter

OK, now you're good to go, let's start having fun.

How to create my first load test?

It's really easy and we're going to do it in just a few steps, at the end I'll share with you the link fo the xml configuration of the test, let's start:

  1. Open JMeter (I guess you already did it after donwload it in previous step)

  2. Right click in "Test Plan" and go to Add -> Threads (Users) -> Thread Group. This is where you will configure the users that JMeter launch, lets put 10 in "Number of Threads (users)" to simulate 10 concurrent users. You can also configure how much time the test will run, check the “Scheduler” box and in this case we will put 30 in “Duration (seconds)” so the load test runs for 30 seconds.

  3. JMeter will try to send as much load as it can, so we need to put something to assure a constant throughput to the API, do to this you need to right click in "Test Plan" and go to Add -> Timer -> Constan Throughput Timer. This is the maximum number of requests that it will send in one minute, because we already configure the test for 10 users let’s assume that it will send 10 requests per second (rps), in one minute it will send 600 requests, so put 600 in “Target throughput (in samples per minute)” box.

  4. We need something to test, an endpoint. So let’s run the API in Visual Studio (VS) in debug mode for now and let VS launch the IIS express, it will assign a port so make sure you have it handy.

  5. Let’s get back to JMeter and in “Thread Group” right click and go to Add -> Sampler -> HTTP Request. You can configure now the endpoint, in “Server Name or IP” just put “localhost” (this where you will put the IP of the service or the subdomain name). Specify the port number that you were assigned in the previous step, in my case it was 62029, so I put that number in “Port Number” box. You need to also specify the route of the API to test, so in “Path” it’s the place to put it, in my case I put “/api/filelog” because that was the endpoint in my IP. You can also configure parameters in the request but we will leave this example as simple as possible.

  6. The good thing about JMeter is that you can see some output while the test is running, so let’s add a few. In “Thread Group” right click and go to Add -> Listener -> Aggregate Report. With this you’ll be able to see the number of request sent in total, the average latency in milliseconds (ms), the median of the latency, 90th percentile latency, the 95th and the 99th (don’t trust in averages please), minimum latency, maximum latency, percentage of requests with errors from total sent, the throughput (rps), data received and sent in total.

  7. You can also add a response time graph to the test, to do this go to “Thread Group” right click and go to Add -> Listener -> Response Time Graph. So, when the load test is running you can see a graph of how the latency is behaving.

  8. You’re done, so let’s run the test by going to the “Run” (Alt + R) menu and click in “Start”

As you can see, in eight steps you were able to create a simple load test with JMeter, so now practice adding more listeners and keep playing with the configuration, add more users to the test and see how your API behaves, you might find pretty interesting things.

Here’s the final screen of how your test plan should look:

JMeter

You can download the configuration file here, have fun!