From d41e148f0b393b39df642e25ddb63bbf0b458b96 Mon Sep 17 00:00:00 2001 From: Tomasz Janiszewski Date: Thu, 30 Oct 2025 11:42:35 +0100 Subject: [PATCH 1/4] chore(bench): measure heap in alert upsert test Signed-off-by: Tomasz Janiszewski --- .../internal/store/postgres/bench_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/central/alert/datastore/internal/store/postgres/bench_test.go b/central/alert/datastore/internal/store/postgres/bench_test.go index 4c03c562679ce..7b25048622c42 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,28 @@ 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) { + var maxHeap uint64 + var maxHeapObj uint64 + ticker := time.NewTicker(10 * time.Millisecond) + go func() { + var m runtime.MemStats + for range ticker.C { + runtime.GC() + runtime.ReadMemStats(&m) + maxHeap = max(maxHeap, m.HeapAlloc) + maxHeapObj = max(maxHeap, m.HeapObjects) + } + }() + for b.Loop() { err := store.UpsertMany(ctx, alerts[:n]) if err != nil { b.Fatal(err) } } + ticker.Stop() + b.ReportMetric(float64(maxHeap), "max_heap_bytes") + b.ReportMetric(float64(maxHeapObj), "max_heap_objects") }) b.Run(fmt.Sprintf("get %d alerts", n), func(b *testing.B) { for b.Loop() { From 847d8876b4f86b3cdcfac714f5a3bc59387c2b65 Mon Sep 17 00:00:00 2001 From: Tomasz Janiszewski Date: Thu, 30 Oct 2025 15:52:34 +0100 Subject: [PATCH 2/4] fix Signed-off-by: Tomasz Janiszewski --- .../internal/store/postgres/bench_test.go | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/central/alert/datastore/internal/store/postgres/bench_test.go b/central/alert/datastore/internal/store/postgres/bench_test.go index 7b25048622c42..4f890c695c726 100644 --- a/central/alert/datastore/internal/store/postgres/bench_test.go +++ b/central/alert/datastore/internal/store/postgres/bench_test.go @@ -41,16 +41,13 @@ 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() { - var m runtime.MemStats for range ticker.C { - runtime.GC() - runtime.ReadMemStats(&m) - maxHeap = max(maxHeap, m.HeapAlloc) - maxHeapObj = max(maxHeap, m.HeapObjects) + maxHeap, maxHeapObj = getHeapAllocAndObjects(maxHeap, maxHeapObj) } }() @@ -61,8 +58,8 @@ func BenchmarkMany(b *testing.B) { } } ticker.Stop() - b.ReportMetric(float64(maxHeap), "max_heap_bytes") - b.ReportMetric(float64(maxHeapObj), "max_heap_objects") + b.ReportMetric(float64(maxHeap-startHeap), "max_heap_bytes") + b.ReportMetric(float64(maxHeapObj-startHeapObj), "max_heap_objects") }) b.Run(fmt.Sprintf("get %d alerts", n), func(b *testing.B) { for b.Loop() { @@ -89,3 +86,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(maxHeap, m.HeapObjects) + return maxHeap, maxHeapObj +} From 5d100f137ed668c8cf9ed28a9932cee4290c3caf Mon Sep 17 00:00:00 2001 From: Tomasz Janiszewski Date: Tue, 4 Nov 2025 12:01:51 +0100 Subject: [PATCH 3/4] report start metrics Signed-off-by: Tomasz Janiszewski --- central/alert/datastore/internal/store/postgres/bench_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/central/alert/datastore/internal/store/postgres/bench_test.go b/central/alert/datastore/internal/store/postgres/bench_test.go index 4f890c695c726..b8c22c2885ed6 100644 --- a/central/alert/datastore/internal/store/postgres/bench_test.go +++ b/central/alert/datastore/internal/store/postgres/bench_test.go @@ -60,6 +60,8 @@ func BenchmarkMany(b *testing.B) { 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() { From d7299b6e0ebff2a5979e57cb9ef175dcc54eb1fb Mon Sep 17 00:00:00 2001 From: Tomasz Janiszewski Date: Fri, 7 Nov 2025 12:52:15 +0100 Subject: [PATCH 4/4] fix Signed-off-by: Tomasz Janiszewski --- central/alert/datastore/internal/store/postgres/bench_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/central/alert/datastore/internal/store/postgres/bench_test.go b/central/alert/datastore/internal/store/postgres/bench_test.go index b8c22c2885ed6..3a654cb7f925f 100644 --- a/central/alert/datastore/internal/store/postgres/bench_test.go +++ b/central/alert/datastore/internal/store/postgres/bench_test.go @@ -94,6 +94,6 @@ func getHeapAllocAndObjects(maxHeap uint64, maxHeapObj uint64) (uint64, uint64) var m runtime.MemStats runtime.ReadMemStats(&m) maxHeap = max(maxHeap, m.HeapAlloc) - maxHeapObj = max(maxHeap, m.HeapObjects) + maxHeapObj = max(maxHeapObj, m.HeapObjects) return maxHeap, maxHeapObj }