Repository navigation
Expand file tree
/
Copy pathmemory_test.go
More file actions
445 lines (343 loc) · 11.6 KB
/
Copy pathmemory_test.go
File metadata and controls
445 lines (343 loc) · 11.6 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package zephyr_test
import (
"bytes"
"fmt"
"net/http/httptest"
"runtime"
"testing"
"time"
"github.com/RobertWHurst/zephyr/v2"
localtransport "github.com/RobertWHurst/zephyr/v2/local-transport"
natstransport "github.com/RobertWHurst/zephyr/v2/nats-transport"
)
// TestIterativeMemoryGrowth_Local runs multiple iterations and checks for memory growth using local transport
func TestIterativeMemoryGrowth_Local(t *testing.T) {
transport := localtransport.New()
gateway := zephyr.NewGateway("test-gateway", transport)
if err := startGateway(t, gateway); err != nil {
t.Fatalf("Failed to start gateway: %v", err)
}
defer gateway.Close()
service := setupTestService(t, transport, "growth-test-service")
defer service.Close()
const (
warmupIterations = 500
testIterations = 3000
sampleInterval = 500
)
// Warmup phase
for i := 0; i < warmupIterations; i++ {
req := httptest.NewRequest("GET", "/test", nil)
rec := httptest.NewRecorder()
transport.Dispatch("growth-test-service", rec, req)
}
// Force GC after warmup
runtime.GC()
runtime.GC()
profiler := NewMemoryProfiler(t, "iterative_growth_local")
profiler.Snapshot("baseline")
// Test phase
for i := 0; i < testIterations; i++ {
req := httptest.NewRequest("GET", "/test", nil)
rec := httptest.NewRecorder()
transport.Dispatch("growth-test-service", rec, req)
if i > 0 && i%sampleInterval == 0 {
profiler.Snapshot(fmt.Sprintf("iteration_%d", i))
}
}
profiler.Snapshot("final")
// Analyze growth trend
slope := profiler.GetMemoryGrowthSlope()
bytesPerIteration := slope / float64(sampleInterval)
t.Logf("Memory trend: %.2f bytes per iteration", bytesPerIteration)
// Flag if growing more than 100 bytes per request (allowing some tolerance)
if bytesPerIteration > 100 {
t.Errorf("Memory growing at %.2f bytes per iteration (threshold: 100)", bytesPerIteration)
profiler.WriteHeapProfile("growth_detected")
}
profiler.AssertNoGrowth(50.0) // Max 50% growth allowed
}
// TestIterativeMemoryGrowth_NATS runs multiple iterations and checks for memory growth using NATS transport
func TestIterativeMemoryGrowth_NATS(t *testing.T) {
nc := setupNATSConnection(t)
defer nc.Close()
transport := natstransport.New(nc)
gateway := zephyr.NewGateway("test-gateway", transport)
if err := startGateway(t, gateway); err != nil {
t.Fatalf("Failed to start gateway: %v", err)
}
defer gateway.Close()
service := setupTestService(t, transport, "growth-test-service-nats")
defer service.Close()
// Allow service discovery
time.Sleep(100 * time.Millisecond)
const (
warmupIterations = 200
testIterations = 1000
sampleInterval = 200
)
// Warmup phase
for i := 0; i < warmupIterations; i++ {
req := httptest.NewRequest("GET", "/test", nil)
rec := httptest.NewRecorder()
transport.Dispatch("growth-test-service-nats", rec, req)
}
// Force GC after warmup
runtime.GC()
runtime.GC()
profiler := NewMemoryProfiler(t, "iterative_growth_nats")
profiler.Snapshot("baseline")
// Test phase
for i := 0; i < testIterations; i++ {
req := httptest.NewRequest("GET", "/test", nil)
rec := httptest.NewRecorder()
transport.Dispatch("growth-test-service-nats", rec, req)
if i > 0 && i%sampleInterval == 0 {
profiler.Snapshot(fmt.Sprintf("iteration_%d", i))
}
}
profiler.Snapshot("final")
slope := profiler.GetMemoryGrowthSlope()
bytesPerIteration := slope / float64(sampleInterval)
t.Logf("Memory trend: %.2f bytes per iteration", bytesPerIteration)
if bytesPerIteration > 200 {
t.Errorf("Memory growing at %.2f bytes per iteration (threshold: 200)", bytesPerIteration)
profiler.WriteHeapProfile("growth_detected")
}
profiler.AssertNoGrowth(50.0)
}
// TestGoroutineLeak_Local tests for goroutine leaks with local transport
func TestGoroutineLeak_Local(t *testing.T) {
transport := localtransport.New()
initialGoroutines := runtime.NumGoroutine()
t.Logf("Initial goroutines: %d", initialGoroutines)
const cycles = 30
for cycle := 0; cycle < cycles; cycle++ {
service := zephyr.NewService(
fmt.Sprintf("goroutine-test-local-%d", cycle),
transport,
simpleHandler,
)
startService(t, service)
// Execute requests
for i := 0; i < 50; i++ {
req := httptest.NewRequest("GET", "/test", nil)
rec := httptest.NewRecorder()
transport.Dispatch(fmt.Sprintf("goroutine-test-local-%d", cycle), rec, req)
}
service.Close()
}
// Allow time for goroutines to exit
time.Sleep(200 * time.Millisecond)
runtime.GC()
finalGoroutines := runtime.NumGoroutine()
t.Logf("Final goroutines: %d", finalGoroutines)
maxGrowth := 10
if finalGoroutines-initialGoroutines > maxGrowth {
t.Errorf("Goroutine leak: started with %d, ended with %d (leaked %d)",
initialGoroutines, finalGoroutines, finalGoroutines-initialGoroutines)
// Dump goroutine stacks for debugging
buf := make([]byte, 64*1024)
n := runtime.Stack(buf, true)
t.Logf("Goroutine dump:\n%s", buf[:n])
}
}
// TestGoroutineLeak_NATS tests for goroutine leaks with NATS transport
func TestGoroutineLeak_NATS(t *testing.T) {
nc := setupNATSConnection(t)
defer nc.Close()
transport := natstransport.New(nc)
initialGoroutines := runtime.NumGoroutine()
t.Logf("Initial goroutines: %d", initialGoroutines)
const cycles = 20
for cycle := 0; cycle < cycles; cycle++ {
service := zephyr.NewService(
fmt.Sprintf("goroutine-test-nats-%d", cycle),
transport,
simpleHandler,
)
startService(t, service)
// Execute requests
for i := 0; i < 30; i++ {
req := httptest.NewRequest("GET", "/test", nil)
rec := httptest.NewRecorder()
transport.Dispatch(fmt.Sprintf("goroutine-test-nats-%d", cycle), rec, req)
}
service.Close()
}
// Allow time for goroutines to exit
time.Sleep(300 * time.Millisecond)
runtime.GC()
finalGoroutines := runtime.NumGoroutine()
t.Logf("Final goroutines: %d", finalGoroutines)
maxGrowth := 10
if finalGoroutines-initialGoroutines > maxGrowth {
t.Errorf("Goroutine leak: started with %d, ended with %d (leaked %d)",
initialGoroutines, finalGoroutines, finalGoroutines-initialGoroutines)
buf := make([]byte, 64*1024)
n := runtime.Stack(buf, true)
t.Logf("Goroutine dump:\n%s", buf[:n])
}
}
// TestServiceRegistrationCycle_Local tests service start/stop cycles for memory leaks
func TestServiceRegistrationCycle_Local(t *testing.T) {
transport := localtransport.New()
gateway := zephyr.NewGateway("test-gateway", transport)
startGateway(t, gateway)
defer gateway.Close()
profiler := NewMemoryProfiler(t, "service_registration_local")
const cycles = 50
// Warmup
for i := 0; i < 10; i++ {
service := zephyr.NewService(fmt.Sprintf("warmup-service-%d", i), transport, simpleHandler)
startService(t, service)
service.Close()
}
runtime.GC()
profiler.Snapshot("baseline")
for i := 0; i < cycles; i++ {
service := zephyr.NewService(fmt.Sprintf("cycle-service-%d", i), transport, simpleHandler)
startService(t, service)
// Do some work
for j := 0; j < 20; j++ {
req := httptest.NewRequest("GET", "/test", nil)
rec := httptest.NewRecorder()
transport.Dispatch(fmt.Sprintf("cycle-service-%d", i), rec, req)
}
service.Close()
if i > 0 && i%10 == 0 {
runtime.GC()
profiler.Snapshot(fmt.Sprintf("cycle_%d", i))
}
}
runtime.GC()
profiler.Snapshot("final")
profiler.AssertNoGrowth(30.0) // Max 30% growth
}
// TestServiceRegistrationCycle_NATS tests service start/stop cycles with NATS
func TestServiceRegistrationCycle_NATS(t *testing.T) {
nc := setupNATSConnection(t)
defer nc.Close()
transport := natstransport.New(nc)
gateway := zephyr.NewGateway("test-gateway", transport)
startGateway(t, gateway)
defer gateway.Close()
profiler := NewMemoryProfiler(t, "service_registration_nats")
const cycles = 30
// Warmup
for i := 0; i < 5; i++ {
service := zephyr.NewService(fmt.Sprintf("warmup-service-nats-%d", i), transport, simpleHandler)
startService(t, service)
time.Sleep(50 * time.Millisecond)
service.Close()
}
runtime.GC()
profiler.Snapshot("baseline")
for i := 0; i < cycles; i++ {
service := zephyr.NewService(fmt.Sprintf("cycle-service-nats-%d", i), transport, simpleHandler)
startService(t, service)
time.Sleep(20 * time.Millisecond)
// Do some work
for j := 0; j < 10; j++ {
req := httptest.NewRequest("GET", "/test", nil)
rec := httptest.NewRecorder()
transport.Dispatch(fmt.Sprintf("cycle-service-nats-%d", i), rec, req)
}
service.Close()
if i > 0 && i%10 == 0 {
runtime.GC()
profiler.Snapshot(fmt.Sprintf("cycle_%d", i))
}
}
runtime.GC()
profiler.Snapshot("final")
profiler.AssertNoGrowth(30.0)
}
// TestMemoryRelease_LargePayload_Local verifies buffers are released after large requests
func TestMemoryRelease_LargePayload_Local(t *testing.T) {
transport := localtransport.New()
gateway := zephyr.NewGateway("test-gateway", transport)
startGateway(t, gateway)
defer gateway.Close()
service := setupTestService(t, transport, "large-payload-service")
defer service.Close()
profiler := NewMemoryProfiler(t, "large_payload_local")
// Generate 1MB payload
payloadSize := 1024 * 1024
payload := bytes.Repeat([]byte("x"), payloadSize)
// Warmup
for i := 0; i < 5; i++ {
req := httptest.NewRequest("POST", "/test", bytes.NewReader(payload))
rec := httptest.NewRecorder()
transport.Dispatch("large-payload-service", rec, req)
}
runtime.GC()
runtime.GC()
profiler.Snapshot("after_warmup")
// Execute requests with large payloads
numRequests := 20
for i := 0; i < numRequests; i++ {
req := httptest.NewRequest("POST", "/test", bytes.NewReader(payload))
rec := httptest.NewRecorder()
transport.Dispatch("large-payload-service", rec, req)
}
// Force GC
runtime.GC()
runtime.GC()
profiler.Snapshot("after_requests")
var m runtime.MemStats
runtime.ReadMemStats(&m)
// Heap should not retain all the payload data
// Allow for ~5MB overhead after 20x1MB requests
maxExpectedHeap := uint64(20 * 1024 * 1024)
if m.HeapAlloc > maxExpectedHeap {
t.Errorf("Heap allocation too high after large payloads: %d bytes (expected < %d)",
m.HeapAlloc, maxExpectedHeap)
profiler.WriteHeapProfile("large_payload_retained")
}
}
// TestMemoryRelease_LargePayload_NATS verifies buffers are released with NATS transport
func TestMemoryRelease_LargePayload_NATS(t *testing.T) {
nc := setupNATSConnection(t)
defer nc.Close()
transport := natstransport.New(nc)
gateway := zephyr.NewGateway("test-gateway", transport)
startGateway(t, gateway)
defer gateway.Close()
service := setupTestService(t, transport, "large-payload-service-nats")
defer service.Close()
time.Sleep(100 * time.Millisecond)
profiler := NewMemoryProfiler(t, "large_payload_nats")
// Generate 100KB payload (smaller for NATS to avoid message size limits)
payloadSize := 100 * 1024
payload := bytes.Repeat([]byte("x"), payloadSize)
// Warmup
for i := 0; i < 5; i++ {
req := httptest.NewRequest("POST", "/test", bytes.NewReader(payload))
rec := httptest.NewRecorder()
transport.Dispatch("large-payload-service-nats", rec, req)
}
runtime.GC()
runtime.GC()
profiler.Snapshot("after_warmup")
// Execute requests with large payloads
numRequests := 20
for i := 0; i < numRequests; i++ {
req := httptest.NewRequest("POST", "/test", bytes.NewReader(payload))
rec := httptest.NewRecorder()
transport.Dispatch("large-payload-service-nats", rec, req)
}
// Force GC
runtime.GC()
runtime.GC()
profiler.Snapshot("after_requests")
var m runtime.MemStats
runtime.ReadMemStats(&m)
// Allow for ~10MB overhead after 20x100KB requests
maxExpectedHeap := uint64(15 * 1024 * 1024)
if m.HeapAlloc > maxExpectedHeap {
t.Errorf("Heap allocation too high after large payloads: %d bytes (expected < %d)",
m.HeapAlloc, maxExpectedHeap)
profiler.WriteHeapProfile("large_payload_retained")
}
}