-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
79 lines (64 loc) · 1.92 KB
/
benchmark_test.go
File metadata and controls
79 lines (64 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package unleash_test
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/Unleash/unleash-go-sdk/v6"
"github.com/Unleash/unleash-go-sdk/v6/api"
)
type mockStorage map[string]api.Feature
func (m mockStorage) Init(backupPath string, appName string) {}
func (m mockStorage) Load() (*api.FeatureResponse, error) {
features := make([]api.Feature, 0, len(m))
for _, f := range m {
features = append(features, f)
}
return &api.FeatureResponse{
Features: features,
Segments: make([]api.Segment, 0),
}, nil
}
func (m mockStorage) Persist(*api.FeatureResponse) error { return nil }
func ptr[T any](v T) *T {
return &v
}
func BenchmarkFeatureToggleEvaluation(b *testing.B) {
err := unleash.Initialize(
unleash.WithListener(&unleash.NoopListener{}),
unleash.WithAppName("go-benchmark"),
unleash.WithUrl("https://app.unleash-hosted.com/demo/api/"),
unleash.WithCustomHeaders(http.Header{"Authorization": {"Go-Benchmark:development.be6b5d318c8e77469efb58590022bb6416100261accf95a15046c04d"}}),
unleash.WithStorage(mockStorage{
"foo": api.Feature{
Name: "foo",
Enabled: true,
Dependencies: &[]api.Dependency{
{Feature: "bar", Enabled: ptr(true)},
},
},
"bar": api.Feature{
Name: "bar",
Enabled: true,
},
}),
)
if err != nil {
b.Fatal(err)
}
startTime := time.Now()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = unleash.IsEnabled("foo", unleash.FeatureOptions{})
}
endTime := time.Now()
// 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
opsPerDayBillions := opsPerDay / 1e9 // Convert to billions
fmt.Printf("Final Estimated Operations Per Day: %.3f billion (%e)\n", opsPerDayBillions, opsPerDay)
}
}