Using h2load for REST API benchmarking

Carvia Tech | March 22, 2019 | 3 min read | 276 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 endpoint, thus does not cause much load on the running system since single thread can make thousands of requests per second.

Installation on macOS

If you have homebrew installed on your mac, installing h2load is just a matter of running a single command:

brew install nghttp2

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 and http/1.1, default is h2c. Alternatively, you can always use --h1 option to force HTTP/1.1 for both cleartext and SSL/TLS.

Basic load testing

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.

Benchmarking HTTP/1.1 endpoint using h2load
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.
Results will look like below:
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:

Benchmarking HTTP/2 endpoint using h2load
h2load -n100 -c10 -m1 https://dapi.shunyafoundation.com/sidecar-pdf/health.json
Results
finished in 483.09ms, 207.00 req/s, 19.41KB/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: 9.38KB (9600) total, 5.58KB (5710) headers (space savings 41.73%), 1.56KB (1600) data
                     min         max         mean         sd        +/- sd
time for request:     4.60ms    127.90ms     16.85ms     23.37ms    95.00%
time for connect:    44.08ms    238.06ms    149.53ms     94.32ms    60.00%
time to 1st byte:    54.88ms    347.46ms    215.60ms    143.15ms    70.00%
req/s           :      20.76       77.99       44.12       26.95    60.00%

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:
  1. SDET: Rest Assured Interview Questions
  2. Top 50 SDET Java Programming Interview Questions & Answers
  3. SDET: JUnit interview questions for automation engineer
  4. Top 15 coding problems for SDET Java Interviews
  5. Commonly used Http methods in RESTful services
  6. Junit 5 Platform Launcher API
  7. REST Assured vs Apache HttpClient and RestTemplate


Find more on this topic:
SDET Interviews image
SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info