-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathgeneratePizzaTraffic.sh
More file actions
executable file
·92 lines (79 loc) · 2.97 KB
/
Copy pathgeneratePizzaTraffic.sh
File metadata and controls
executable file
·92 lines (79 loc) · 2.97 KB
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
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# Check if host is provided as a command line argument
if [ -z "$1" ]; then
echo "Usage: $0 <host>"
echo "Example: $0 http://localhost:3000"
exit 1
fi
host=$1
# Trap SIGINT (Ctrl+C) to execute the cleanup function
cleanup() {
echo "Terminating background processes..."
kill $pid1 $pid2 $pid3 $pid4 $pid5
exit 0
}
trap cleanup SIGINT
# Wrap curl command to return HTTP response codes
execute_curl() {
echo $(eval "curl -s -o /dev/null -w \"%{http_code}\" $1")
}
# Function to login and get a token
login() {
response=$(curl -s -X PUT $host/api/auth -d "{\"email\":\"$1\", \"password\":\"$2\"}" -H 'Content-Type: application/json')
token=$(echo $response | jq -r '.token')
echo $token
}
# Simulate a user requesting the menu every 3 seconds
while true; do
result=$(execute_curl $host/api/order/menu)
echo "Requesting menu..." $result
sleep 3
done &
pid1=$!
# Simulate a user with an invalid email and password every 25 seconds
while true; do
result=$(execute_curl "-X PUT \"$host/api/auth\" -d '{\"email\":\"unknown@jwt.com\", \"password\":\"bad\"}' -H 'Content-Type: application/json'")
echo "Logging in with invalid credentials..." $result
sleep 25
done &
pid2=$!
# Simulate a franchisee logging in every two minutes
while true; do
token=$(login "f@jwt.com" "franchisee")
echo "Login franchisee..." $( [ -z "$token" ] && echo "false" || echo "true" )
sleep 110
result=$(execute_curl "-X DELETE $host/api/auth -H \"Authorization: Bearer $token\"")
echo "Logging out franchisee..." $result
sleep 10
done &
pid3=$!
# Simulate a diner ordering a pizza every 50 seconds
while true; do
token=$(login "d@jwt.com" "diner")
echo "Login diner..." $( [ -z "$token" ] && echo "false" || echo "true" )
result=$(execute_curl "-X POST $host/api/order -H 'Content-Type: application/json' -d '{\"franchiseId\": 1, \"storeId\":1, \"items\":[{ \"menuId\": 1, \"description\": \"Veggie\", \"price\": 0.05 }]}' -H \"Authorization: Bearer $token\"")
echo "Bought a pizza..." $result
sleep 20
result=$(execute_curl "-X DELETE $host/api/auth -H \"Authorization: Bearer $token\"")
echo "Logging out diner..." $result
sleep 30
done &
pid4=$!
# Simulate a failed pizza order every 5 minutes
while true; do
token=$(login "d@jwt.com" "diner")
echo "Login hungry diner..." $( [ -z "$token" ] && echo "false" || echo "true" )
items='{ "menuId": 1, "description": "Veggie", "price": 0.05 }'
for (( i=0; i < 21; i++ ))
do items+=', { "menuId": 1, "description": "Veggie", "price": 0.05 }'
done
result=$(execute_curl "-X POST $host/api/order -H 'Content-Type: application/json' -d '{\"franchiseId\": 1, \"storeId\":1, \"items\":[$items]}' -H \"Authorization: Bearer $token\"")
echo "Bought too many pizzas..." $result
sleep 5
result=$(execute_curl "-X DELETE $host/api/auth -H \"Authorization: Bearer $token\"")
echo "Logging out hungry diner..." $result
sleep 295
done &
pid5=$!
# Wait for the background processes to complete
wait $pid1 $pid2 $pid3 $pid4 $pid5