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

Function TestMemoryLeaks_UpdateStatementPool

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

Source from the content-addressed store, hash-verified

431}
432
433func TestMemoryLeaks_UpdateStatementPool(t *testing.T) {
434 runtime.GC()
435 runtime.GC()
436 time.Sleep(10 * time.Millisecond)
437
438 var m1 runtime.MemStats
439 runtime.ReadMemStats(&m1)
440
441 const iterations = 10000
442
443 t.Logf("Running %d iterations of UpdateStatement pool operations...", iterations)
444
445 for i := 0; i < iterations; i++ {
446 stmt := GetUpdateStatement()
447
448 stmt.TableName = "users"
449 stmt.Assignments = append(stmt.Assignments, UpdateExpression{
450 Column: &Identifier{Name: "status"},
451 Value: &LiteralValue{Value: "active"},
452 })
453 stmt.Where = &BinaryExpression{
454 Left: &Identifier{Name: "id"},
455 Operator: "=",
456 Right: &LiteralValue{Value: "123"},
457 }
458
459 PutUpdateStatement(stmt)
460
461 if i%1000 == 0 && i > 0 {
462 runtime.GC()
463 }
464 }
465
466 runtime.GC()
467 runtime.GC()
468 time.Sleep(10 * time.Millisecond)
469
470 var m2 runtime.MemStats
471 runtime.ReadMemStats(&m2)
472
473 allocDiff := int64(m2.Alloc) - int64(m1.Alloc)
474 totalAllocDiff := int64(m2.TotalAlloc) - int64(m1.TotalAlloc)
475 bytesPerOp := float64(totalAllocDiff) / float64(iterations)
476
477 t.Logf("UpdateStatement pool memory stats:")
478 t.Logf(" Alloc diff: %d bytes", allocDiff)
479 t.Logf(" TotalAlloc diff: %d bytes", totalAllocDiff)
480 t.Logf(" Bytes per operation: %.2f", bytesPerOp)
481
482 const maxAllocIncrease = 1024 * 1024 // 1MB
483 const maxBytesPerOp = 6000 // 6KB
484
485 if allocDiff > maxAllocIncrease {
486 t.Errorf("Memory leak in UpdateStatement pool: %d bytes (threshold: %d)", allocDiff, maxAllocIncrease)
487 }
488
489 if bytesPerOp > maxBytesPerOp {
490 t.Errorf("High memory usage in UpdateStatement pool: %.2f bytes/op (threshold: %d)", bytesPerOp, maxBytesPerOp)

Callers

nothing calls this directly

Calls 3

GetUpdateStatementFunction · 0.85
PutUpdateStatementFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected