How to use Artillery for Load testing on APIs?
Regardless of the product, you are creating, it is always important to ensure the quality of the product by making a battery of tests before placing it on the market. When it comes to software development, some metrics are essential to make clear to everyone involve in the project.
That could include its users, how reliable a particular system/application is to support public use.
Within the area of software quality, several types of tests aim to achieve the objective mentioned above, to show everyone that the product is stable and robust, some of them: integration test, unit test, penetration test, regression test, and so on.
What we aim?
This blog aims to talk a little about the load test. Which we create in essence to simulate different amounts of the attempt to access a given system or device.
To have as output a report of how the software behaved in a given scenario.
When we talk about APIs and infrastructure escalation, it’s interesting to know the exact number of requests that the server (or servers) can respond correctly at an acceptable time for its clients.
In case you’ve ever tried to do something like that, you’ve probably come across JMeter.
Which is one of the most famous and complete tools for this kind of work.
However, the learning curve with JMeter is a bit long, as there are many settings/options that the user ends up getting lost at the beginning until they find what they need for their case.
In an attempt to make this load testing process a little more user-friendly. Artillery, a tool that with a few steps allows you to simulate various types of scenarios for testing services. They are used for HTTP communication and/or web sockets.
To install Artillery:
npm install -g artillery
To test your installation:
If a dinosaur has appeared in your terminal, it’s all right and you can move on.
To start running your load tests, you need to create a configuration file. You can give it any name, but for this article, I’ll create one called artillery.yml.
In this, you will put all the information about your API, such as endpoints, routes, and scenarios.
You can both test isolated routes/features, and more complex scenarios, such as a purchase process in e-commerce.
which would have one route to search for products, another for checkout, and another for payment.
Below is an example of this file:
In the file above we put the endpoint of our API, the duration attribute represents the duration of this test cycle in seconds, and the arrival rate the number of new users per second.
To run the test, run the following command:
artillery run artillery.yml
After execution, we have the following result:
All-time metrics are in Millis, RPS (request per second), codes are HTTP codes, and the number of responses with the same, in the case above.
We had 1200 (60×20, as we configured) requests in 60 seconds, and all returned 200. Scenarios launched are the ‘virtual users’ created and Completed Scenarios are how many of them have managed to run the scenario successfully.
Important: While the test is running, a preview of the result is being printed on the terminal every 10 seconds, but only at the end do you have the consolidated numbers of the full test.
Now you can go on changing competing user numbers, amount of test time, new scenarios, simulating more complex flows, and so on.