Skip to content

Commit 1c7fa7a

Browse files
committed
[cache] small improvements to tests
1 parent cb1b73f commit 1c7fa7a

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

pkg/cache/memory_profile_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestMemoryCache_MemoryAllocationPattern(t *testing.T) {
2222

2323
// Perform cache operations that trigger allocations
2424
const numItems = 1000
25-
for i := 0; i < numItems; i++ {
25+
for i := range numItems {
2626
key := "profile-key-" + strconv.Itoa(i)
2727
value := "profile-value-" + strconv.Itoa(i)
2828

@@ -225,12 +225,12 @@ func TestMemoryCache_MemoryLeakDetection(t *testing.T) {
225225

226226
// Create and destroy many cache instances
227227
const numCaches = 1000
228-
for i := 0; i < numCaches; i++ {
228+
for i := range numCaches {
229229
// Create a new cache
230230
tempCache := cache.NewMemory(0)
231231

232232
// Add some items
233-
for j := 0; j < 10; j++ {
233+
for j := range 10 {
234234
key := "leak-key-" + strconv.Itoa(i) + "-" + strconv.Itoa(j)
235235
value := "leak-value-" + strconv.Itoa(i) + "-" + strconv.Itoa(j)
236236

@@ -252,18 +252,18 @@ func TestMemoryCache_MemoryLeakDetection(t *testing.T) {
252252
runtime.ReadMemStats(&m2)
253253

254254
// Calculate memory growth
255-
allocDiff := m2.TotalAlloc - m1.TotalAlloc
255+
heapDiff := m2.HeapAlloc - m1.HeapAlloc
256256

257257
t.Logf("Memory leak detection stats:")
258-
t.Logf(" Total allocated: %d bytes", m2.TotalAlloc)
259-
t.Logf(" Allocation difference: %d bytes", allocDiff)
258+
t.Logf(" Heap alloc: %d bytes", m2.HeapAlloc)
259+
t.Logf(" Heap delta: %d bytes", heapDiff)
260260
t.Logf(" Heap objects: %d", m2.HeapObjects)
261261
t.Logf(" GC cycles: %d", m2.NumGC)
262262

263263
// Memory should not grow significantly after cleanup
264264
// This helps detect potential memory leaks
265-
if allocDiff > 10*1024*1024 { // 10MB threshold for leak detection
266-
t.Errorf("Potential memory leak detected: %d bytes unallocated after cleanup", allocDiff)
265+
if heapDiff > 1*1024*1024 { // 1MB threshold for leak detection
266+
t.Errorf("Potential memory leak detected: %d bytes retained after cleanup", heapDiff)
267267
}
268268
}
269269

@@ -272,11 +272,12 @@ func BenchmarkMemoryCache_MemoryUsage(b *testing.B) {
272272
cache := cache.NewMemory(0)
273273
ctx := context.Background()
274274

275+
b.ReportAllocs()
276+
runtime.GC()
277+
var m1, m2 runtime.MemStats
278+
runtime.ReadMemStats(&m1)
275279
b.ResetTimer()
276280
b.RunParallel(func(pb *testing.PB) {
277-
var m1, m2 runtime.MemStats
278-
runtime.ReadMemStats(&m1)
279-
280281
i := 0
281282
for pb.Next() {
282283
key := "bench-key-" + strconv.Itoa(i)
@@ -291,8 +292,7 @@ func BenchmarkMemoryCache_MemoryUsage(b *testing.B) {
291292

292293
i++
293294
}
294-
295-
runtime.ReadMemStats(&m2)
296-
b.Logf("Memory usage per iteration: %.2f bytes", float64(m2.TotalAlloc-m1.TotalAlloc)/float64(b.N))
297295
})
296+
runtime.ReadMemStats(&m2)
297+
b.Logf("TotalAlloc per op: %.2f bytes/op", float64(m2.TotalAlloc-m1.TotalAlloc)/float64(b.N))
298298
}

0 commit comments

Comments
 (0)