forked from flagship-io/flagship-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocation_test.go
203 lines (174 loc) · 6.05 KB
/
allocation_test.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package decision
import (
"errors"
"math/rand"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func testVariationGroupAlloc(vg VariationGroup, t *testing.T, isCumulativeAlloc bool) {
counts := []int{}
for i := 1; i < 100000; i++ {
vAlloc, err := getRandomAllocation(strconv.Itoa(rand.Int()), "", &vg, isCumulativeAlloc)
if err != nil {
t.Errorf("error when getting alloc: %v", err)
return
}
for i, v := range vg.Variations {
if v.ID == vAlloc.ID {
for len(counts) <= i {
counts = append(counts, 0)
}
counts[i]++
}
}
}
countTotal := 0
for i, v := range counts {
t.Logf("Count v%d : %d", i+1, v)
countTotal += v
}
nbVarWithTraffic := 0
hasVariationFullTraffic := false
for i, v := range vg.Variations {
if i == 0 || i > 0 && v.Allocation != 0 && !hasVariationFullTraffic {
nbVarWithTraffic++
}
if v.Allocation == 100 {
hasVariationFullTraffic = true
}
}
countWithTraffic := 0
for _, v := range counts {
if v > 0 {
countWithTraffic++
}
}
if countWithTraffic != nbVarWithTraffic {
t.Errorf("Problem with stats: assigned vars : %d, nb var total : %d", countWithTraffic, nbVarWithTraffic)
}
previousRatio := float32(0)
for i, v := range counts {
correctRatio := vg.Variations[i].Allocation / 100
if isCumulativeAlloc {
if i >= 1 {
previousRatio += vg.Variations[i-1].Allocation
}
correctRatio = (vg.Variations[i].Allocation - previousRatio) / 100
}
ratio := float32(v) / float32(countTotal)
if correctRatio-ratio > 0.05 {
t.Errorf("Problem with stats: ratio %f, correctRatio : %f", ratio, correctRatio)
}
}
}
func TestVariationAllocation(t *testing.T) {
variationArray := []*Variation{}
variationArray = append(variationArray, &Variation{ID: "1", Allocation: 50})
variationArray = append(variationArray, &Variation{ID: "2", Allocation: 50})
variationsGroupInfo := VariationGroup{
Variations: variationArray,
}
testVariationGroupAlloc(variationsGroupInfo, t, false)
variationArray = []*Variation{}
variationArray = append(variationArray, &Variation{ID: "1", Allocation: 33})
variationArray = append(variationArray, &Variation{ID: "2", Allocation: 33})
variationArray = append(variationArray, &Variation{ID: "3", Allocation: 34})
variationsGroupInfo = VariationGroup{
Variations: variationArray,
}
testVariationGroupAlloc(variationsGroupInfo, t, false)
variationArray = []*Variation{}
variationArray = append(variationArray, &Variation{ID: "1", Allocation: 10})
variationArray = append(variationArray, &Variation{ID: "2", Allocation: 25})
variationArray = append(variationArray, &Variation{ID: "3", Allocation: 25})
variationArray = append(variationArray, &Variation{ID: "4", Allocation: 40})
variationsGroupInfo = VariationGroup{
Variations: variationArray,
}
testVariationGroupAlloc(variationsGroupInfo, t, false)
variationArray = []*Variation{}
variationArray = append(variationArray, &Variation{ID: "1", Allocation: 90})
variationArray = append(variationArray, &Variation{ID: "2", Allocation: 10})
variationArray = append(variationArray, &Variation{ID: "3", Allocation: 0})
variationArray = append(variationArray, &Variation{ID: "4", Allocation: 0})
variationsGroupInfo = VariationGroup{
Variations: variationArray,
}
testVariationGroupAlloc(variationsGroupInfo, t, false)
variationArray = []*Variation{}
variationArray = append(variationArray, &Variation{ID: "1", Allocation: 90})
variationArray = append(variationArray, &Variation{ID: "2", Allocation: 100})
variationArray = append(variationArray, &Variation{ID: "3", Allocation: 100})
variationArray = append(variationArray, &Variation{ID: "4", Allocation: 100})
variationsGroupInfo = VariationGroup{
Variations: variationArray,
}
testVariationGroupAlloc(variationsGroupInfo, t, true)
variationArray = []*Variation{}
variationArray = append(variationArray, &Variation{ID: "1", Allocation: 90})
variationArray = append(variationArray, &Variation{ID: "2", Allocation: 0})
variationArray = append(variationArray, &Variation{ID: "3", Allocation: 10})
variationArray = append(variationArray, &Variation{ID: "4", Allocation: 0})
variationsGroupInfo = VariationGroup{
Variations: variationArray,
}
testVariationGroupAlloc(variationsGroupInfo, t, false)
variationArray = []*Variation{}
variationArray = append(variationArray, &Variation{ID: "1", Allocation: 50})
variationArray = append(variationArray, &Variation{ID: "2", Allocation: 0})
variationsGroupInfo = VariationGroup{
Variations: variationArray,
}
allocErrors := []error{}
nbTrials := 100000
for i := 1; i < nbTrials; i++ {
_, err := getRandomAllocation(strconv.Itoa(rand.Int()), "", &variationsGroupInfo, false)
if err != nil {
allocErrors = append(allocErrors, err)
continue
}
}
errRatio := float64(len(allocErrors)) / float64(nbTrials)
t.Logf("errRatio: %f", errRatio)
isRatioCorrect := 0.5-errRatio < 0.05
assert.EqualValues(t, errors.New("Visitor untracked"), allocErrors[0])
assert.True(t, isRatioCorrect)
}
func TestIsVisitorInBucket(t *testing.T) {
// visitor in bucket 100% should be allowed
is, err := isVisitorInBucket("123", &Campaign{
ID: "123",
BucketRanges: [][]float64{{0., 100.}},
})
assert.Nil(t, err)
assert.True(t, is)
// visitor in bucket in its appropriate bucket should be allowed
is, err = isVisitorInBucket("123", &Campaign{
ID: "123",
BucketRanges: [][]float64{{71., 71.5}},
})
assert.Nil(t, err)
assert.True(t, is)
// visitor in bucket in one of its appropriate bucket should be allowed
is, err = isVisitorInBucket("123", &Campaign{
ID: "123",
BucketRanges: [][]float64{{71., 71.5}, {40., 50.}},
})
assert.Nil(t, err)
assert.True(t, is)
// same condition but on an other campaign, the visitor have the same hash and should be
is, err = isVisitorInBucket("123", &Campaign{
ID: "456",
BucketRanges: [][]float64{{71., 71.5}},
})
assert.Nil(t, err)
assert.True(t, is)
// no buckets, visitor not allocated
is, err = isVisitorInBucket("123", &Campaign{
ID: "999",
BucketRanges: [][]float64{},
})
assert.Nil(t, err)
assert.False(t, is)
}