-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadtest.sh
More file actions
executable file
·90 lines (75 loc) · 2.71 KB
/
loadtest.sh
File metadata and controls
executable file
·90 lines (75 loc) · 2.71 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
#!/usr/bin/env bash
#
# Load test for the singleflight service.
# Sends concurrent requests in waves to demonstrate:
# - Singleflight deduplication (many concurrent requests for the same ID)
# - Cache hits vs misses (first wave = miss, subsequent = hit until TTL expires)
# - HTTP latency under load
#
# Usage: ./loadtest.sh
# Requirements: curl, bash
BASE_URL="http://localhost:8080/template-details"
TEMPLATE_IDS=($(seq 1 100))
CONCURRENCY=200 # requests per burst
WAVES=25 # number of burst waves
PAUSE_BETWEEN=1 # seconds between waves
total_requests=0
total_errors=0
echo "============================================"
echo " Singleflight Load Test"
echo "============================================"
echo " Concurrency per wave : $CONCURRENCY"
echo " Waves : $WAVES"
echo " Pause between waves : ${PAUSE_BETWEEN}s"
echo " Template IDs : ${TEMPLATE_IDS[*]}"
echo "============================================"
echo ""
for wave in $(seq 1 $WAVES); do
# Pick a random template ID -- all concurrent requests hit the SAME ID
# to maximize singleflight deduplication
id=${TEMPLATE_IDS[$((RANDOM % ${#TEMPLATE_IDS[@]}))]}
echo "--- Wave $wave/$WAVES: $CONCURRENCY concurrent requests for id=$id ---"
pids=()
errors=0
start=$(date +%s%N)
for i in $(seq 1 $CONCURRENCY); do
(
status=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL?id=$id")
if [ "$status" != "200" ]; then
exit 1
fi
) &
pids+=($!)
done
# Wait and count errors
for pid in "${pids[@]}"; do
if ! wait "$pid"; then
((errors++))
fi
done
end=$(date +%s%N)
elapsed_ms=$(( (end - start) / 1000000 ))
total_requests=$((total_requests + CONCURRENCY))
total_errors=$((total_errors + errors))
echo " Completed in ${elapsed_ms}ms | Errors: $errors/$CONCURRENCY"
# Also sprinkle in some requests for random IDs (spread across cache)
for spread_id in "${TEMPLATE_IDS[@]}"; do
curl -s -o /dev/null "$BASE_URL?id=$spread_id" &
done
wait
total_requests=$((total_requests + ${#TEMPLATE_IDS[@]}))
if [ "$wave" -lt "$WAVES" ]; then
sleep $PAUSE_BETWEEN
fi
done
echo ""
echo "============================================"
echo " Load Test Complete"
echo "============================================"
echo " Total requests : $total_requests"
echo " Total errors : $total_errors"
echo " Success rate : $(( (total_requests - total_errors) * 100 / total_requests ))%"
echo "============================================"
echo ""
echo "Check Grafana at http://localhost:3000 (admin/admin)"
echo "Set time range to 'Last 5 minutes' and refresh."