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

Function TestMemoryLeaks_InsertStatementPool

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

Source from the content-addressed store, hash-verified

372}
373
374func TestMemoryLeaks_InsertStatementPool(t *testing.T) {
375 runtime.GC()
376 runtime.GC()
377 time.Sleep(10 * time.Millisecond)
378
379 var m1 runtime.MemStats
380 runtime.ReadMemStats(&m1)
381
382 const iterations = 10000
383
384 t.Logf("Running %d iterations of InsertStatement pool operations...", iterations)
385
386 for i := 0; i < iterations; i++ {
387 stmt := GetInsertStatement()
388
389 stmt.TableName = "users"
390 stmt.Columns = append(stmt.Columns, &Identifier{Name: "name"}, &Identifier{Name: "email"})
391 // Values is now [][]Expression for multi-row support
392 stmt.Values = append(stmt.Values, []Expression{&LiteralValue{Value: "John"}, &LiteralValue{Value: "john@test.com"}})
393
394 PutInsertStatement(stmt)
395
396 if i%1000 == 0 && i > 0 {
397 runtime.GC()
398 }
399 }
400
401 runtime.GC()
402 runtime.GC()
403 time.Sleep(10 * time.Millisecond)
404
405 var m2 runtime.MemStats
406 runtime.ReadMemStats(&m2)
407
408 allocDiff := int64(m2.Alloc) - int64(m1.Alloc)
409 totalAllocDiff := int64(m2.TotalAlloc) - int64(m1.TotalAlloc)
410 bytesPerOp := float64(totalAllocDiff) / float64(iterations)
411
412 t.Logf("InsertStatement pool memory stats:")
413 t.Logf(" Alloc diff: %d bytes", allocDiff)
414 t.Logf(" TotalAlloc diff: %d bytes", totalAllocDiff)
415 t.Logf(" Bytes per operation: %.2f", bytesPerOp)
416
417 const maxAllocIncrease = 1024 * 1024 // 1MB
418 const maxBytesPerOp = 6000 // 6KB
419
420 if allocDiff > maxAllocIncrease {
421 t.Errorf("Memory leak in InsertStatement pool: %d bytes (threshold: %d)", allocDiff, maxAllocIncrease)
422 }
423
424 if bytesPerOp > maxBytesPerOp {
425 t.Errorf("High memory usage in InsertStatement pool: %.2f bytes/op (threshold: %d)", bytesPerOp, maxBytesPerOp)
426 }
427
428 if allocDiff <= maxAllocIncrease && bytesPerOp <= maxBytesPerOp {
429 t.Logf("✅ InsertStatement pool memory leak test PASSED")
430 }
431}

Callers

nothing calls this directly

Calls 3

GetInsertStatementFunction · 0.85
PutInsertStatementFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected