This guide aims at enabling the readers to be able to configure and load test multiple API’s with the ease of Apache JMeter tool. Apache JMeter is an open-source tool, which allows us to perform cross-platform testing, quick scope testing. It offers a lot of plugins and also enables load testing and stress testing.
What is an API ?
API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message, or check the weather on your phone, you’re using an API.
APIs are sometimes thought of as contracts, with documentation that represents an agreement between parties: If party 1 sends a remote request structured a particular way, this is how party 2’s software will respond.
Because APIs simplify how developers integrate new application components into an existing architecture, they help business and IT teams collaborate. Business needs often change quickly in response to ever shifting digital markets, where new competitors can change a whole industry with a new app. In order to stay competitive, it's important to support the rapid development and deployment of innovative services. Cloud-native application development is an identifiable way to increase development speed, and it relies on connecting a microservices application architecture through APIs.
Now that you have a slight knowledge about what an API is, let us advance with the configuration of Apache JMeter.
Configuring Apache JMeter:
...
Now start the JMeter application with the following command:
Code Block |
---|
./jmeter |
Testing The Api’s Using JMeter:
Once the JMeter application is running perfect, the interface will somewhat look like this depending upon the version you are using.
...
It divides the home window into two sections: the left side has all the test plan elements, and on the right side, all the configurations of that element are added.
As you are on the test plan element, you can see the configurations of the same.
Right-click on the test plan
Select the add option
Then in the menu that appears, select the threads option and then thread groups
Here, the number of threads refers to the number of users. The ramp-up period in seconds option states the time gap between the users' hits. There is a loop count where you can choose how many times the test will run for the number of users, and also, you can select the test to run infinite times.
There is also a scheduler here to help you schedule the test start-time and end-time.
For the purpose of this tutorial, let's set everything to one.
Now, select some sample APIs, as given below;
Go to your browser
Search for “some sample rest API for testing”
Open the first link http://reqres.in
Let’s create and run GET, POST, PUT, and DELETE Rest API requests in JMeter in the demo.
...
So, choose the first link: List Users.
Here, click on the request link
Open the link that appears in the new tab
Identify the URL for this API
Copy this URL and open the JMeter window
Then in the JMeter window, do the following:
Right-click on the Thread group
Navigate to “add”
In the box that appears, select “samplers”
You can see the types of requests that JMeter can work on
Choose the HTTP request.
Here, you will have to give the address to some home pages or websites
In the Server Name or IP box, give the paste the GET API request URL
In the Protocol box, put "HTTPS"
If the URL in the protocol was HTTP, this box would have been left empty. In the URL, put the URL link; remember not to put any slashes in the IP Address. Also, anything after the URL should be put in the path.
Now, put the path from the URL in the path box
Next, you must take part of the URL, beginning with the “question mark” and use it to assign the parameters. This is required since it is the GET API type.
Add the name in the parameters
Give the name as page and value as 2
Name this HTTP Request as GetApi
...
After this HTTP request is ready, perform the test on it. To determine what the results of the test will be, add some listeners.
Right-click on the Thread group
Go to listener options
In the box, check the different types of reports that JMeter provides
For now, Select two "view results tree" and “view results in table"
Then save the test and run it
Simply click on the green button to run the test.
After running the test, it's time to check the results:
Go to the "view results tree"
The green status shows that the test was successful.
Then go “view results in a table”
The green status again shows the test was successful.
Since the number of threads and loop was denoted as one - both at the entry in both the results, the test result has come out accordingly.
Now, try one more thing:
Go to the thread group
Change the number of threads to be 10
Run the test
After running the test, check the "results in the table" and see the testing being performed. This can be seen in case "view results in the table" is available too.
In the view results tree, you can click on a request and see different information. Here you can see the sample start, load time, latency error, etc. In the "view results in table," window, you will be able to see all the information in a tabular format.
...
You can also see the response data in the above image. This is the same data available when you copied the URL from the browser. Furthermore, the response headers and the request placed can also be checked.
Now, when you are done with the GET API request, let's add the second request that is the POST request.
Add an HTTP request again by going to the sampler in the thread group
Come back to the browser window
Click on the POST API option
Open the request URL in a new window
Go to this window and copy the URL
Come back to the JMeter window
Rename this HTTP request as Post API
Then paste the URL in the server name or IP box
Place HTTP in the protocol box, address in the Server or IP address box, and path in the path box
And in the HTTP Request drop-down, select the POST option this time
Navigate to the parameter part
Here, select the body data option and add body
For adding the body:
Go back to the browser window
Copy the body from here
Come back to the JMeter window
Paste the body here
...
Now, when the test is all set it’s time to check the output.
Go to the two listeners and clear the previous output
Then save the test and run it
Now you can see the two tests since the thread count is one
Making the thread count 10 again, you can see the test running a required number of times, 20 times. Furthermore, the information about different requests that include other parameters like latency, errors, etc., can be accessed.
Now, let's move on to the third API request, the PUT request.
Go back to the browser
Click on the PUT request
Open the requests in the new tab
Copy the URL
Come back to the JMeter window
Make a new HTTP request like the previous two times
Rename this HTTP request as Put API
Paste the URL in the server name or IP box
Put HTTP in the protocol box, address in the Server or IP address box, and path in the path box
And in this HTTP Request drop-down, select the PUT option this time
Here too, you will have to add body data.
Go back to the browser window
Copy the body from here
Coming back to the JMeter window, select the body data option and paste the body here.
...
Now, after it sets the test, run the test in the case of the previous two APIs.
Go to the two listeners and clear the previous output
Make the thread count as one
Save the test and run it
Again make the thread count as 10 and see what happens.
You can see the test running a required number of times
And a lot of information can be checked again, as in the previous cases
Now, let's add the last API request that is the DELETE request.
Go back to the browser
Click on the DELETE request
Open the requests in the new tab
Copy the URL
Come back to the JMeter window
Make a new HTTP request
Rename this HTTP request as Delete API
Paste the URL in the server name or IP box
Put HTTP in the protocol box, address in the Server or IP address box, and path in the path box
And in this HTTP Request drop-down, select the Delete option this time
...
Here you do not have to add anything in the parameters section.
So, go to the two listeners and clear the existing information
Save the test and run it
Since the number of threads was not 1 this time, the test can be seen running the required number of times. You can see the information for any API request by clicking on it.
...
Hence, you can now create and perform API Testing for different API requests in JMeter
Congratulations for making it through this guide. By now, you would have understood everything you need to know about JMeter API Testing. In the beginning, you learned what an API is, then you learned about the configuration of Apache JMeter. Then finally, you understood the process of performing API Testing using JMeter with a hands-on demo.