qmk/docker/auth-bench/run-ab.sh

60 lines
1.6 KiB
Bash

#!/bin/bash
function test_urls {
local urls=(${URLS//,/ })
local username=$USERNAME
local password=$PASSWORD
for url in "${urls[@]}"; do
curl -u "$username:$password" "$url"
done
}
function run_apache_bench {
local urls=(${URLS//,/ })
local username=$USERNAME
local password=$PASSWORD
local requests=$REQUESTS
local concurrency=$CONCURRENCY
local log_file="/logs/authenticated.log"
for url in "${urls[@]}"; do
ab -n "$requests" -c "$concurrency" -A "$username:$password" "$url" | tee -a "$log_file"
done
}
function check_ttfb_and_log {
local urls=(${URLS//,/ })
local username=$USERNAME
local password=$PASSWORD
local log_file="/logs/authenticated.csv"
local total_ttfb sum_ttfb mean_ttfb http_code
echo "URL,Username,Mean TTFB,HTTP Code" > "$log_file"
for url in "${urls[@]}"; do
total_ttfb=0
for i in {1..5}; do
response=$(curl -o /dev/null -s -w "%{time_starttransfer},%{http_code}\n" -u "$username:$password" "$url")
IFS=',' read -r ttfb http_code <<< "$response"
total_ttfb=$(echo "$total_ttfb + $ttfb" | bc)
done
sum_ttfb=$total_ttfb
mean_ttfb=$(echo "scale=3; $sum_ttfb / 5" | bc)
echo "$url,$username,$mean_ttfb,$http_code" >> "$log_file"
done
local haste_url=$(curl -X POST -s -F "file=@${log_file}" https://haste.nixc.us/documents | awk -F '"' '{print "https://haste.nixc.us/"$4}')
echo "Logs uploaded to: $haste_url"
}
function prepare_log_file {
local log_file="/logs/authenticated.csv"
> "$log_file"
mkdir -p $(dirname "$log_file")
touch "$log_file"
}
prepare_log_file
test_urls
run_apache_bench
check_ttfb_and_log