forked from devops329/jwt-pizza-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatePizzaTraffic.sh
65 lines (56 loc) · 2.04 KB
/
generatePizzaTraffic.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
#!/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
# Function to cleanly exit
cleanup() {
echo "Terminating background processes..."
kill $pid1 $pid2 $pid3 $pid4
exit 0
}
# Trap SIGINT (Ctrl+C) to execute the cleanup function
trap cleanup SIGINT
# Simulate a user requesting the menu every 3 seconds
while true; do
curl -s "$host/api/order/menu" > /dev/null
echo "Requesting menu..."
sleep 3
done &
pid1=$!
# Simulate a user with an invalid email and password every 25 seconds
while true; do
curl -s -X PUT "$host/api/auth" -d '{"email":"unknown@jwt.com", "password":"bad"}' -H 'Content-Type: application/json' > /dev/null
echo "Logging in with invalid credentials..."
sleep 25
done &
pid2=$!
# Simulate a franchisee logging in every two minutes
while true; do
response=$(curl -s -X PUT $host/api/auth -d '{"email":"f@jwt.com", "password":"franchisee"}' -H 'Content-Type: application/json')
token=$(echo $response | grep -o '"token":"[^"]*"' | sed 's/"token":"//;s/"//')
echo "Login franchisee..."
sleep 110
curl -s -X DELETE $host/api/auth -H "Authorization: Bearer $token" > /dev/null
echo "Logging out franchisee..."
sleep 10
done &
pid3=$!
# Simulate a diner ordering a pizza every 20 seconds
while true; do
response=$(curl -s -X PUT $host/api/auth -d '{"email":"d@jwt.com", "password":"diner"}' -H 'Content-Type: application/json')
token=$(echo $response | grep -o '"token":"[^"]*"' | sed 's/"token":"//;s/"//')
echo "Login diner..."
curl -s -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" > /dev/null
echo "Bought a pizza..."
sleep 20
curl -s -X DELETE $host/api/auth -H "Authorization: Bearer $token" > /dev/null
echo "Logging out diner..."
sleep 30
done &
pid4=$!
# Wait for the background processes to complete
wait $pid1 $pid2 $pid3 $pid4