Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add evaluation benchmarks #185

Merged
merged 1 commit into from
Nov 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ setup: ## Install dev tools
@echo ">> installing dev tools"
go install -v $(TOOLS)

.PHONY: bench
bench: ## Run all the benchmarks
@echo ">> running benchmarks"
go test -v -bench=. $(SOURCE_FILES) -run=XXX

.PHONY: test
test: ## Run all the tests
@echo ">> running tests"
Expand Down
2 changes: 1 addition & 1 deletion storage/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ func TestMain(m *testing.M) {

func run(m *testing.M) int {
logger = logrus.New()
logger.Level = logrus.DebugLevel

debug := os.Getenv("DEBUG")
if debug == "" {
logger.Level = logrus.DebugLevel
logger.Out = ioutil.Discard
}

Expand Down
324 changes: 324 additions & 0 deletions storage/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1323,3 +1323,327 @@ func Test_evaluate(t *testing.T) {
assert.Empty(t, d)
})
}

func BenchmarkEvaluate_SingleVariantDistribution(b *testing.B) {
flag, _ := flagStore.CreateFlag(context.TODO(), &flipt.CreateFlagRequest{
Key: b.Name(),
Name: b.Name(),
Description: "foo",
Enabled: true,
})

var variants []*flipt.Variant

for _, req := range []*flipt.CreateVariantRequest{
{
FlagKey: flag.Key,
Key: fmt.Sprintf("foo_%s", b.Name()),
},
{
FlagKey: flag.Key,
Key: fmt.Sprintf("bar_%s", b.Name()),
},
} {
variant, _ := flagStore.CreateVariant(context.TODO(), req)
variants = append(variants, variant)
}

segment, _ := segmentStore.CreateSegment(context.TODO(), &flipt.CreateSegmentRequest{
Key: b.Name(),
Name: b.Name(),
Description: "foo",
})

_, _ = segmentStore.CreateConstraint(context.TODO(), &flipt.CreateConstraintRequest{
SegmentKey: segment.Key,
Type: flipt.ComparisonType_STRING_COMPARISON_TYPE,
Property: "bar",
Operator: opEQ,
Value: "baz",
})

_, _ = segmentStore.CreateConstraint(context.TODO(), &flipt.CreateConstraintRequest{
SegmentKey: segment.Key,
Type: flipt.ComparisonType_BOOLEAN_COMPARISON_TYPE,
Property: "admin",
Operator: opTrue,
})

rule, _ := ruleStore.CreateRule(context.TODO(), &flipt.CreateRuleRequest{
FlagKey: flag.Key,
SegmentKey: segment.Key,
})

_, _ = ruleStore.CreateDistribution(context.TODO(), &flipt.CreateDistributionRequest{
FlagKey: flag.Key,
RuleId: rule.Id,
VariantId: variants[0].Id,
Rollout: 100,
})

runs := []struct {
name string
req *flipt.EvaluationRequest
}{
{
name: "match string value",
req: &flipt.EvaluationRequest{
FlagKey: flag.Key,
EntityId: "1",
Context: map[string]string{
"bar": "baz",
"admin": "true",
},
},
},
{
name: "no match string value",
req: &flipt.EvaluationRequest{
FlagKey: flag.Key,
EntityId: "1",
Context: map[string]string{
"bar": "boz",
"admin": "true",
},
},
},
{
name: "no match just bool value",
req: &flipt.EvaluationRequest{
FlagKey: flag.Key,
EntityId: "1",
Context: map[string]string{
"admin": "true",
},
},
},
{
name: "no match just string value",
req: &flipt.EvaluationRequest{
FlagKey: flag.Key,
EntityId: "1",
Context: map[string]string{
"bar": "baz",
},
},
},
}

for _, bb := range runs {
req := bb.req

b.Run(bb.name, func(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
_, _ = evaluator.Evaluate(context.TODO(), req)
}
})
}
}

