Simple load testing script (in bash)

Run

sh load-test.sh 20 "http://api.myserver.com/get_info"

This keeps hitting your server at the rate of 20 calls/second until you ask it to stop.

Code Explanation

There’s a while true loop that has a sleep 1 in it so the block get executed every second.

echo $(($(date +%s) — START)) | awk ‘{print int($1/60)”:”int($1%60)}’

This is for printing the current minute and second of the execution (12:45).

Then there’s a for loop that runs the number of times given as the first argument. Inside that, a function is called with the url and & so that it’s run in the background.

get $2 &

Inside the get function, we have a verbose silent curl to make the request. Then piped to tr removes all the newline. Then the following awk command appends a “- - - - - 05:23:19 PM” to it and logs it in /tmp/perf-test.log

awk -v date=”$(date +’%r’)” ‘{print $0"\n-----”, date}’ >> /tmp/perf-test.log

If you want to add custom headers, add it in the curl command in the file.

PS. Not having `inline code` options in medium is a bit painful

Edit: Medium has implemented inline code and it works great

Show your support

Clapping shows how much you appreciated Bharath Raja’s story.