Update docker/bench/run-ab.sh

This commit is contained in:
colin 2024-02-07 11:06:50 +00:00
parent 9734935762
commit 7ee72e7590
1 changed files with 29 additions and 12 deletions

View File

@ -30,8 +30,8 @@ upload_to_hastebin() {
} }
parse_to_csv() { parse_to_csv() {
local input=$log_file local input="$1"
local output=/log/scan.csv local output="$2"
local output_dir=$(dirname "$output") local output_dir=$(dirname "$output")
# Ensure the output directory exists # Ensure the output directory exists
@ -45,18 +45,35 @@ parse_to_csv() {
exit 1 exit 1
fi fi
: > "$output" # Clear output file before appending # Write CSV header
echo "URL,Time (ms),Status,Test Type" > "$output"
grep -Eo 'https?://[^ ]+' "$input" | sort -u | while read -r url; do grep -E 'TTFB test|ApacheBench test' "$input" | while IFS= read -r line; do
local ttfb_line=$(grep -m 1 "$url" "$input" | grep 'TTFB test passed' | awk -F'Mean TTFB: ' '{print $2}') local url=$(echo "$line" | grep -Eo 'https?://[^ ]+')
local ab_line=$(grep -m 1 "$url" "$input" | grep 'ApacheBench test passed' | awk -F'Average time: ' '{print $2}') local time
local ttfb_test=${ttfb_line%ms.*} local status
local ab_test=${ab_line% milliseconds.*} local test_type
if [[ -n "$ttfb_test" || -n "$ab_test" ]]; then
if [[ -z "$ttfb_test" ]]; then ttfb_test="N/A"; fi if [[ "$line" =~ "TTFB test passed" ]]; then
if [[ -z "$ab_test" ]]; then ab_test="N/A"; fi time=$(echo "$line" | awk -F'Mean TTFB: ' '{print $2}' | awk '{print $1}')
echo "$url,$ttfb_test,$ab_test" >> "$output" status="pass"
test_type="TTFB"
elif [[ "$line" =~ "ApacheBench test passed" ]]; then
time=$(echo "$line" | awk -F'Average time: ' '{print $2}' | awk '{print $1}')
status="pass"
test_type="AB"
elif [[ "$line" =~ "TTFB test failed" ]]; then
time="N/A" # Indicate not applicable or failed to obtain time
status="fail"
test_type="TTFB"
elif [[ "$line" =~ "ApacheBench test failed" ]]; then
time="N/A" # Indicate not applicable or failed to obtain time
status="fail"
test_type="AB"
fi fi
# Output to CSV
echo "$url,$time,$status,$test_type" >> "$output"
done done
} }