-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·77 lines (63 loc) · 2.2 KB
/
test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
# Prompt user for load balancer IPv4 address
read -p "Enter the IPv4 address of the load balancer: " IP_ADDRESS
# Construct base URL
BASE_URL="http://${IP_ADDRESS}:2000"
NUM_REQUESTS=20
# Function to make a single request to /api/v1/hello
make_hello_request() {
start_time=$(date +%s.%N)
# Capture the response body in one file and the status code separately
response=$(curl -s -w "%{http_code}" -o temp_response.txt $BASE_URL/api/v1/hello)
end_time=$(date +%s.%N)
# Calculate duration in milliseconds
duration=$(echo "($end_time - $start_time) * 1000" | bc)
# Check if status code is an integer and handle errors
if ! [[ "$response" =~ ^[0-9]+$ ]]; then
response="000" # Use a default status code if response isn't numeric (like "Internal Error")
fi
echo "$response $duration"
}
# Make requests to /api/v1/hello
echo "Making $NUM_REQUESTS requests to /api/v1/hello at $BASE_URL..."
successful_requests=0
failed_requests=0
total_time=0
for i in $(seq 1 $NUM_REQUESTS); do
result=$(make_hello_request)
status_code=$(echo $result | cut -d' ' -f1)
response_time=$(echo $result | cut -d' ' -f2)
echo "Request $i:"
if [ "$status_code" -eq 200 ]; then
echo "Response body: $(cat temp_response.txt)"
successful_requests=$((successful_requests + 1))
else
# If the status code isn't 200, display the error message
echo "Failed with status code: $status_code"
error_response=$(cat temp_response.txt)
if [ -n "$error_response" ]; then
echo "Error response: $error_response"
else
echo "No response body (possible network error)"
fi
failed_requests=$((failed_requests + 1))
fi
# Accumulate total time for calculating the average
total_time=$(echo "$total_time + $response_time" | bc)
echo
done
total_requests=$NUM_REQUESTS
# Print hello endpoint stats
echo
echo "Hello Endpoint Stats:"
echo "{
\"successful_requests\": $successful_requests,
\"failed_requests\": $failed_requests,
\"total_requests\": $total_requests,
}"
# Get worker stats
echo
echo "Worker Stats:"
curl -s $BASE_URL/worker/stats | jq '.'
# Cleanup temp file
rm temp_response.txt