Created
November 18, 2024 17:52
-
-
Save krcm0209/618dad74ffdac7bf82452fabe1a27377 to your computer and use it in GitHub Desktop.
Simple bash API perf test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Variables | |
URL="" | |
BEARER_TOKEN="" | |
API_KEY="" | |
NUM_REQUESTS=100 | |
# Array to store latencies | |
latencies=() | |
# Perform requests | |
echo "Starting performance testing..." | |
for ((i=1; i<=NUM_REQUESTS; i++)); do | |
start_time=$(date +%s%3N) | |
# Send request | |
response=$(curl -s -o /dev/null -w "%{http_code}" \ | |
-H "Authorization: Bearer $BEARER_TOKEN" \ | |
-H "x-api-key: $API_KEY" \ | |
"$URL") | |
end_time=$(date +%s%3N) | |
latency=$((end_time - start_time)) | |
# Store latency | |
latencies+=("$latency") | |
# Output progress | |
echo "Request $i: $latency ms (HTTP $response)" | |
done | |
# Calculate average latency | |
average=$(printf "%s\n" "${latencies[@]}" | awk '{sum += $1} END {print sum/NR}') | |
# Calculate median latency | |
sorted_latencies=($(printf "%s\n" "${latencies[@]}" | sort -n)) | |
count=${#sorted_latencies[@]} | |
if (( count % 2 == 0 )); then | |
# Even number of latencies | |
mid1=${sorted_latencies[$((count / 2 - 1))]} | |
mid2=${sorted_latencies[$((count / 2))]} | |
median=$(((mid1 + mid2) / 2)) | |
else | |
# Odd number of latencies | |
median=${sorted_latencies[$((count / 2))]} | |
fi | |
# Output results | |
echo "-----------------------------------" | |
echo "Performance Test Results:" | |
echo "Total Requests: $NUM_REQUESTS" | |
echo "Average Latency: $average ms" | |
echo "Median Latency: $median ms" | |
echo "-----------------------------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment