A/B Testing
Problem statement:
Write AB testing logic in java, which can route to branches based on the percentage given.
Example:
A 25
B 75
In this case, 25% of traffic has to go to "A" and 75% to "B"
Solution:
Step 1:
Find GCD of the percentages 25 and 75. GCD is 25
Divide them by the GCD which results in 1 and 3 is the converted percentage.
Step 2:
Build request lookup map for the sum of converted percentage.
Have a counter map for each branches, for each request the selected lowest percentage branch counter will be incremented.
Request Count Counter Map Converted Percentage Lowest Percentage
A B A(1) B(3)
0 0 0 0 0 A
1 1 0 1 0 B
2 1 1 1 0.3 B
3 1 2 1 0.6 B
If we notice in the above table, out of 4 requests A was selected only once and B selected thrice.
This contributes to the respective percentage.
Test:
Now this request map along with sum of converted percentage 4(1+3) can be used to AB route.
Request Count Modulo%4 Request map lookup Route
1 1 {0:A, 1:B, 2:B, 3:B} B
2 2 {0:A, 1:B, 2:B, 3:B} B
3 3 {0:A, 1:B, 2:B, 3:B} B
4 0 {0:A, 1:B, 2:B, 3:B} A
5 1 {0:A, 1:B, 2:B, 3:B} B
6 2 {0:A, 1:B, 2:B, 3:B} B
7 3 {0:A, 1:B, 2:B, 3:B} B
8 0 {0:A, 1:B, 2:B, 3:B} A
Once the request map is built, then it's simple modulo and lookup to select the route.
Please find the code below, the logic can work for more than 2 routes/branches.
Once the request map is built, then it's simple modulo and lookup to select the route.
Please find the code below, the logic can work for more than 2 routes/branches.
Comments
Post a Comment