From 6c81593f2ad33e49df7047bb0b07b1675086156d Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Tue, 6 Feb 2024 23:44:58 -0800 Subject: [PATCH] Feat[testing]: Add benchmarks --- ratelimiter_test.go | 98 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/ratelimiter_test.go b/ratelimiter_test.go index 3049f61..57ba7f6 100644 --- a/ratelimiter_test.go +++ b/ratelimiter_test.go @@ -346,3 +346,101 @@ func Test_debugChannelOverflow(t *testing.T) { t.Fatalf("debug channel did not overflow") } } + +func BenchmarkCheck(b *testing.B) { + b.StopTimer() + b.ReportAllocs() + limiter := NewDefaultLimiter() + b.StartTimer() + for n := 0; n < b.N; n++ { + limiter.Check(dummyTicker) + } +} + +func BenchmarkCheckHardcore(b *testing.B) { + b.StopTimer() + b.ReportAllocs() + limiter := NewHardcoreLimiter(25, 25) + b.StartTimer() + for n := 0; n < b.N; n++ { + limiter.Check(dummyTicker) + } +} + +func BenchmarkCheckStrict(b *testing.B) { + b.StopTimer() + b.ReportAllocs() + limiter := NewStrictLimiter(25, 25) + b.StartTimer() + for n := 0; n < b.N; n++ { + limiter.Check(dummyTicker) + } +} + +func BenchmarkCheckStringer(b *testing.B) { + b.StopTimer() + b.ReportAllocs() + limiter := NewDefaultLimiter() + b.StartTimer() + for n := 0; n < b.N; n++ { + limiter.CheckStringer(dummyTicker) + } +} + +func BenchmarkPeek(b *testing.B) { + b.StopTimer() + b.ReportAllocs() + limiter := NewDefaultLimiter() + b.StartTimer() + for n := 0; n < b.N; n++ { + limiter.Peek(dummyTicker) + } +} + +func BenchmarkConcurrentCheck(b *testing.B) { + b.StopTimer() + b.ReportAllocs() + limiter := NewDefaultLimiter() + b.StartTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + limiter.Check(dummyTicker) + } + }) +} + +func BenchmarkConcurrentSetAndCheckHardcore(b *testing.B) { + b.StopTimer() + b.ReportAllocs() + limiter := NewHardcoreLimiter(25, 25) + b.StartTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + limiter.Check(dummyTicker) + } + }) +} + +func BenchmarkConcurrentSetAndCheckStrict(b *testing.B) { + b.StopTimer() + b.ReportAllocs() + limiter := NewDefaultStrictLimiter() + b.StartTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + limiter.Check(dummyTicker) + } + }) +} + +func BenchmarkConcurrentPeek(b *testing.B) { + b.StopTimer() + b.ReportAllocs() + limiter := NewDefaultLimiter() + b.StartTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + limiter.Peek(dummyTicker) + } + }) +}