func BenchmarkEvaluate_RolloutDistribution(b *testing.B) {
flag, _ := flagStore.CreateFlag(context.TODO(), &flipt.CreateFlagRequest{
Key: b.Name(),
Name: b.Name(),
Description: "foo",
Enabled: true,
})

var variants []*flipt.Variant

for _, req := range []*flipt.CreateVariantRequest{
{
FlagKey: flag.Key,
Key: fmt.Sprintf("foo_%s", b.Name()),
},
{
FlagKey: flag.Key,
Key: fmt.Sprintf("bar_%s", b.Name()),
},
} {
variant, _ := flagStore.CreateVariant(context.TODO(), req)
variants = append(variants, variant)
}

segment, _ := segmentStore.CreateSegment(context.TODO(), &flipt.CreateSegmentRequest{
Key: b.Name(),
Name: b.Name(),
Description: "foo",
})

_, _ = segmentStore.CreateConstraint(context.TODO(), &flipt.CreateConstraintRequest{
SegmentKey: segment.Key,
Type: flipt.ComparisonType_STRING_COMPARISON_TYPE,
Property: "bar",
Operator: opEQ,
Value: "baz",
})

rule, _ := ruleStore.CreateRule(context.TODO(), &flipt.CreateRuleRequest{
FlagKey: flag.Key,
SegmentKey: segment.Key,
})

for _, req := range []*flipt.CreateDistributionRequest{
{
FlagKey: flag.Key,
RuleId: rule.Id,
VariantId: variants[0].Id,
Rollout: 50,
},
{
FlagKey: flag.Key,
RuleId: rule.Id,
VariantId: variants[1].Id,
Rollout: 50,
},
} {
_, _ = ruleStore.CreateDistribution(context.TODO(), req)
}

runs := []struct {
name string
req *flipt.EvaluationRequest
}{
{
name: "match string value - variant 1",
req: &flipt.EvaluationRequest{
FlagKey: flag.Key,
EntityId: "1",
Context: map[string]string{
"bar": "baz",
},
},
},
{
name: "match string value - variant 2",
req: &flipt.EvaluationRequest{
FlagKey: flag.Key,
EntityId: "10",
Context: map[string]string{
"bar": "baz",
},
},
},
{
name: "no match string value",
req: &flipt.EvaluationRequest{
FlagKey: flag.Key,
EntityId: "1",
Context: map[string]string{
"bar": "boz",
},
},
},
}

for _, bb := range runs {
req := bb.req

b.Run(bb.name, func(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
_, _ = evaluator.Evaluate(context.TODO(), req)
}
})
}
}

func BenchmarkEvaluate_NoConstraints(b *testing.B) {
flag, _ := flagStore.CreateFlag(context.TODO(), &flipt.CreateFlagRequest{
Key: b.Name(),
Name: b.Name(),
Description: "foo",
Enabled: true,
})

var variants []*flipt.Variant

for _, req := range []*flipt.CreateVariantRequest{
{
FlagKey: flag.Key,
Key: fmt.Sprintf("foo_%s", b.Name()),
},
{
FlagKey: flag.Key,
Key: fmt.Sprintf("bar_%s", b.Name()),
},
} {
variant, _ := flagStore.CreateVariant(context.TODO(), req)
variants = append(variants, variant)
}

segment, _ := segmentStore.CreateSegment(context.TODO(), &flipt.CreateSegmentRequest{
Key: b.Name(),
Name: b.Name(),
Description: "foo",
})

rule, _ := ruleStore.CreateRule(context.TODO(), &flipt.CreateRuleRequest{
FlagKey: flag.Key,
SegmentKey: segment.Key,
})

for _, req := range []*flipt.CreateDistributionRequest{
{
FlagKey: flag.Key,
RuleId: rule.Id,
VariantId: variants[0].Id,
Rollout: 50,
},
{
FlagKey: flag.Key,
RuleId: rule.Id,
VariantId: variants[1].Id,
Rollout: 50,
},
} {
_, _ = ruleStore.CreateDistribution(context.TODO(), req)
}

runs := []struct {
name string
req *flipt.EvaluationRequest
}{
{
name: "match no value - variant 1",
req: &flipt.EvaluationRequest{
FlagKey: flag.Key,
EntityId: "01",
Context: map[string]string{},
},
},
{
name: "match no value - variant 2",
req: &flipt.EvaluationRequest{
FlagKey: flag.Key,
EntityId: "10",
Context: map[string]string{},
},
},
{
name: "match string value - variant 2",
req: &flipt.EvaluationRequest{
FlagKey: flag.Key,
EntityId: "10",
Context: map[string]string{
"bar": "boz",
},
},
},
}

for _, bb := range runs {
req := bb.req

b.Run(bb.name, func(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
_, _ = evaluator.Evaluate(context.TODO(), req)
}
})
}
}