-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.sh
More file actions
executable file
·76 lines (66 loc) · 2.08 KB
/
load.sh
File metadata and controls
executable file
·76 lines (66 loc) · 2.08 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
#!/usr/bin/env bash
# Sends a continuous stream of requests to the backend to generate
# realistic traffic — useful for populating Grafana dashboards.
#
# Usage:
# ./load.sh # default: http://localhost:8000, 20 req/s
# ./load.sh 50 # 50 req/s
# ./load.sh 10 http://localhost:8001
set -euo pipefail
RPS="${1:-20}"
BASE_URL="${2:-http://localhost:8000}"
INTERVAL=$(python3 -c "print(1/$RPS)")
ENDPOINTS=(
"GET /"
"GET /api/items"
"GET /api/items/1"
"GET /api/items/5"
"GET /api/items/42"
"GET /api/items/101" # 404
"GET /api/items/-1" # 400
"POST /api/items"
"PUT /api/items/3"
"PATCH /api/items/7"
"DELETE /api/items/10"
"DELETE /api/items/200" # 404
"POST /api/orders"
"GET /api/orders/1234"
"GET /api/orders/99999" # 404
"DELETE /api/orders/5678"
"GET /api/random"
"GET /api/slow"
"GET /api/cpu"
"GET /api/bad-request" # 400
"GET /api/server-error" # 500
"GET /api/exception"
)
ITEM_BODY='{"name": "Test Item", "price": 9.99}'
ORDER_BODY='{"item_id": 1, "quantity": 2}'
send() {
local method="$1"
local path="$2"
local url="${BASE_URL}${path}"
case "$method $path" in
"POST /api/items")
curl -sf -X POST "$url" -H "Content-Type: application/json" -d "$ITEM_BODY" -o /dev/null ;;
"PUT /api/items/"*)
curl -sf -X PUT "$url" -H "Content-Type: application/json" -d "$ITEM_BODY" -o /dev/null ;;
"PATCH /api/items/"*)
curl -sf -X PATCH "$url" -H "Content-Type: application/json" -d "$ITEM_BODY" -o /dev/null ;;
"POST /api/orders")
curl -sf -X POST "$url" -H "Content-Type: application/json" -d "$ORDER_BODY" -o /dev/null ;;
*)
curl -sf -X "$method" "$url" -o /dev/null ;;
esac
return 0
}
echo "Sending requests to $BASE_URL at ~${RPS} req/s. Ctrl+C to stop."
i=0
while true; do
entry="${ENDPOINTS[$((i % ${#ENDPOINTS[@]}))]}"
method="${entry%% *}"
path="${entry#* }"
send "$method" "$path" &
i=$((i + 1))
sleep "$INTERVAL"
done