top of page
  • Writer's pictureSam Shetty

Hey !!! Load testing for developers


At Scale, Everything Breaks ​!!!

"How do I generate load for my API locally ?" has been a question I have encountered quite frequently, especially when the CPU and Memory stats spike up in the production environment. I am writing this post to walk you through the whats, whens, whys and hows of load testing in your own workspace using Hey. WHAT is Hey ?

Hey is a lightweight cli program which sends load to your application. WHEN to use Hey ?

As developers, load testing our API is not the first thing on our mind. That could partly be due to the pre setup we envision before running these tests. What if we could run load tests from my cli or even better write bash scripts to run a sequence of APIs at scale. This is where a load test cli program would be a true winner. WHY to use Hey ? Other load test applications like JMeter, Gatling etc while are useful for running integrated load tests in the cloud, we often need to simulate load locally on our development workspace to identify memory leaks, cpu intensive functions etc. This is where Hey comes in. It is open source and written in go. You can fork it locally and modify it to to meet your needs as there is no learning curve. Locust works in the same space as hey, Locust needs some basic python skills while Hey works with just CLI params.​ There is no learning curve. HOW to use Hey ?

5 Steps to get started with Hey:

Step 1: Install Hey brew install hey OR go get -u github.com/rakyll/hey Step 2: Read the list of Parameters


	-n  Number of requests to run. Default is 200.
  	-c  Number of workers to run concurrently. Total number of requests cannot be smaller than the concurrency level. Default is 50.
	-z  Duration of application to send requests. When duration is reached,
      application stops and exits. If duration is specified, n is ignored.
      Examples: -z 10s -z 3m.
 	-m  HTTP method, one of GET, POST, PUT, DELETE, HEAD, OPTIONS.
  	-H  Custom HTTP header. You can specify as many as needed by repeating the flag.
      For example, -H "Accept: text/html" -H "Content-Type: application/xml"
	-d  HTTP request body.
  	-D  HTTP request body from file. For example, /home/user/file.txt or ./file.txt.

There are more parameters which you can explore here Step 3: Examples of Hey 1. With Headers: You can initialize your header values for instance bearer tokens in your system envar. $auth_token is an envar in this example.


> hey -H "Authorization: Bearer $auth_token" -m POST -D $absolute_path_json_file -T application/json -c 10 -z 5s ​http://localhost:9040/query

2. With Number of requests: This will run 100 API calls to the server. The -d flag is when you need to give a raw JSON request. To use a request file instead add the -D flag.

> hey -m POST -d '{"query":"query{\n\ttrainers{\n    name\n    city\n    licenseID\n    licenseState\n    }\n}","variables":{}}' -T application/json -n 100 http://localhost:8080/v2/gql

3. With time to run load and concurrency factor: -z 5s -c 10 : will run the requests for 5 seconds with 10 workers at a time

> hey -m POST -d '{"query":"query{\n\ttrainers{\n    name\n    city\n    licenseID\n    licenseState\n    }\n}","variables":{}}' -T application/json -c 10 -z 5s http://localhost:8080/v2/gql

Step 4: Hey Output:

Output of hey displays various request and response stats.

Step 5: Scripting with Hey Run hey n number of times for a sequence of APIs.


request_count=$2
concurrency=$3
url=$5

#Run commands sequentially using hey n number of times 
max=10
for (( i=1; i <= $max; ++i ))
do
    hey -n $request_count -c $concurrency -m POST -D 
$list_trainers_json -T application/json $url >> trainers.out

    hey -n $request_count -c $concurrency -m POST -D     $get_trainer_by_id_json -T application/json $url >> trainers.out
done

Conclusion:

Performance tests today have become an integral part of the testing life cycle. Often we encounter well behaved and docile code runs rampant at scale. This post is just a small example of the vast range of things we can do with "hey". It is definitely a tool I would be using in my personal workspace.

References: Source code: https://github.com/rakyll/hey


0 comments

Recent Posts

See All

Comments


bottom of page