$ brew install nghttp2
h2load for REST API benchmarking
Upasana | May 03, 2020 | 3 min read | 733 views
h2load is a benchmarking tool for HTTP/2 and HTTP/1 with support for SSL/TLS. h2load uses non-blocking io for making concurrent calls to target GET/POST Http endpoint, thus does not cause much load on the running system since single thread can make thousands of requests per second.
Installation
macOS
If you have homebrew installed on your mac, installing h2load is just a matter of running a single command:
Ubuntu
On ubuntu 18.04 or 20.04 LTS, you can run the below command:
$ sudo apt install nghttp2-client
Usage
- -n
-
the total number of requests. Default: 1
- -c
-
the number of concurrent clients. Default: 1
- -m
-
The max concurrent streams to issue per client. Default: 1
- -t
-
the number of native threads. Defaults: 1.
- --header
-
Add/Override a header to the request.
- -v
-
Output debug information.
- -h
-
displays help information.
- -p
-
can be used to specify protocol, by default HTTP/2 is used. Available protocols are:
h2c
andhttp/1.1
, default ish2c
. Alternatively, you can always use--h1
option to force HTTP/1.1 for both cleartext and SSL/TLS.
Basic load testing
HTTP GET endpoint benchmarking
below command will perform benchmark to URI http://localhost:8080/health.json using total 10000 requests, 100 concurrent clients and each client creating maxmium 10 concurrent streams.
h2load -n10000 -c100 -m10 --h1 "http://localhost:8080/health.json". (1)
1 | h1 flag is used to enforce HTTP/1.1 instead of default HTTP/2 which requires SSL/TLS connection. |
finished in 233.18s, 0.43 req/s, 105B/s
requests: 100 total, 100 started, 100 done, 100 succeeded, 0 failed, 0 errored, 0 timeout
status codes: 100 2xx, 0 3xx, 0 4xx, 0 5xx
traffic: 23.93KB (24500) total, 15.53KB (15900) headers (space savings 0.00%), 4.20KB (4300) data
min max mean sd +/- sd
time for request: 9.22s 29.59s 22.60s 3.01s 72.00%
time for connect: 599.74ms 599.90ms 599.80ms 51us 70.00%
time to 1st byte: 20.26s 24.39s 23.16s 1.47s 80.00%
req/s : 0.04 0.05 0.04 0.00 80.00%
For HTTP/2 endpoints, we do not need to supply --h1
flag, so the command will look like this:
h2load -n100 -c10 -m1 https://dapi.shunyafoundation.com/sidecar-pdf/health.json
HTTP POST endpoint benchmarking
For benchmarking POST endpoints, you first need to create a file containing request payload of POST request.
{
"query": "convert text into entities"
}
Now you have the file testpayload.json
, below command can run requests against POST endpoint on the server.
$ h2load -v http://host:port/v1/ner -d testpayload.json --h1 --header 'Content-Type: application/json' -n 500 -t 4 -c 4 -T 10
In the above command we have specified payload using -d flag and also, we specified custom timeout for each request to 10 seconds.
Time based load testing
h2load supports time based load-testing. Using this method will perform time based load testing in terms of a given duration instead of a pre-defined number of requests.
- --warm-up-time
-
warms up period
- --duration
-
duration in seconds for which benchmarking will run against the server, if supplied,
-n
option is ignored by h2load client.
For example, the below command will perform load-testing for 10 seconds after 5 seconds of warming up period.
h2load -c100 -m100 --duration=10 --warm-up-time=5 --h1 http://localhost:8080/health.json
Adding HTTP Headers
We can easily add HTTP headers in request using --header
flag, the below example illustrates this:
h2load -n1000 -c10 -m1 --header="accept-encoding: gzip" https://localhost:443/health.json
Request Timeout Headers
We can set connection timeout in seconds using available flags --connection-active-timeout
and --connection-inactivity-timeout
h2load -t2 -n10 -c10 -m1 --connection-active-timeout=3 --connection-inactivity-timeout=3 https://www.google.com/
When no timeout value is set (either active or inactive), h2load will keep a connection open indefinitely, waiting for a response.
Top articles in this category:
- Rest Assured API Testing Interview Questions
- 50 Java Interview Questions for SDET Automation Engineer
- Junit interview questions for SDET automation engineer
- Junit 5 Platform Launcher API
- Java Coding Problems for SDET Automation Engineer
- RestAssured multipart file upload
- Http methods for RESTful services