-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory-loop.sh
More file actions
executable file
·386 lines (328 loc) · 14 KB
/
memory-loop.sh
File metadata and controls
executable file
·386 lines (328 loc) · 14 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/bin/bash
# Memory Leak Test - Continuous API Calls Script
# This script provides various ways to loop curl requests to trigger memory growth
BASE_URL="http://localhost:8080/api"
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to check if application is running
check_app() {
if curl -s "$BASE_URL/health" > /dev/null 2>&1; then
echo -e "${GREEN}✅ Application is running${NC}"
return 0
else
echo -e "${RED}❌ Application is not running. Please start it first:${NC}"
echo " java -Xms256m -Xmx512m -XX:+UseG1GC -XX:+PrintGC -jar target/memory-leak-app-1.0.0.jar"
return 1
fi
}
# Function to display help
show_help() {
echo -e "${BLUE}Memory Leak Test - Continuous API Calls${NC}"
echo "Usage: $0 [OPTION]"
echo ""
echo "Options:"
echo " simple - Basic infinite loop (1 second delay)"
echo " slow - Slow loop for long-term testing (30 second delay)"
echo " auto-restart - Auto-restart mode with crash detection (60 second delay)"
echo " fast - Fast loop (0.1 second delay)"
echo " monitored - Loop with memory monitoring"
echo " counted [N] - Run N requests (default: 50)"
echo " parallel [N] - Run N parallel requests (default: 10)"
echo " stress - High-stress testing mode"
echo " watch - Real-time memory monitoring"
echo " clear - Clear all caches and reset memory"
echo " help - Show this help message"
echo ""
echo "Examples:"
echo " $0 simple # Basic infinite loop"
echo " $0 slow # Long-term gradual testing (3-4 hours)"
echo " $0 auto-restart # Auto-restart with 60s intervals"
echo " $0 auto-restart 120 # Auto-restart with 120s intervals"
echo " $0 counted 100 # Run 100 requests"
echo " $0 parallel 5 # 5 parallel requests"
echo " $0 monitored # Loop with monitoring"
echo " $0 clear # Clear all caches"
}
# Basic infinite loop
simple_loop() {
echo -e "${GREEN}Starting simple infinite loop (Ctrl+C to stop)${NC}"
counter=1
while true; do
echo -e "${BLUE}Request $counter${NC}"
curl -X POST "$BASE_URL/process" 2>/dev/null || echo -e "${RED}Request failed${NC}"
echo ""
counter=$((counter + 1))
sleep 1
done
}
# Slow loop for long-term testing (3-4 hours)
slow_loop() {
echo -e "${GREEN}Starting slow loop for long-term testing (30 second delays)${NC}"
echo -e "${YELLOW}This mode is designed for 3-4 hour gradual memory growth${NC}"
echo -e "${YELLOW}Ctrl+C to stop${NC}"
counter=1
start_time=$(date +%s)
while true; do
current_time=$(date +%s)
elapsed=$((current_time - start_time))
elapsed_hours=$((elapsed / 3600))
elapsed_mins=$(((elapsed % 3600) / 60))
echo -e "${BLUE}Request $counter (Running for ${elapsed_hours}h ${elapsed_mins}m)${NC}"
# Get memory stats before request
response=$(curl -s -X POST "$BASE_URL/process")
if [ $? -eq 0 ]; then
# Extract memory info
used_memory=$(echo "$response" | grep -o '"usedMemoryMB":[0-9]*' | cut -d':' -f2)
max_memory=$(echo "$response" | grep -o '"maxMemoryMB":[0-9]*' | cut -d':' -f2)
cache_size=$(echo "$response" | grep -o '"primaryCacheSize":[0-9]*' | cut -d':' -f2)
if [ ! -z "$used_memory" ] && [ ! -z "$max_memory" ]; then
memory_percent=$((used_memory * 100 / max_memory))
echo -e " Memory: ${YELLOW}${used_memory}MB/${max_memory}MB (${memory_percent}%)${NC} | Cache: ${YELLOW}${cache_size}${NC}"
fi
else
echo -e "${RED}Request failed${NC}"
fi
echo ""
counter=$((counter + 1))
sleep 30 # 30 second delay for gradual growth
done
}
# Fast loop
fast_loop() {
echo -e "${GREEN}Starting fast loop (Ctrl+C to stop)${NC}"
counter=1
while true; do
echo -e "${BLUE}Fast Request $counter${NC}"
curl -X POST "$BASE_URL/process" 2>/dev/null || echo -e "${RED}Request failed${NC}"
counter=$((counter + 1))
sleep 0.1
done
}
# Loop with monitoring
monitored_loop() {
echo -e "${GREEN}Starting monitored loop (Ctrl+C to stop)${NC}"
counter=1
while true; do
echo -e "${BLUE}=== Request $counter ===${NC}"
response=$(curl -s -X POST "$BASE_URL/process")
if [ $? -eq 0 ]; then
# Extract key metrics using grep and cut (works without jq)
used_memory=$(echo "$response" | grep -o '"usedMemoryMB":[0-9]*' | cut -d':' -f2)
cache_size=$(echo "$response" | grep -o '"primaryCacheSize":[0-9]*' | cut -d':' -f2)
total_requests=$(echo "$response" | grep -o '"totalRequests":[0-9]*' | cut -d':' -f2)
echo -e "Memory: ${YELLOW}${used_memory}MB${NC} | Cache: ${YELLOW}${cache_size}${NC} | Total: ${YELLOW}${total_requests}${NC}"
# Warning for high memory usage
if [ "$used_memory" -gt 400 ]; then
echo -e "${RED}⚠️ High memory usage detected: ${used_memory}MB${NC}"
fi
else
echo -e "${RED}Request failed${NC}"
fi
counter=$((counter + 1))
sleep 2
done
}
# Counted requests
counted_loop() {
local count=${1:-50}
echo -e "${GREEN}Running $count requests${NC}"
for i in $(seq 1 $count); do
echo -e "${BLUE}Request $i/$count${NC}"
response=$(curl -s -X POST "$BASE_URL/process")
if [ $? -eq 0 ]; then
# Extract metrics
used_memory=$(echo "$response" | grep -o '"usedMemoryMB":[0-9]*' | cut -d':' -f2)
cache_size=$(echo "$response" | grep -o '"primaryCacheSize":[0-9]*' | cut -d':' -f2)
echo -e "Memory: ${YELLOW}${used_memory}MB${NC} | Cache: ${YELLOW}${cache_size}${NC}"
else
echo -e "${RED}Request $i failed${NC}"
fi
sleep 1
done
echo -e "${GREEN}Completed $count requests${NC}"
}
# Parallel requests
parallel_requests() {
local count=${1:-10}
echo -e "${GREEN}Running $count parallel requests${NC}"
for i in $(seq 1 $count); do
curl -s -X POST "$BASE_URL/process" &
echo -e "${BLUE}Started parallel request $i${NC}"
done
echo -e "${YELLOW}Waiting for all requests to complete...${NC}"
wait
echo -e "${GREEN}All parallel requests completed${NC}"
}
# Stress testing
stress_test() {
echo -e "${RED}⚡ Starting stress test mode${NC}"
echo -e "${YELLOW}Phase 1: 20 fast requests${NC}"
for i in $(seq 1 20); do
echo -e "${BLUE}Stress request $i/20${NC}"
curl -X POST "$BASE_URL/process" 2>/dev/null &
sleep 0.05
done
wait
echo -e "${YELLOW}Phase 2: 10 parallel requests${NC}"
for i in $(seq 1 10); do
curl -s -X POST "$BASE_URL/process" &
done
wait
echo -e "${GREEN}Stress test completed${NC}"
# Show final stats
echo -e "${BLUE}Final memory stats:${NC}"
curl -s "$BASE_URL/stats" | grep -E '"usedMemoryMB"|"primaryCacheSize"|"totalRequests"'
}
# Real-time monitoring
watch_memory() {
echo -e "${GREEN}Real-time memory monitoring (Ctrl+C to stop)${NC}"
while true; do
clear
echo -e "${BLUE}=== Memory Leak Application Status ===${NC}"
echo -e "${YELLOW}$(date)${NC}"
echo ""
health_response=$(curl -s "$BASE_URL/health")
if [ $? -eq 0 ]; then
status=$(echo "$health_response" | grep -o '"status":"[^"]*"' | cut -d'"' -f4)
memory_percent=$(echo "$health_response" | grep -o '"memoryUsagePercent":"[^"]*"' | cut -d'"' -f4)
used_mb=$(echo "$health_response" | grep -o '"usedMemoryMB":[0-9]*' | cut -d':' -f2)
max_mb=$(echo "$health_response" | grep -o '"maxMemoryMB":[0-9]*' | cut -d':' -f2)
echo -e "Status: ${GREEN}$status${NC}"
echo -e "Memory Usage: ${YELLOW}$memory_percent%${NC} (${used_mb}MB / ${max_mb}MB)"
else
echo -e "${RED}Unable to connect to application${NC}"
fi
sleep 2
done
}
# Clear all caches
clear_caches() {
echo -e "${YELLOW}Clearing all caches and resetting memory...${NC}"
# Get stats before clearing
echo -e "${BLUE}Memory stats before clearing:${NC}"
before_response=$(curl -s "$BASE_URL/stats")
if [ $? -eq 0 ]; then
used_memory=$(echo "$before_response" | grep -o '"usedMemoryMB":[0-9]*' | cut -d':' -f2)
cache_size=$(echo "$before_response" | grep -o '"primaryCacheSize":[0-9]*' | cut -d':' -f2)
total_requests=$(echo "$before_response" | grep -o '"totalRequests":[0-9]*' | cut -d':' -f2)
echo -e "Memory: ${YELLOW}${used_memory}MB${NC} | Cache: ${YELLOW}${cache_size}${NC} | Requests: ${YELLOW}${total_requests}${NC}"
else
echo -e "${RED}Failed to get stats before clearing${NC}"
fi
# Clear caches
echo -e "${BLUE}Sending clear request...${NC}"
clear_response=$(curl -s -X POST "$BASE_URL/clear")
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Caches cleared successfully${NC}"
# Show stats after clearing
echo -e "${BLUE}Memory stats after clearing:${NC}"
used_memory=$(echo "$clear_response" | grep -o '"usedMemoryMB":[0-9]*' | cut -d':' -f2)
cache_size=$(echo "$clear_response" | grep -o '"primaryCacheSize":[0-9]*' | cut -d':' -f2)
total_requests=$(echo "$clear_response" | grep -o '"totalRequests":[0-9]*' | cut -d':' -f2)
echo -e "Memory: ${YELLOW}${used_memory}MB${NC} | Cache: ${YELLOW}${cache_size}${NC} | Requests: ${YELLOW}${total_requests}${NC}"
# Wait a moment and force GC
echo -e "${YELLOW}Waiting for garbage collection...${NC}"
sleep 3
# Final health check
final_health=$(curl -s "$BASE_URL/health")
if [ $? -eq 0 ]; then
final_memory=$(echo "$final_health" | grep -o '"usedMemoryMB":[0-9]*' | cut -d':' -f2)
memory_percent=$(echo "$final_health" | grep -o '"memoryUsagePercent":"[^"]*"' | cut -d'"' -f4)
echo -e "${GREEN}Final memory usage: ${final_memory}MB (${memory_percent}%)${NC}"
fi
else
echo -e "${RED}❌ Failed to clear caches${NC}"
fi
}
# Auto-restart loop with crash detection
auto_restart_loop() {
local delay=${1:-60} # Default 60 seconds, but allow override
echo -e "${GREEN}Starting auto-restart mode with ${delay}s intervals${NC}"
echo -e "${YELLOW}This mode will automatically restart the application if it crashes${NC}"
echo -e "${YELLOW}Monitoring application health every ${delay} seconds${NC}"
echo -e "${YELLOW}Ctrl+C to stop${NC}"
counter=1
start_time=$(date +%s)
restart_count=0
while true; do
current_time=$(date +%s)
elapsed=$((current_time - start_time))
hours=$((elapsed / 3600))
minutes=$(((elapsed % 3600) / 60))
echo -e "${BLUE}Auto-restart check $counter (${hours}h ${minutes}m elapsed, restarts: $restart_count)${NC}"
# Check if application is responding
if ! curl -s "$BASE_URL/health" > /dev/null 2>&1; then
echo -e "${RED}❌ Application not responding - attempting restart${NC}"
# Kill any existing Java processes for this app
echo -e "${YELLOW}Stopping existing application processes...${NC}"
pkill -f "memory-overloader" 2>/dev/null || true
sleep 5
# Start application with larger heap for longer runtime
echo -e "${YELLOW}Starting application with enhanced memory configuration...${NC}"
cd /Users/lbaya/Documents/rca-workspace/test_apps/memory_overloader
# Use larger heap and optimized GC for longer runtime
export JAVA_OPTS="-Xms1g -Xmx3g -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication"
nohup mvn spring-boot:run > app.log 2>&1 &
echo -e "${YELLOW}Waiting 30 seconds for application startup...${NC}"
sleep 30
# Verify restart was successful
if curl -s "$BASE_URL/health" > /dev/null 2>&1; then
restart_count=$((restart_count + 1))
echo -e "${GREEN}✅ Application restarted successfully (restart #$restart_count)${NC}"
else
echo -e "${RED}❌ Failed to restart application - retrying in ${delay} seconds${NC}"
fi
else
# Application is responding - make a test request
echo -e "${CYAN}Making memory leak request...${NC}"
response=$(curl -s -X POST "$BASE_URL/process" 2>/dev/null)
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Request successful${NC}"
else
echo -e "${YELLOW}⚠️ Request failed but app is still responding${NC}"
fi
fi
counter=$((counter + 1))
sleep "$delay"
done
}
# Main script logic
case "${1:-help}" in
"simple")
check_app && simple_loop
;;
"slow")
check_app && slow_loop
;;
"auto-restart")
auto_restart_loop "${2:-60}"
;;
"fast")
check_app && fast_loop
;;
"monitored")
check_app && monitored_loop
;;
"counted")
check_app && counted_loop "$2"
;;
"parallel")
check_app && parallel_requests "$2"
;;
"stress")
check_app && stress_test
;;
"watch")
check_app && watch_memory
;;
"clear")
check_app && clear_caches
;;
"help"|*)
show_help
;;
esac