Skip to content

Instantly share code, notes, and snippets.

@bernardmcmanus
Last active September 1, 2016 14:26
Show Gist options
  • Save bernardmcmanus/0b1fa2e5c9a799b049cca4a3e3e72129 to your computer and use it in GitHub Desktop.
Save bernardmcmanus/0b1fa2e5c9a799b049cca4a3e3e72129 to your computer and use it in GitHub Desktop.
Simple, concurrent HTTP load test in bash
#!/bin/bash
# Config
concurrency=20
lag=0.05
precision=3
curl_format="%{time_total}"
# Runtime
pids=()
batch=0
tmp="$(mktemp -d "/tmp/$(basename "$0").XXXXXX")" || {
echo "Error creating temporary directory" >&2
exit 1
}
function parse_args(){
local args=""
for a in "${@}"; do
if [[ $a != \-* ]]; then
a="'$a'"
fi
args="$args $a"
done
echo "$args"
}
function get_command(){
echo "curl -s --insecure -w '$curl_format' -o /dev/null -m 1 ${@}"
}
function analyze(){
read -ra result_files <<< $(ls $tmp/r.$batch.*)
read -ra result_times <<< $(
for f in ${result_files[@]}; do
cat $f
done
)
average=$({
total=0
for t in ${result_times[@]}; do
total=$(bc <<< "$total+$t")
done
printf "%.3f" $(bc <<< "scale=$precision;$total/${#result_times[@]}")
})
echo "average=${average}s"
}
if [ $# -eq 0 ]; then
echo -e "\nusage: ./load-test.sh <args>\n"
echo -e "\t=> $(get_command "<args>")\n"
exit 1;
fi
trap "{ xargs kill -9 <<< \$(printf \"%s\n\" \"\${pids[@]}\"); rm -R $tmp; } &> /dev/null; exit" SIGINT SIGTERM
args=$(parse_args "${@}")
while [[ true ]]; do
pids=()
printf "Batch #$batch "
for i in $(seq 1 $concurrency); do
{
metrics=$(eval $(get_command $args))
echo "$metrics" > "$tmp/r.$batch.$i"
printf "."
} &
pids+=($!)
sleep $lag
done
wait
echo " $(analyze)"
((batch++))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment