Skip to content

Instantly share code, notes, and snippets.

@adrianocanofre
Last active April 29, 2024 11:17
Show Gist options
  • Save adrianocanofre/f979726d1a560b6ab508939612a7d86c to your computer and use it in GitHub Desktop.
Save adrianocanofre/f979726d1a560b6ab508939612a7d86c to your computer and use it in GitHub Desktop.
Script to load test, using cURL.
#!/bin/bash
CONCURRENCY=1
REQUESTS=10
ADDRESS="http://localhos:8080"
TIMEOUT=2
show_help() {
cat << EOF
Naive Stress Test with cURL.
Usage: ./stress-test.sh [-a ADDRESS] [-c CONCURRENCY] [-r REQUESTS]
Params:
-a address to be tested.
Defaults to localhost:8080
-c conccurency: how many process to spawn
Defaults to 1
-r number of requests per process
Defaults to 10
-t timeout per request in seconds
Defaults 2s
-h show this help text
Example:
$ ./load-test.sh -c 4 -r 100 (400 requests to localhost:8000)
EOF
}
while getopts ":a:c:r:t:h" opt; do
case $opt in
a)
ADDRESS=$OPTARG
;;
c)
CONCURRENCY=$OPTARG
;;
r)
REQUESTS=$OPTARG
;;
t)
TIMEOUT=$OPTARG
;;
h)
show_help
exit 0
;;
\?)
show_help >&2
echo "Invalid argument: $OPTARG" &2
exit 1
;;
esac
done
shift $((OPTIND-1))
#### Main
echo ""
echo "###########################"
echo "######## Load Test ########"
echo "###########################"
echo ""
echo -e "DATE: $(date) \nURL: $ADDRESS \nrate: $REQUESTS calls/second"
START="$(date -u +%s.%3N)"
for i in `seq 1 $CONCURRENCY`; do
curl -s --output /dev/null "$ADDRESS?[1-$REQUESTS] --connect-timeout $TIMEOUT " & pidlist="$pidlist $!"
echo "Finalized $((REQUESTS*i)) requests "
done
# Execute and wait
FAIL=0
for job in $pidlist; do
#echo $job
wait $job || let "FAIL += 1"
done
# Verify if any failed
END="$(date -u +%s.%3N)"
echo "Failed Requests : ($FAIL) de ($((CONCURRENCY * REQUESTS))) "
TOTAL="$(bc <<<"scale=5;$END-$START")"
TIMEREQUEST=`echo "scale=5;$TOTAL / $REQUESTS * $CONCURRENCY" | bc -l`
echo "Total Time: $TOTAL [s]"
RT=`echo "scale=5;$TIMEREQUEST/1000" | bc -l`
echo "Time/Request: " $RT
@josep11
Copy link

josep11 commented Apr 29, 2024

There is a typo with the default address constant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment