From c73e3ca9aabded6dd1aa5765e961a26436b082a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Fri, 1 Sep 2023 11:55:21 +0100 Subject: [PATCH] test: benchmark feature toggle evaluation --- benchmark_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 benchmark_test.go diff --git a/benchmark_test.go b/benchmark_test.go new file mode 100644 index 0000000..bea7298 --- /dev/null +++ b/benchmark_test.go @@ -0,0 +1,50 @@ +package unleash_test + +import ( + "fmt" + "net/http" + "testing" + "time" + + "github.com/Unleash/unleash-client-go/v3" +) + +type NoOpListener struct{} + +func (l *NoOpListener) OnReady() {} +func (l *NoOpListener) OnError(err error) {} +func (l *NoOpListener) OnWarning(warning error) {} +func (l *NoOpListener) OnCount(name string, enabled bool) {} +func (l *NoOpListener) OnSent(payload unleash.MetricsData) {} +func (l *NoOpListener) OnRegistered(payload unleash.ClientData) {} + +func BenchmarkFeatureToggleEvaluation(b *testing.B) { + unleash.Initialize( + unleash.WithListener(&NoOpListener{}), + unleash.WithAppName("go-benchmark"), + unleash.WithUrl("https://app.unleash-hosted.com/demo/api/"), + unleash.WithCustomHeaders(http.Header{"Authorization": {"Go-Benchmark:development.be6b5d318c8e77469efb58590022bb6416100261accf95a15046c04d"}}), + ) + + b.ResetTimer() + startTime := time.Now() + + for i := 0; i < b.N; i++ { + _ = unleash.IsEnabled("go-benchmark") + } + + endTime := time.Now() + b.StopTimer() + + // Calculate ns/op (nanoseconds per operation) + nsPerOp := float64(endTime.Sub(startTime).Nanoseconds()) / float64(b.N) + + // Calculate operations per day + opsPerSec := 1e9 / nsPerOp + opsPerDay := opsPerSec * 60 * 60 * 24 + + if b.N > 1000000 { // Only print if the number of iterations is large enough for a stable result + opsPerDayTrillions := opsPerDay / 1e9 // Convert to billions + fmt.Printf("Final Estimated Operations Per Day: %.3f billion (%e)\n", opsPerDayTrillions, opsPerDay) + } +}