Last active
August 29, 2025 12:20
-
-
Save denisolvr/b370d7191a63842667a011af9490d2c2 to your computer and use it in GitHub Desktop.
Return an AVG of response time based on X number of requests
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 | |
| # Color codes for output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' # No Color | |
| # Function to display usage | |
| usage() { | |
| echo "Usage: $0 -u <URL> -n <number_of_requests> [-t <timeout>] [-h]" | |
| echo "" | |
| echo "Options:" | |
| echo " -u URL URL to test (required)" | |
| echo " -n NUMBER Number of requests to make (required)" | |
| echo " -t TIMEOUT Timeout in seconds (default: 10)" | |
| echo " -h Show this help message" | |
| echo "" | |
| echo "Example:" | |
| echo " $0 -u https://example.com -n 10" | |
| echo " $0 -u https://example.com -n 50 -t 30" | |
| exit 1 | |
| } | |
| # Default values | |
| TIMEOUT=10 | |
| URL="" | |
| NUM_REQUESTS=0 | |
| # Parse command line arguments | |
| while getopts "u:n:t:h" opt; do | |
| case ${opt} in | |
| u ) | |
| URL="$OPTARG" | |
| ;; | |
| n ) | |
| NUM_REQUESTS="$OPTARG" | |
| ;; | |
| t ) | |
| TIMEOUT="$OPTARG" | |
| ;; | |
| h ) | |
| usage | |
| ;; | |
| \? ) | |
| echo "Invalid option: -$OPTARG" >&2 | |
| usage | |
| ;; | |
| esac | |
| done | |
| # Validate required parameters | |
| if [ -z "$URL" ] || [ "$NUM_REQUESTS" -eq 0 ]; then | |
| echo -e "${RED}Error: URL and number of requests are required${NC}" | |
| usage | |
| fi | |
| # Validate number of requests is positive | |
| if ! [[ "$NUM_REQUESTS" =~ ^[0-9]+$ ]] || [ "$NUM_REQUESTS" -le 0 ]; then | |
| echo -e "${RED}Error: Number of requests must be a positive integer${NC}" | |
| exit 1 | |
| fi | |
| # Arrays to store results | |
| declare -a response_times | |
| declare -a http_codes | |
| successful_requests=0 | |
| failed_requests=0 | |
| echo -e "${GREEN}========================================${NC}" | |
| echo -e "${GREEN}Load Time Testing Script${NC}" | |
| echo -e "${GREEN}========================================${NC}" | |
| echo -e "${YELLOW}URL:${NC} $URL" | |
| echo -e "${YELLOW}Number of requests:${NC} $NUM_REQUESTS" | |
| echo -e "${YELLOW}Timeout:${NC} ${TIMEOUT}s" | |
| echo -e "${GREEN}========================================${NC}" | |
| echo "" | |
| # Progress bar function | |
| show_progress() { | |
| local current=$1 | |
| local total=$2 | |
| local width=50 | |
| local percentage=$((current * 100 / total)) | |
| local completed=$((width * current / total)) | |
| printf "\rProgress: [" | |
| printf "%${completed}s" | tr ' ' '=' | |
| printf "%$((width - completed))s" | tr ' ' '>' | |
| printf "] %3d%% (%d/%d)" $percentage $current $total | |
| } | |
| echo "Starting requests..." | |
| # Perform requests | |
| for i in $(seq 1 $NUM_REQUESTS); do | |
| # Show progress | |
| show_progress $i $NUM_REQUESTS | |
| # Make the request and capture metrics | |
| response=$(curl -o /dev/null -s -w "%{time_total},%{http_code}" \ | |
| --connect-timeout $TIMEOUT \ | |
| --max-time $TIMEOUT \ | |
| "$URL" 2>/dev/null) | |
| if [ $? -eq 0 ]; then | |
| # Parse response | |
| response_time=$(echo $response | cut -d',' -f1) | |
| http_code=$(echo $response | cut -d',' -f2) | |
| # Store results | |
| response_times+=($response_time) | |
| http_codes+=($http_code) | |
| # Count successful requests (2xx and 3xx status codes) | |
| if [[ $http_code =~ ^[23][0-9]{2}$ ]]; then | |
| ((successful_requests++)) | |
| else | |
| ((failed_requests++)) | |
| fi | |
| else | |
| ((failed_requests++)) | |
| response_times+=(0) | |
| http_codes+=(0) | |
| fi | |
| # Small delay to avoid overwhelming the server | |
| sleep 0.1 | |
| done | |
| echo -e "\n\n${GREEN}========================================${NC}" | |
| echo -e "${GREEN}Results${NC}" | |
| echo -e "${GREEN}========================================${NC}" | |
| # Calculate statistics if we have successful requests | |
| if [ ${#response_times[@]} -gt 0 ] && [ $successful_requests -gt 0 ]; then | |
| # Calculate average (excluding failed requests) | |
| total=0 | |
| min_time=999999 | |
| max_time=0 | |
| count=0 | |
| for i in "${!response_times[@]}"; do | |
| time=${response_times[$i]} | |
| code=${http_codes[$i]} | |
| # Only include successful requests in calculations | |
| if [[ $code =~ ^[23][0-9]{2}$ ]] && [ $(echo "$time > 0" | bc) -eq 1 ]; then | |
| total=$(echo "$total + $time" | bc) | |
| ((count++)) | |
| # Find min | |
| if [ $(echo "$time < $min_time" | bc) -eq 1 ]; then | |
| min_time=$time | |
| fi | |
| # Find max | |
| if [ $(echo "$time > $max_time" | bc) -eq 1 ]; then | |
| max_time=$time | |
| fi | |
| fi | |
| done | |
| # Calculate average | |
| if [ $count -gt 0 ]; then | |
| average=$(echo "scale=3; $total / $count" | bc) | |
| # Convert to milliseconds for display | |
| avg_ms=$(echo "scale=0; $average * 1000" | bc) | |
| min_ms=$(echo "scale=0; $min_time * 1000" | bc) | |
| max_ms=$(echo "scale=0; $max_time * 1000" | bc) | |
| echo -e "${YELLOW}Successful requests:${NC} $successful_requests / $NUM_REQUESTS" | |
| echo -e "${YELLOW}Failed requests:${NC} $failed_requests / $NUM_REQUESTS" | |
| echo -e "${YELLOW}Success rate:${NC} $(echo "scale=1; $successful_requests * 100 / $NUM_REQUESTS" | bc)%" | |
| echo "" | |
| echo -e "${GREEN}Response Time Statistics (successful requests only):${NC}" | |
| echo -e "${YELLOW}Average:${NC} ${avg_ms}ms (${average}s)" | |
| echo -e "${YELLOW}Minimum:${NC} ${min_ms}ms (${min_time}s)" | |
| echo -e "${YELLOW}Maximum:${NC} ${max_ms}ms (${max_time}s)" | |
| # Show HTTP status code distribution | |
| echo "" | |
| echo -e "${GREEN}HTTP Status Code Distribution:${NC}" | |
| printf "%s\n" "${http_codes[@]}" | sort | uniq -c | while read count code; do | |
| if [ "$code" != "0" ]; then | |
| echo -e " ${YELLOW}$code:${NC} $count requests" | |
| fi | |
| done | |
| # Performance summary | |
| echo "" | |
| echo -e "${GREEN}Performance Summary:${NC}" | |
| if [ $(echo "$average < 0.5" | bc) -eq 1 ]; then | |
| echo -e " ${GREEN}Excellent${NC} - Average response time under 500ms" | |
| elif [ $(echo "$average < 1" | bc) -eq 1 ]; then | |
| echo -e " ${GREEN}Good${NC} - Average response time under 1 second" | |
| elif [ $(echo "$average < 3" | bc) -eq 1 ]; then | |
| echo -e " ${YELLOW}Fair${NC} - Average response time under 3 seconds" | |
| else | |
| echo -e " ${RED}Poor${NC} - Average response time over 3 seconds" | |
| fi | |
| fi | |
| else | |
| echo -e "${RED}Error: No successful requests completed${NC}" | |
| exit 1 | |
| fi | |
| echo -e "${GREEN}========================================${NC}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment