bench/sampler_bench_test.go

package bench_test

import (
	"testing"
	"time"

	"mercemay.top/src/lambdalog/internal/sampler"
	"mercemay.top/src/lambdalog/internal/sampler/adaptive"
	"mercemay.top/src/lambdalog/internal/sampler/random"
	"mercemay.top/src/lambdalog/internal/sampler/threshold"
)

func BenchmarkRandomSampler(b *testing.B) {
	s := random.NewPercent(10)
	s.SeedFixed(1)
	in := sampler.Input{Now: time.Unix(0, 0), Level: "info"}
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = s.Sample(in)
	}
}

func BenchmarkAdaptiveSampler(b *testing.B) {
	s := adaptive.New(100)
	s.SeedFixed(1)
	base := time.Unix(0, 0)
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = s.Sample(sampler.Input{Now: base.Add(time.Duration(i))})
	}
}

func BenchmarkThresholdSampler(b *testing.B) {
	s := threshold.New("warn")
	in := sampler.Input{Level: "error"}
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = s.Sample(in)
	}
}

func BenchmarkChain_Combined(b *testing.B) {
	chain := sampler.Chain{threshold.New("debug"), random.NewPercent(50)}
	in := sampler.Input{Level: "info", Now: time.Unix(0, 0)}
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = chain.Sample(in)
	}
}

func BenchmarkAdaptive_Parallel(b *testing.B) {
	s := adaptive.New(1000)
	s.SeedFixed(2)
	b.ReportAllocs()
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		now := time.Unix(0, 0)
		for pb.Next() {
			_ = s.Sample(sampler.Input{Now: now})
		}
	})
}