Skip to content

Instantly share code, notes, and snippets.

@Xunnamius
Last active May 6, 2020 16:55
Show Gist options
  • Save Xunnamius/01b73faa6e14d4be1759333f17b1b3fa to your computer and use it in GitHub Desktop.
Save Xunnamius/01b73faa6e14d4be1759333f17b1b3fa to your computer and use it in GitHub Desktop.
Blitz, the dirty server stress tester
#!/bin/bash
# A little program to quickly stress test a server
# blitz <url> <# of requests> <method>
# <url> defaults to localhost
# <# of requests> defaults to 30
# <method> defaults to POST
# Probably a good idea to type method names with capital letters
declare -a pids
requests=$([ -z "$2" ] && echo "30" || echo $2)
uri=$([ -z "$1" ] && echo "localhost" || echo $1)
method=$([ -z "$3" ] && echo "POST" || echo $3)
echo 'Pinging ''"'"$uri"'"'" $requests times ($method)..."
waitPids() {
while [ ${#pids[@]} -ne 0 ]; do
local range=$(eval echo {0..$((${#pids[@]}-1))})
local i
for i in $range; do
if ! kill -0 ${pids[$i]} 2> /dev/null; then
unset pids[$i]
fi
done
pids=("${pids[@]}") # Expunge nulls created by unset.
sleep 1
done
echo "Done!"
}
addPid() {
pid=$1
pids=(${pids[@]} $pid)
}
for (( c=0; c<$requests; c++ ))
do
(curl -X $method -Is $uri | head -n1) &
addPid $!
done
waitPids
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment