MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / TestMemoryLeaks_SelectStatementPool

Function TestMemoryLeaks_SelectStatementPool

pkg/sql/ast/pool_test.go:312–372  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

310}
311
312func TestMemoryLeaks_SelectStatementPool(t *testing.T) {
313 runtime.GC()
314 runtime.GC()
315 time.Sleep(10 * time.Millisecond)
316
317 var m1 runtime.MemStats
318 runtime.ReadMemStats(&m1)
319
320 const iterations = 10000
321
322 t.Logf("Running %d iterations of SelectStatement pool operations...", iterations)
323
324 for i := 0; i < iterations; i++ {
325 stmt := GetSelectStatement()
326
327 stmt.TableName = "users"
328 stmt.Columns = append(stmt.Columns, &Identifier{Name: "id"}, &Identifier{Name: "name"})
329 stmt.Where = &BinaryExpression{
330 Left: &Identifier{Name: "active"},
331 Operator: "=",
332 Right: &LiteralValue{Value: "true"},
333 }
334
335 PutSelectStatement(stmt)
336
337 if i%1000 == 0 && i > 0 {
338 runtime.GC()
339 }
340 }
341
342 runtime.GC()
343 runtime.GC()
344 time.Sleep(10 * time.Millisecond)
345
346 var m2 runtime.MemStats
347 runtime.ReadMemStats(&m2)
348
349 allocDiff := int64(m2.Alloc) - int64(m1.Alloc)
350 totalAllocDiff := int64(m2.TotalAlloc) - int64(m1.TotalAlloc)
351 bytesPerOp := float64(totalAllocDiff) / float64(iterations)
352
353 t.Logf("SelectStatement pool memory stats:")
354 t.Logf(" Alloc diff: %d bytes", allocDiff)
355 t.Logf(" TotalAlloc diff: %d bytes", totalAllocDiff)
356 t.Logf(" Bytes per operation: %.2f", bytesPerOp)
357
358 const maxAllocIncrease = 1024 * 1024 // 1MB
359 const maxBytesPerOp = 6000 // 6KB
360
361 if allocDiff > maxAllocIncrease {
362 t.Errorf("Memory leak in SelectStatement pool: %d bytes (threshold: %d)", allocDiff, maxAllocIncrease)
363 }
364
365 if bytesPerOp > maxBytesPerOp {
366 t.Errorf("High memory usage in SelectStatement pool: %.2f bytes/op (threshold: %d)", bytesPerOp, maxBytesPerOp)
367 }
368
369 if allocDiff <= maxAllocIncrease && bytesPerOp <= maxBytesPerOp {

Callers

nothing calls this directly

Calls 3

GetSelectStatementFunction · 0.85
PutSelectStatementFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected