DEV Community

Afshar Mohebi
Afshar Mohebi

Posted on

Multiple calling an API with JMeter

After getting acquainted with JMeter, it's time to solve a real problem. Recently, I am encountering a shameful yet common bug in our project: an special database field which its incremental behavior is controlled by the application rather than database is populated with repetitive values. The field has a strong business impact and should be instantly addressed. The bug is hard to be reproduced. Only one of customers have complained about. After some trial and error, I found that it may be caused by concurrency problems.

JMeter comes to play from now. Under correct settings of the tool, I was able to simulate the error. Each run of the JMeter results into the same bad results. Having this at disposal, development efforts can easily check whether their solution has resolved the issue correctly. Additionally, the test scenario helps to guess the root cause.

To have JMeter to load test the project, firstly, I created a new project. Afterwards, I added a Thread Group which was not hard to configure. I just set number of threads equal to 5 and set ramp-up to same value. In second step, I added a Sampler of type HTTP Request to the thread group. A thread group says how many threads will be running the test; while, a sampler is the thing which creates the requests and send them to the server. Basic configuration of HTTP Request is self explanatory: it just includes server name and, method and path.

By first run via command line, I saw nothing than a bunch of 401 unauthorized errors. The target API was configured to use JWT as the protection guard. It needed an Authorization header populated with a suitable token to work, just as Postman does. To do so, I added a HTTP Header Manager Config Element to the HTTP request. Then, added two headers, *Authorization to pass the token, and Content-Type set to application/x-www-form-urlencoded. The latter gives an ability to me to pass API parameters via HTTP request. And the final step was just running it in CLI mode:

jmeter -n -t CheckAPI.jmx -l LogFile.log
Enter fullscreen mode Exit fullscreen mode

Take a look at this simple but yet useful test project:

JMeter Test Project

Top comments (0)