orchestrator/geoip: add a benchmark for Iterate*Databases()

Now:

```
goos: linux
goarch: amd64
pkg: akvorado/orchestrator/geoip
cpu: AMD Ryzen 7 PRO 6850U with Radeon Graphics
BenchmarkIterDatabase/ASN-16                3376               457.0 ns/entry
BenchmarkIterDatabase/GeoIP-16              2410               754.5 ns/entry
```

Before 0a10764cc9:

```
goos: linux
goarch: amd64
pkg: akvorado/orchestrator/geoip
cpu: AMD Ryzen 7 PRO 6850U with Radeon Graphics
BenchmarkIterDatabase/ASN-16                2863               609.3 ns/entry
BenchmarkIterDatabase/GeoIP-16              3286               719.3 ns/entry
```

I was hoping for a bit more!
This commit is contained in:
Vincent Bernat
2025-08-17 08:43:59 +02:00
parent 2c8161e946
commit 6b2af58a64
4 changed files with 32 additions and 3 deletions

View File

@@ -18,7 +18,7 @@ type MockComponent struct {
} }
// NewMock will create a daemon component that does nothing. // NewMock will create a daemon component that does nothing.
func NewMock(t *testing.T) Component { func NewMock(t testing.TB) Component {
t.Helper() t.Helper()
return &MockComponent{ return &MockComponent{
lifecycleComponent: lifecycleComponent{ lifecycleComponent: lifecycleComponent{

View File

@@ -103,7 +103,7 @@ func CheckExternalService(t *testing.T, name string, candidates []string) string
} }
// StartStop starts a component and stops it on cleanup. // StartStop starts a component and stops it on cleanup.
func StartStop(t *testing.T, component any) { func StartStop(t testing.TB, component any) {
t.Helper() t.Helper()
if starterC, ok := component.(starter); ok { if starterC, ok := component.(starter); ok {
if err := starterC.Start(); err != nil { if err := starterC.Start(); err != nil {

View File

@@ -13,6 +13,35 @@ import (
"akvorado/common/reporter" "akvorado/common/reporter"
) )
func BenchmarkIterDatabase(b *testing.B) {
r := reporter.NewMock(b)
c := NewMock(b, r, true)
b.Run("ASN", func(b *testing.B) {
entries := 0
for b.Loop() {
c.IterASNDatabases(func(netip.Prefix, ASNInfo) error {
entries++
return nil
})
}
b.ReportMetric(0, "ns/op")
b.ReportMetric(float64(b.Elapsed())/float64(entries), "ns/entry")
})
b.Run("GeoIP", func(b *testing.B) {
entries := 0
for b.Loop() {
c.IterGeoDatabases(func(netip.Prefix, GeoInfo) error {
entries++
return nil
})
}
b.ReportMetric(0, "ns/op")
b.ReportMetric(float64(b.Elapsed())/float64(entries), "ns/entry")
})
}
func TestIterDatabase(t *testing.T) { func TestIterDatabase(t *testing.T) {
r := reporter.NewMock(t) r := reporter.NewMock(t)
c := NewMock(t, r, true) c := NewMock(t, r, true)

View File

@@ -21,7 +21,7 @@ import (
// available here: // available here:
// - https://github.com/maxmind/MaxMind-DB/blob/main/source-data/GeoLite2-ASN-Test.json // - https://github.com/maxmind/MaxMind-DB/blob/main/source-data/GeoLite2-ASN-Test.json
// - https://github.com/maxmind/MaxMind-DB/blob/main/source-data/GeoLite2-Country-Test.json // - https://github.com/maxmind/MaxMind-DB/blob/main/source-data/GeoLite2-Country-Test.json
func NewMock(t *testing.T, r *reporter.Reporter, withData bool) *Component { func NewMock(t testing.TB, r *reporter.Reporter, withData bool) *Component {
t.Helper() t.Helper()
config := DefaultConfiguration() config := DefaultConfiguration()
_, src, _, _ := runtime.Caller(0) _, src, _, _ := runtime.Caller(0)