Skip to content

Instantly share code, notes, and snippets.

@Incanus3
Created October 16, 2019 21:18
Show Gist options
  • Save Incanus3/f20f9f057f0850d5750ad104e6ba4839 to your computer and use it in GitHub Desktop.
Save Incanus3/f20f9f057f0850d5750ad104e6ba4839 to your computer and use it in GitHub Desktop.
#!/bin/bash
dir="$(dirname "$0")"
bold="$(tput bold)"
normal="$(tput sgr0)"
function get_token {
(exec http --ignore-stdin --timeout 180 post $1/auth/get_token username=$2 password=$3 | jq -r .token)
}
function get_servers {
local servers_file="$1"
grep -Pv '^$|^\s*#' "$servers_file"
}
function get_server_name {
# for https://canteen-ct-backend.testing-tth.asrv.cz returns canteen-ct-backend
local host="$1"
echo "$host" | grep -Po "((?<=http://)|(?<=https://))[^.]+(?=[.:/])"
}
function do_load_test {
local host="$1"
local token="$2"
local concurrency="$3"
local page_size="$4"
local duration="$5"
local timeout="${6:-60}"
(exec hey -z "$duration" -c "$concurrency" -t "$timeout" -A "application/json" -T "application/json" \
-H "Authorization: Token $token" "$host/support/load_test?page_size=$page_size")
}
function test_one {
local host="$1"
local user="$2"
local pass="$3"
local concurrency="${4:-3,10,50}"
local page_size="${5:-100}"
local duration="${6:-1m}"
local reports_dir="$7"
local reports_prefix="$8"
for c in $(echo "$concurrency" | tr ',' '\n'); do
info="testing ${host} with concurrency=${c}, page_size=${page_size} and duration=${duration}"
echo "${bold}${info}${normal}" >&2
token="$(get_token "$host" "$user" "$pass")"
if [ -d "$reports_dir" ]; then
base_name="${reports_dir}/${reports_prefix}-$(get_server_name "$host")"
report_file="${base_name}-c${c}-ps${page_size}-${duration}.txt"
echo "reporting to file $report_file" >&2
echo "$info" > "$report_file"
do_load_test "$host" "$token" "$c" "$page_size" "$duration" >> "$report_file"
echo "reporting to $report_file finished"
else
do_load_test "$host" "$token" "$c" "$page_size" "$duration"
fi
done
}
if [ "$0" = "${BASH_SOURCE[0]}" ]; then
servers_file="${1:-servers.txt}"
concurrency="$2"
page_size="$3"
duration="$4"
run_in_parallel="$5"
reports_prefix="${6:-load-test-report}"
reports_dir="${7:-reports}"
if ! [ -s "$servers_file" ]; then
echo "servers file not found or empty" >&2
exit 1
fi
get_servers "$servers_file" | while read -r host user pass; do
test_one "$host" "$user" "$pass" "$concurrency" "$page_size" "$duration" \
"$reports_dir" "$reports_prefix" &
done
# echo "my pid: $BASHPID"
# pgrep -P "$BASHPID"
# ps --ppid "$BASHPID"
wait
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment