From 59e907673d9d846735d9e3d16a9410b9ac679ca5 Mon Sep 17 00:00:00 2001 From: Kostiantyn Masliuk <1pkg@protonmail.com> Date: Thu, 3 Oct 2024 21:04:51 -0700 Subject: [PATCH] benchmarks: apmbench SetMemoryLimit so benchmarks do not go OOM (#14257) --- systemtest/cmd/apmbench/main.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/systemtest/cmd/apmbench/main.go b/systemtest/cmd/apmbench/main.go index 26a8df18e6c..075cc1b4849 100644 --- a/systemtest/cmd/apmbench/main.go +++ b/systemtest/cmd/apmbench/main.go @@ -22,6 +22,7 @@ import ( "flag" "fmt" "log" + "runtime/debug" "testing" "time" @@ -30,6 +31,7 @@ import ( "golang.org/x/time/rate" "github.com/elastic/apm-server/systemtest/benchtest" + "github.com/elastic/go-sysinfo" ) func Benchmark1000Transactions(b *testing.B, l *rate.Limiter) { @@ -138,6 +140,11 @@ func Benchmark10000AggregationGroups(b *testing.B, l *rate.Limiter) { func main() { flag.Parse() + bytes, err := sysMemory() + if err != nil { + log.Fatal(err) + } + debug.SetMemoryLimit(int64(float64(bytes) * 0.9)) if err := benchtest.Run( Benchmark1000Transactions, BenchmarkOTLPTraces, @@ -151,3 +158,15 @@ func main() { log.Fatal(err) } } + +func sysMemory() (uint64, error) { + host, err := sysinfo.Host() + if err != nil { + return 0, err + } + mem, err := host.Memory() + if err != nil { + return 0, err + } + return mem.Total, nil +}