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

Function TestMemoryLeaks_BinaryExpressionPool

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

Source from the content-addressed store, hash-verified

611}
612
613func TestMemoryLeaks_BinaryExpressionPool(t *testing.T) {
614 runtime.GC()
615 runtime.GC()
616 time.Sleep(10 * time.Millisecond)
617
618 var m1 runtime.MemStats
619 runtime.ReadMemStats(&m1)
620
621 const iterations = 10000
622
623 t.Logf("Running %d iterations of BinaryExpression pool operations...", iterations)
624
625 for i := 0; i < iterations; i++ {
626 expr := GetBinaryExpression()
627 expr.Left = &Identifier{Name: "col"}
628 expr.Operator = "="
629 expr.Right = &LiteralValue{Value: "val"}
630 PutBinaryExpression(expr)
631
632 if i%1000 == 0 && i > 0 {
633 runtime.GC()
634 }
635 }
636
637 runtime.GC()
638 runtime.GC()
639 time.Sleep(10 * time.Millisecond)
640
641 var m2 runtime.MemStats
642 runtime.ReadMemStats(&m2)
643
644 allocDiff := int64(m2.Alloc) - int64(m1.Alloc)
645 totalAllocDiff := int64(m2.TotalAlloc) - int64(m1.TotalAlloc)
646 bytesPerOp := float64(totalAllocDiff) / float64(iterations)
647
648 t.Logf("BinaryExpression pool memory stats:")
649 t.Logf(" Alloc diff: %d bytes", allocDiff)
650 t.Logf(" TotalAlloc diff: %d bytes", totalAllocDiff)
651 t.Logf(" Bytes per operation: %.2f", bytesPerOp)
652
653 const maxAllocIncrease = 1024 * 1024 // 1MB
654 const maxBytesPerOp = 5000 // 5KB
655
656 if allocDiff > maxAllocIncrease {
657 t.Errorf("Memory leak in BinaryExpression pool: %d bytes (threshold: %d)", allocDiff, maxAllocIncrease)
658 }
659
660 if bytesPerOp > maxBytesPerOp {
661 t.Errorf("High memory usage in BinaryExpression pool: %.2f bytes/op (threshold: %d)", bytesPerOp, maxBytesPerOp)
662 }
663
664 if allocDiff <= maxAllocIncrease && bytesPerOp <= maxBytesPerOp {
665 t.Logf("✅ BinaryExpression pool memory leak test PASSED")
666 }
667}
668
669func TestMemoryLeaks_LiteralValuePool(t *testing.T) {
670 runtime.GC()

Callers

nothing calls this directly

Calls 3

GetBinaryExpressionFunction · 0.85
PutBinaryExpressionFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected