This code is intended to check if canary rollouts are:
- sticky
- proportional to what we set in the UI
In this case I created a feature flag called insights-operator-gathering-conditions-service
in insights.unleash.devshift.com
with a stickiness in the userId
field and a gradual rollout of 50% of the
population.
I created a benchmark where I check the answers of this dummy function defined in main.go
func getInt(clusterID string) int {
if unleash.IsEnabled(
unleashFeatureToggle,
unleash.WithContext(context.Context{
UserId: clusterID,
})) {
return 1
}
return 0
}
that returns 1
for all clusterID
(userId
in terms of Unleash) that are
categorized by Unleash as canary candidates. The benchmark is configured using
some constant values
const (
nClusters = 500
nRequestsPerCluster = 10
wantPercentage = 50
)
and prints a summary with the results:
❯ go test -v -bench=. -count 1
goos: darwin
goarch: arm64
pkg: github.com/juandspy/learning-feature-flags
BenchmarkGetInt
Total percentage of clusters using the canary version: 50.40%, want 50%
Total percentage of clusters using the canary version: 49.40%, want 50%
Total percentage of clusters using the canary version: 49.20%, want 50%
Total percentage of clusters using the canary version: 50.40%, want 50%
Total percentage of clusters using the canary version: 52.40%, want 50%
Total percentage of clusters using the canary version: 50.60%, want 50%
BenchmarkGetInt-8 1000000000 0.005782 ns/op
PASS
ok github.com/juandspy/learning-feature-flags 0.922s
We can see that the percentage of clusters identified for the canary is around 50% and that the benchmark passes without any failure. This is important as there is a check for stickiness:
// Ensure the stickiness:
// Assert that all items in results[u] are the same
first := results[u][0]
for j := 1; j < nRequestsPerCluster; j++ {
if results[u][j] != first {
b.Errorf("Values are not the same for UUID %s: %v", u, results[u])
}
}