Skip to content

Instantly share code, notes, and snippets.

@paulwaldmann
Last active November 3, 2023 10:49
Show Gist options
  • Save paulwaldmann/745882a5cce98e8298d7385a234fb2ee to your computer and use it in GitHub Desktop.
Save paulwaldmann/745882a5cce98e8298d7385a234fb2ee to your computer and use it in GitHub Desktop.
Simple website benchmark testing script
#!/bin/bash
url="https://example.com"
total_requests=100
output_file="results.txt"
perform_request() {
local status_code
status_code=$(curl -o /dev/null -s -w "%{http_code}\n" -I "$url")
echo "$status_code"
}
write_to_file() {
echo "$1" >>"$output_file"
}
echo "Starting benchmark test..."
total_time=0
success_requests=0
fail_requests=0
for ((i = 1; i <= total_requests; i++)); do
echo "Request $i/$total_requests"
start_time=$(date +%s%N)
status_code=$(perform_request)
end_time=$(date +%s%N)
elapsed_time=$((end_time - start_time))
total_time=$((total_time + elapsed_time))
if [[ "$status_code" -ge 200 && "$status_code" -lt 300 ]]; then
((success_requests++))
else
((fail_requests++))
fi
done
average_time=$(echo "scale=3; $total_time / $total_requests / 1000000" | bc)
{
echo "Benchmark results:"
echo "Total Requests: $total_requests"
echo "Successful Requests: $success_requests"
echo "Failed Requests: $fail_requests"
echo "Average Response Time: ${average_time}ms"
} >"$output_file"
echo "Benchmark test complete. Results saved to $output_file."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment