trying to add more log info.

This commit is contained in:
colin 2024-02-22 12:52:02 +00:00
parent ae2a4e7ce8
commit 71be6834f9
1 changed files with 46 additions and 43 deletions

View File

@ -1,5 +1,47 @@
#!/bin/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,HTTP Code,Mean TTFB" > "$log_file"
for url in "${urls[@]}"; do
total_ttfb=0
for i in {1..5}; do
response=$(curl -o /dev/null -s -w "%{http_code},%{time_starttransfer}\n" -u "$username:$password" "$url")
http_code=$(echo "$response" | cut -d',' -f1)
ttfb=$(echo "$response" | cut -d',' -f2)
total_ttfb=$(echo "$total_ttfb + $ttfb" | bc)
done
sum_ttfb=$total_ttfb
mean_ttfb=$(echo "scale=3; $sum_ttfb / 5" | bc)
echo "$url,$username,$http_code,$mean_ttfb" | tee -a "$log_file"
done
}
function prepare_log_file { function prepare_log_file {
local log_file="/logs/authenticated.csv" local log_file="/logs/authenticated.csv"
> "$log_file" > "$log_file"
@ -7,47 +49,8 @@ function prepare_log_file {
touch "$log_file" touch "$log_file"
} }
function run_apache_bench_and_check_ttfb {
local urls=(${URLS//,/ })
local username=$USERNAME
local password=$PASSWORD
local requests=$REQUESTS
local concurrency=$CONCURRENCY
local log_file="/logs/authenticated.csv"
echo "URL,Average TTFB (ms),AB Mean Time per Request (ms)" | tee "$log_file"
for url in "${urls[@]}"; do
local total_ttfb=0
local ttfb=0
# Apache Bench
local ab_summary=$(ab -n "$requests" -c "$concurrency" -A "$username:$password" "$url" 2>&1 | grep "Time per request" | head -1)
local ab_mean_time_per_request=$(echo "$ab_summary" | awk '{print $4}' | awk '{printf "%.3f", $1}')
# TTFB
for i in {1..5}; do
ttfb=$(curl -o /dev/null -s -w "%{time_starttransfer}\n" -u "$username:$password" "$url")
total_ttfb=$(echo "$total_ttfb + $ttfb" | bc -l)
done
local mean_ttfb=$(echo "scale=3; ($total_ttfb / 5) * 1000" | bc)
echo "$url,${mean_ttfb},${ab_mean_time_per_request}" | tee -a "$log_file"
done
}
function upload_to_hastebin {
local log_file="/logs/authenticated.csv"
local response=$(curl -X POST -s --data-binary @"${log_file}" https://haste.nixc.us/documents)
local key=$(echo $response | awk -F '"' '{print $4}')
if [ ! -z "$key" ]; then
local haste_url="https://haste.nixc.us/$key"
echo "Logs uploaded to: $haste_url"
else
echo "Failed to upload logs to Hastebin."
fi
}
prepare_log_file prepare_log_file
run_apache_bench_and_check_ttfb
upload_to_hastebin test_urls
run_apache_bench
check_ttfb_and_log