diff --git a/central/alert/datastore/internal/store/postgres/bench_test.go b/central/alert/datastore/internal/store/postgres/bench_test.go index 4c03c562679ce..3a654cb7f925f 100644 --- a/central/alert/datastore/internal/store/postgres/bench_test.go +++ b/central/alert/datastore/internal/store/postgres/bench_test.go @@ -3,7 +3,9 @@ package postgres import ( "context" "fmt" + "runtime" "testing" + "time" "github.com/stackrox/rox/generated/storage" "github.com/stackrox/rox/pkg/postgres/pgtest" @@ -39,12 +41,27 @@ func BenchmarkMany(b *testing.B) { for n := 1; n < alertsNum; n = n * 2 { b.Run(fmt.Sprintf("upsert %d alerts", n), func(b *testing.B) { + startHeap, startHeapObj := getHeapAllocAndObjects(0, 0) + var maxHeap uint64 + var maxHeapObj uint64 + ticker := time.NewTicker(10 * time.Millisecond) + go func() { + for range ticker.C { + maxHeap, maxHeapObj = getHeapAllocAndObjects(maxHeap, maxHeapObj) + } + }() + for b.Loop() { err := store.UpsertMany(ctx, alerts[:n]) if err != nil { b.Fatal(err) } } + ticker.Stop() + b.ReportMetric(float64(maxHeap-startHeap), "max_heap_bytes") + b.ReportMetric(float64(maxHeapObj-startHeapObj), "max_heap_objects") + b.ReportMetric(float64(startHeap), "start_heap_bytes") + b.ReportMetric(float64(startHeapObj), "start_heap_objects") }) b.Run(fmt.Sprintf("get %d alerts", n), func(b *testing.B) { for b.Loop() { @@ -71,3 +88,12 @@ func BenchmarkMany(b *testing.B) { }) } } + +func getHeapAllocAndObjects(maxHeap uint64, maxHeapObj uint64) (uint64, uint64) { + runtime.GC() + var m runtime.MemStats + runtime.ReadMemStats(&m) + maxHeap = max(maxHeap, m.HeapAlloc) + maxHeapObj = max(maxHeapObj, m.HeapObjects) + return maxHeap, maxHeapObj +}