others-Use Apache Bench(ab) command to test RESTful apis example

1. Introduction

In this post, I would demo how to use apache bench(ab) command to test RESTful apis, I would demo the http get and http post(json) with ab test, and I would introduce how to repeatedly execute the comand over and over without user intervention.

If you prefer the GUI graphical tool like postman to test REST apis, you can read this article:

2. The api

You can refer to this post to view the source code of the api, to summary, there are two RESTapis to test:

  • Http Get api
    • The url is: http://localhost:8080/studentsApi/student/1
    • It would return the No.1 Student in the database
  • Http Post api(JSON)
    • The url is: http://localhost:8080/studentsApi/newStudent
    • It would require the client to post the object as JSON format string

3. Use Apache Bench(ab) test RESTful apis

Apache Bench(also known as ab) is a benchmark test commandline tool in linux and macos, and it’s a single-threaded command line computer program for measuring the performance of HTTP web servers. Originally designed to test the Apache HTTP Server, it is generic enough to test any web server.

To make sure you can use the ab command ,just execute it in a terminal client like this:


You should see this result: ab-test-restful-api-11

If not ,install the ab tool by using yum:

yum install httpd-tools

Then run the ab command again, you should continue now!

3.1 The simple ab command example

The following command

ab -n 100 -c 10 "http://en.wikipedia.org/wiki/Main_Page"

will execute 100 HTTP GET requests, processing up to 10 requests concurrently, to the specified URL, in this example, “http://en.wikipedia.org/wiki/Main_Page”.

3.2 Test the RESTful Http Get api

Just try this:

ab -n 10 -c 2 http://localhost:8080/studentsApi/student/1

The test would send 10 requests to the server with concurrency of 2.

The result is: postman-test-restful-api-12

You can see that:

  • You have sent totally 10 requests to the server
  • Your concurrency is 2
  • All requests are success

3.3 Test the RESTful Http Post api

If you want to post a JSON to server via ab tool, just use the following options supplied by ab:

  • -p means to POST it
  • -T sets the Content-Type
  • -c is concurrent clients
  • -n is the number of requests to run in the test

Next, we should prepare a JSON file to be posted by ab: create a file named student.json with the following content:

{"name":"jackauto","branch":"it","percentage":20,"phone":1211232,"email":"[email protected]"}

And execute the ab command in the same directory of the student.json

ab -p student.json -T application/json -c 1 -n 1 http://localhost:8080/studentsApi/newStudent

This command means I want to send 1 request to server with concurrency of 1. After execution, there should be only 1 post request that processed by server.

3.4 Run the test repeatedly without intervention

What if we want to run the test repeatedly(by specified interval)? You can just use the watch command.

3.4.1 What is watch command

Watch is used to run any arbitrary command at regular intervals and displays the output of the command on the terminal window.

It is useful when you have to execute a command repeatedly and watch the command output change over time. For example, you can use the watch command to monitor the system uptime or disk usage.

The watch utility is a part of the procps (or procps-ng) package which is pre-installed on nearly all Linux distributions.

The command example is:


For example, we watch the date of system:

watch date

It would open a dedicated screen to show the datetime of system, and update the datetime every 2 seconds.

3.4.2 How to use watch command with ab

Just try this example:

watch -n 1 ab -p student.json -T application/json -c 1 -n 1 http://localhost:8080/studentsApi/newStudent

It would repeatedly execute the ab test and result in inserting new record into the database every 1 second.

Cool, isn’t it?

4. Summary

Use the ab command line to test RESTful apis is easy , and if you want to use graphical UI to test the RESTful apis, just use postman , I have writen an article about it, you can refer to this article.