|
| 1 | +package cloud.eppo; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import cloud.eppo.logging.Assignment; |
| 7 | +import cloud.eppo.logging.AssignmentLogger; |
| 8 | +import cloud.eppo.ufc.dto.Attributes; |
| 9 | +import java.lang.management.ManagementFactory; |
| 10 | +import java.lang.management.ThreadMXBean; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.concurrent.atomic.AtomicInteger; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +public class ProfileBaseEppoClientTest { |
| 20 | + private static final Logger log = LoggerFactory.getLogger(ProfileBaseEppoClientTest.class); |
| 21 | + |
| 22 | + private static final String DUMMY_FLAG_API_KEY = "dummy-flags-api-key"; // Will load flags-v1 |
| 23 | + private static final String TEST_HOST = |
| 24 | + "https://us-central1-eppo-qa.cloudfunctions.net/serveGitHubRacTestFile"; |
| 25 | + |
| 26 | + private static BaseEppoClient eppoClient; |
| 27 | + private static final AssignmentLogger noOpAssignmentLogger = |
| 28 | + new AssignmentLogger() { |
| 29 | + @Override |
| 30 | + public void logAssignment(Assignment assignment) { |
| 31 | + /* no-op */ |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + @BeforeAll |
| 36 | + public static void initClient() { |
| 37 | + eppoClient = |
| 38 | + new BaseEppoClient( |
| 39 | + DUMMY_FLAG_API_KEY, |
| 40 | + "java", |
| 41 | + "3.0.0", |
| 42 | + TEST_HOST, |
| 43 | + noOpAssignmentLogger, |
| 44 | + null, |
| 45 | + false, |
| 46 | + false); |
| 47 | + |
| 48 | + eppoClient.loadConfiguration(); |
| 49 | + |
| 50 | + log.info("Test client initialized"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testGetStringAssignmentPerformance() { |
| 55 | + Map<String, AtomicInteger> variationCounts = new HashMap<>(); |
| 56 | + Attributes subjectAttributes = new Attributes(); |
| 57 | + subjectAttributes.put("country", "FR"); |
| 58 | + |
| 59 | + ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); |
| 60 | + long startTime = threadBean.getCurrentThreadCpuTime(); |
| 61 | + |
| 62 | + int numIterations = 10000; |
| 63 | + |
| 64 | + for (int i = 0; i < numIterations; i++) { |
| 65 | + String subjectKey = "subject" + i; |
| 66 | + String assignedVariation = |
| 67 | + eppoClient.getStringAssignment( |
| 68 | + "new-user-onboarding", subjectKey, subjectAttributes, "default"); |
| 69 | + AtomicInteger existingCount = |
| 70 | + variationCounts.computeIfAbsent(assignedVariation, k -> new AtomicInteger(0)); |
| 71 | + existingCount.incrementAndGet(); |
| 72 | + } |
| 73 | + long endTime = threadBean.getCurrentThreadCpuTime(); |
| 74 | + long elapsedTime = endTime - startTime; |
| 75 | + |
| 76 | + log.info("Assignment counts: {}", variationCounts); |
| 77 | + log.info("CPU Time: {}", elapsedTime); |
| 78 | + |
| 79 | + // Assert assignments shook out as expected based the shard ranges |
| 80 | + assertEquals(4, variationCounts.keySet().size()); |
| 81 | + // Expect ~40% default |
| 82 | + assertEquals(0.40, variationCounts.get("default").doubleValue() / numIterations, 0.02); |
| 83 | + // Expect ~30% control (50% of 60%) |
| 84 | + assertEquals(0.30, variationCounts.get("control").doubleValue() / numIterations, 0.02); |
| 85 | + // Expect ~18% red (30% of 60%) |
| 86 | + assertEquals(0.18, variationCounts.get("red").doubleValue() / numIterations, 0.02); |
| 87 | + // Expect ~12% yellow (20% of 60%) |
| 88 | + assertEquals(0.12, variationCounts.get("yellow").doubleValue() / numIterations, 0.02); |
| 89 | + |
| 90 | + // Seeing ~48,000,000 - ~54,000,000 for 10k iterations on a M2 Macbook Pro; let's fail if more |
| 91 | + // than 150,000,000; giving a generous allowance for slower systems (like GitHub) but will still |
| 92 | + // catch if things slow down considerably |
| 93 | + long maxAllowedTime = 15000 * numIterations; |
| 94 | + assertTrue( |
| 95 | + elapsedTime < maxAllowedTime, |
| 96 | + "Cpu time of " + elapsedTime + " is more than the " + maxAllowedTime + " allowed"); |
| 97 | + } |
| 98 | +} |
0 commit comments