Skip to content

Commit

Permalink
test: benchmark feature toggle evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
nunogois committed Sep 1, 2023
1 parent b18d777 commit c73e3ca
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit c73e3ca

Please sign in to comment.