Skip to content

Commit

Permalink
ci: expanded benchmark coverage (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-milligan authored Dec 13, 2022
1 parent c2a9ac9 commit 8108d7d
Show file tree
Hide file tree
Showing 2 changed files with 393 additions and 41 deletions.
214 changes: 173 additions & 41 deletions pkg/eval/fractional_evaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/open-feature/flagd/pkg/logger"
"github.com/open-feature/flagd/pkg/model"

"google.golang.org/protobuf/types/known/structpb"
)

Expand Down Expand Up @@ -176,26 +175,26 @@ func TestFractionalEvaluation(t *testing.T) {
"yellow": "#FFFF00",
},
Targeting: []byte(`{
"fractionalEvaluation": [
"email",
[
"red",
25
],
[
"blue",
25
],
[
"green",
25
],
[
"yellow",
25
]
]
}`),
"fractionalEvaluation": [
"email",
[
"red",
25
],
[
"blue",
25
],
[
"green",
25
],
[
"yellow",
25
]
]
}`),
},
},
},
Expand All @@ -218,14 +217,14 @@ func TestFractionalEvaluation(t *testing.T) {
"yellow": "#FFFF00",
},
Targeting: []byte(`{
"fractionalEvaluation": [
"email",
[
"black",
100
]
]
}`),
"fractionalEvaluation": [
"email",
[
"black",
100
]
]
}`),
},
},
},
Expand All @@ -252,18 +251,18 @@ func TestFractionalEvaluation(t *testing.T) {
"yellow": "#FFFF00",
},
Targeting: []byte(`{
"fractionalEvaluation": [
"email",
[
"red",
25
],
[
"blue",
25
]
]
}`),
"fractionalEvaluation": [
"email",
[
"red",
25
],
[
"blue",
25
]
]
}`),
},
},
},
Expand Down Expand Up @@ -306,3 +305,136 @@ func TestFractionalEvaluation(t *testing.T) {
})
}
}

func BenchmarkFractionalEvaluation(b *testing.B) {
flags := Flags{
Flags: map[string]Flag{
"headerColor": {
State: "ENABLED",
DefaultVariant: "red",
Variants: map[string]any{
"red": "#FF0000",
"blue": "#0000FF",
"green": "#00FF00",
"yellow": "#FFFF00",
},
Targeting: []byte(`{
"if": [
{
"in": ["@faas.com", {
"var": ["email"]
}]
},
{
"fractionalEvaluation": [
"email",
[
"red",
25
],
[
"blue",
25
],
[
"green",
25
],
[
"yellow",
25
]
]
}, null
]
}`),
},
},
}

tests := map[string]struct {
flags Flags
flagKey string
context *structpb.Struct
expectedValue string
expectedVariant string
expectedReason string
expectedError error
}{
"test@faas.com": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "test@faas.com",
}},
}},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"test2@faas.com": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "test2@faas.com",
}},
}},
expectedVariant: "yellow",
expectedValue: "#FFFF00",
expectedReason: model.TargetingMatchReason,
},
"test3@faas.com": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "test3@faas.com",
}},
}},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"test4@faas.com": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "test4@faas.com",
}},
}},
expectedVariant: "blue",
expectedValue: "#0000FF",
expectedReason: model.TargetingMatchReason,
},
}
reqID := "test"
for name, tt := range tests {
b.Run(name, func(b *testing.B) {
je := JSONEvaluator{state: tt.flags}
for i := 0; i < b.N; i++ {
value, variant, reason, err := resolve[string](
reqID, tt.flagKey, tt.context, je.evaluateVariant, je.state.Flags[tt.flagKey].Variants,
)

if value != tt.expectedValue {
b.Errorf("expected value '%s', got '%s'", tt.expectedValue, value)
}

if variant != tt.expectedVariant {
b.Errorf("expected variant '%s', got '%s'", tt.expectedVariant, variant)
}

if reason != tt.expectedReason {
b.Errorf("expected reason '%s', got '%s'", tt.expectedReason, reason)
}

if err != tt.expectedError {
b.Errorf("expected err '%v', got '%v'", tt.expectedError, err)
}
}
})
}
}
Loading

0 comments on commit 8108d7d

Please sign in to comment.