-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_requests.sh
executable file
·81 lines (65 loc) · 1.87 KB
/
send_requests.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
78
79
80
81
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 <num_requests> <kafka-port> <other-clients>"
exit 1
fi
num_requests=$1
# Kafka broker random choices
ports=(9090 9091 9092)
selected_port=${ports[$RANDOM % ${#ports[@]}]}
kafka_port=${2:-$selected_port}
clients=${3:-"9090,9091,9092"}
base_url="http://localhost:$kafka_port/addScore"
names=("Ronaldo" "Messi" "Pele" "Maradona")
clubs=("FCB" "RMA" "MU" "FCB")
# Generate a random score
generate_score() {
local team1=$((RANDOM % 5))
local team2=$((RANDOM % 5))
echo "${team1}-${team2}"
}
# Record the start time with nanoseconds before the loop
start_time=$(date +%s.%N)
# Loop for sending requests
minute=1
for i in $(seq 1 "$num_requests"); do
# ZooWeeper Server random choices
receiver_ip=$((RANDOM % 3 + 8080))
name=${names[$RANDOM % ${#names[@]}]}
club=${clubs[$RANDOM % ${#clubs[@]}]}
score=$(generate_score)
# Use curl to send the request and capture the response
response=$(curl -s -X POST "$base_url" \
-H "Content-Type: application/json" \
-d "$(cat <<EOF
{
"metadata": {
"ReceiverIp": "$receiver_ip",
"Clients": "$clients"
},
"gameResults": {
"Minute": $minute,
"Player": "$name",
"Club": "$club",
"Score": "$score"
}
}
EOF
)")
# Check if the response contains "OK"
if echo "$response" | grep -q "OK"; then
echo "Received OK response for request $i."
else
echo "Received unexpected response for request $i: $response"
fi
minute=$((minute + 1))
if [ $minute -gt 90 ]; then
minute=1
fi
done
# Record the end time with nanoseconds after the last request
end_time=$(date +%s.%N)
# Calculate the total time taken with nanoseconds precision using awk
total_time_seconds=$(awk "BEGIN {print $end_time - $start_time}")
echo "Total time taken for $num_requests requests: $total_time_seconds seconds"
echo "Requests sent."