Skip to content

Instantly share code, notes, and snippets.

@Lucho00Cuba
Created September 12, 2022 14:55
Show Gist options
  • Select an option

  • Save Lucho00Cuba/c2efa8669d031a1cc971b7d732e40de8 to your computer and use it in GitHub Desktop.

Select an option

Save Lucho00Cuba/c2efa8669d031a1cc971b7d732e40de8 to your computer and use it in GitHub Desktop.
Stress Test HTTP
#!/usr/bin/env bash
TIME=0
pidlist=""
show_help() {
cat << EOF
Naive Stress Test with cURL.
Usage: ./stress-test.sh [-a ADDRESS] [-c CONCURRENCY] [-r REQUESTS] [-p PROTOCOL]
Params:
-a address to be tested.
-c conccurency: how many process to spawn
-r number of requests per process
-p protocol (http||https)
-s time between requests (seconds). Value default is 2
-h show this help text
Example:
$ ./stress-test.sh -a localhost:8080 -c 1 -r 5 -p http -s 10 (5 requests to http://localhost:8080)
EOF
}
#### CLI
declare -i param
param=$#
if (( $param == 0 || $param < 4 )); then
show_help
exit 0
fi
while getopts ":a:c:r:p:s:h" opt; do
case $opt in
a)
ADDRESS=$OPTARG
;;
c)
CONCURRENCY=$OPTARG
;;
r)
REQUESTS=$OPTARG
;;
p)
PROTOCOL=$OPTARG
;;
s)
TIME=$OPTARG
;;
h)
show_help
exit 0
;;
\?)
show_help >&2
echo "Invalid argument: $OPTARG" &2
exit 1
;;
esac
done
shift $((OPTIND-1))
#### Main
stress(){
nreq=""
for req in `seq 1 $REQUESTS`; do
curl -vsk "$PROTOCOL://$ADDRESS/req-$con-$req" 1>>curl.out 2>>curl.err & pidlist="$pidlist $!"
if [[ $TIME -ge 1 ]]; then
sleep $TIME
fi
done
# Execute and wait
FAIL=0
for job in $pidlist; do
echo "CLIENT $1 - ID_REQ: $job"
wait $job || let "FAIL += 1"
done
# Verify if any failed
if [ "$FAIL" -eq 0 ]; then
echo "CLIENT $1 - STRESS TEST SUCCESS!"
else
echo "Failed Requests: ($FAIL)"
fi
}
for con in `seq 1 $CONCURRENCY`; do
stress $con &
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment