chore(benchmarks): use b.Loop() BE-12182 (#1072)

pull/12341/merge
andres-portainer 2025-08-20 12:54:26 -03:00 committed by GitHub
parent 89f5a20786
commit 30aba86380
2 changed files with 8 additions and 17 deletions

View File

@ -189,9 +189,7 @@ func BenchmarkFilterEndpointsBySearchCriteria_PartialMatch(b *testing.B) {
searchString := "edge-group" searchString := "edge-group"
b.ResetTimer() for b.Loop() {
for range b.N {
e := filterEndpointsBySearchCriteria(endpoints, endpointGroups, edgeGroups, tagsMap, searchString) e := filterEndpointsBySearchCriteria(endpoints, endpointGroups, edgeGroups, tagsMap, searchString)
if len(e) != n { if len(e) != n {
b.FailNow() b.FailNow()
@ -237,13 +235,9 @@ func BenchmarkFilterEndpointsBySearchCriteria_FullMatch(b *testing.B) {
searchString := "edge-group" searchString := "edge-group"
b.ResetTimer() for b.Loop() {
for range b.N {
e := filterEndpointsBySearchCriteria(endpoints, endpointGroups, edgeGroups, tagsMap, searchString) e := filterEndpointsBySearchCriteria(endpoints, endpointGroups, edgeGroups, tagsMap, searchString)
if len(e) != n { require.Len(b, e, n)
b.FailNow()
}
} }
} }

View File

@ -4,6 +4,8 @@ import (
"testing" "testing"
"github.com/portainer/portainer/api/slicesx" "github.com/portainer/portainer/api/slicesx"
"github.com/stretchr/testify/require"
) )
func Test_Filter(t *testing.T) { func Test_Filter(t *testing.T) {
@ -60,12 +62,9 @@ func Benchmark_Filter(b *testing.B) {
source[i] = i source[i] = i
} }
b.ResetTimer() for b.Loop() {
for range b.N {
e := slicesx.Filter(source, func(x int) bool { return x%2 == 0 }) e := slicesx.Filter(source, func(x int) bool { return x%2 == 0 })
if len(e) != n/2 { require.Len(b, e, n/2)
b.FailNow()
}
} }
} }
@ -89,8 +88,6 @@ func Benchmark_FilterInPlace(b *testing.B) {
b.ResetTimer() b.ResetTimer()
for i := range b.N { for i := range b.N {
e := slicesx.FilterInPlace(copies[i], func(x int) bool { return x%2 == 0 }) e := slicesx.FilterInPlace(copies[i], func(x int) bool { return x%2 == 0 })
if len(e) != n/2 { require.Len(b, e, n/2)
b.FailNow()
}
} }
} }