Many times when we are developing an API we only do function tests, that is, tests that the whole process works correctly. But what happens when that process is thought to be consumed 100 times at the same time. How can we tell if our code is not broken? Or will our server be powerful enough to withstand something like this? If you’re currently going through something like this, this article is for you, stress tests with Postman
Many times we code our API so that everything works correctly and most of the time we leave the heaviest tests to be done in production, for example, see how our code reacts and see if the hardware that we assign to our server will be sufficient.
But there are ways of testing to find out if there is such a vulnerability in our API. For this, there are stress tests. And it is not only stressing our code but also the goal is stressing our server to see how it would react in very specific scenarios.
To achieve this goal we are going to use Postman. It is an excellent software to be able to test our APIs in a very dynamic and simple way. It is a tool that I recommend that you familiarize yourself with as soon as possible if you do not know it. Of course, if you are or want to be a back-end developer.
Before continuing I recommend that you check this article. Since we must have a basic understanding of Postman’s functionalities before continuing with the next paragraphs.
Ok, now that we have basic knowledge of the tool, now we can continue. First of all, we have to make an API. In this case, I will make a simple API in Node. Below you can see the code if you want to continue with the same example. But feel free to use the API of your choice.
Don’t forget to install the “express” and “body-parser” packages for the project to work properly.
Next, we are going to open Postman and the first thing we have to do is create a new collection that we will call “test”. For that, we position ourselves on the folder icon with the plus symbol.
Then inside the folder, we add one more called Get, finally, inside this folder, we create a request that we will call “Test”. Our diagram has to look like this.
The next thing is to configure our request. Based on our test API that we created earlier, our request will be a GET type method, then we put our URL with the name that we registered from our API. Finally, we press “Send” to test it. Our request has to look like this:
Remember that if you are using a different API, you must make sure to configure the parameters that the request needs well so that everything works correctly.
Well, we have our API call ready. The next thing is to make 15 calls at a time, to see how our application and server behave. For this, we go to the top of Postman. We will see a button called “Runner”, we press it and we will see a screen like these:
Then we select the folder that we created earlier “Test”. We configure our other parameters, in this example, we will not use Environment, so we can pass it, in the iterations we will put 15, in a delay we will put 0, this parameter can be used in the future if we want to do tests with waiting time in each request.
Next, we have the Log answers parameter, here we can tell you if we want the logs at all times or only when an error occurs. Finally, we have the Data parameter that will work for us to be able to add files if our request needs it.
Once we have our parameters ready, we should have something like this:
When we run our tests, we will see a panel with each of them and their selected status of each one. We can see in detail the time that the query took and in the event that we have an error, the log of what happened can be seen.
Our API console should look like this:
Finally, we can see a list of our tests to store them, in case it to be necessary to consult for future tests.
Maybe this example does not demonstrate the point I want to get to very well, but let’s imagine a more robust method, where we have to send a lot of information including an attached image. Simply a method that needs more resources.
So it is necessary to perform stress tests to monitor their behavior when receiving 100 or more requests. In this way, you will realize if you need more resources at the server level or you can also find imperfections in the code and therefore improve them.
I recommend that you always stress test your apps as most of them are meant to be used by millions of people. I hope I have helped or at least encourage you to research more on the subject.