|
| 1 | +package io.harness.cf.client.api; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import io.harness.cf.client.api.testutils.DummyConnector; |
| 6 | +import io.harness.cf.client.dto.Target; |
| 7 | +import io.harness.cf.model.FeatureConfig; |
| 8 | +import io.harness.cf.model.Variation; |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.concurrent.*; |
| 15 | +import java.util.concurrent.atomic.LongAdder; |
| 16 | +import java.util.stream.Stream; |
| 17 | +import lombok.AllArgsConstructor; |
| 18 | +import lombok.EqualsAndHashCode; |
| 19 | +import lombok.NonNull; |
| 20 | +import org.junit.jupiter.api.Disabled; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +/* |
| 24 | + * This stress test is disabled by default and needs to be run manually. Make sure to set TARGETS_FILE and |
| 25 | + * FLAGS_FILE from the production files in ff-sdk-testgrid or create files with at least 18K unique targets |
| 26 | + * and 400 unique flags if you want to run this test. |
| 27 | + * |
| 28 | + * If running in IntelliJ you can attach JFR from a terminal after starting the test using something like: |
| 29 | + * |
| 30 | + * jcmd $(jcmd | grep -i junit | cut -f 1 -d " ") JFR.start duration=10m filename=flight.jfr |
| 31 | + */ |
| 32 | +@Disabled |
| 33 | +class MetricsProcessorStressTest { |
| 34 | + |
| 35 | + boolean RUN_PERPETUALLY = false; // useful for longer tests, note if true, test will never exit |
| 36 | + boolean DUMP_POSTED_METRICS = false; |
| 37 | + int NUM_THREADS = 32; |
| 38 | + int VARIATION_COUNT = 4; |
| 39 | + String TARGETS_FILE = "/tmp/prod2_targets.txt"; |
| 40 | + String FLAGS_FILE = "/tmp/flags.txt"; |
| 41 | + |
| 42 | + @Test |
| 43 | + void testRegisterEvaluationContention() throws Exception { |
| 44 | + |
| 45 | + final DummyConnector dummyConnector = new DummyConnector(DUMP_POSTED_METRICS); |
| 46 | + |
| 47 | + final MetricsProcessor metricsProcessor = |
| 48 | + new MetricsProcessor( |
| 49 | + dummyConnector, |
| 50 | + BaseConfig.builder() |
| 51 | + // .globalTargetEnabled(false) |
| 52 | + .build(), |
| 53 | + new DummyMetricsCallback()); |
| 54 | + |
| 55 | + metricsProcessor.start(); |
| 56 | + |
| 57 | + System.out.println("Loading..."); |
| 58 | + |
| 59 | + final List<String> targets = loadFile(TARGETS_FILE); |
| 60 | + final List<String> flags = loadFile(FLAGS_FILE); |
| 61 | + |
| 62 | + System.out.printf("Loaded %d targets\n", targets.size()); |
| 63 | + System.out.printf("Loaded %d flags\n", flags.size()); |
| 64 | + |
| 65 | + final ConcurrentLinkedQueue<TargetAndFlag> targetAndFlags = |
| 66 | + createFlagTargetVariationPermutations(flags, targets); |
| 67 | + |
| 68 | + System.out.printf("Starting...processing %d flags/targets\n", targetAndFlags.size()); |
| 69 | + |
| 70 | + final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS); |
| 71 | + final LongAdder totalProcessed = new LongAdder(); |
| 72 | + |
| 73 | + for (int i = 0; i < NUM_THREADS; i++) { |
| 74 | + final int threadNum = i; |
| 75 | + executor.submit( |
| 76 | + () -> { |
| 77 | + Thread.currentThread().setName("THREAD" + threadNum); |
| 78 | + System.out.println("start thread " + Thread.currentThread().getName()); |
| 79 | + |
| 80 | + TargetAndFlag next; |
| 81 | + |
| 82 | + while ((next = targetAndFlags.poll()) != null) { |
| 83 | + final Target target = Target.builder().identifier(next.target).build(); |
| 84 | + final FeatureConfig feature = FeatureConfig.builder().feature(next.flag).build(); |
| 85 | + final Variation variation = |
| 86 | + Variation.builder() |
| 87 | + .identifier(next.variation) |
| 88 | + .value(next.variation + "Value") |
| 89 | + .build(); |
| 90 | + |
| 91 | + metricsProcessor.registerEvaluation(target, feature.getFeature(), variation); |
| 92 | + |
| 93 | + if (RUN_PERPETUALLY) { |
| 94 | + targetAndFlags.add(next); |
| 95 | + } |
| 96 | + |
| 97 | + totalProcessed.increment(); |
| 98 | + } |
| 99 | + |
| 100 | + System.out.printf("thread %s finished\n", Thread.currentThread().getName()); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + while (targetAndFlags.size() > 0) { |
| 105 | + System.out.printf( |
| 106 | + "target/flags/variations processed %d, map size %d, pending evaluations=%d \n", |
| 107 | + totalProcessed.sum(), |
| 108 | + metricsProcessor.getQueueSize(), |
| 109 | + metricsProcessor.getPendingMetricsToBeSent()); |
| 110 | + |
| 111 | + Thread.sleep(1000); |
| 112 | + } |
| 113 | + |
| 114 | + metricsProcessor.runOneIteration(); |
| 115 | + |
| 116 | + assertEquals(flags.size() * targets.size() * VARIATION_COUNT, (int) totalProcessed.sum()); |
| 117 | + assertEquals( |
| 118 | + flags.size() * targets.size() * VARIATION_COUNT, |
| 119 | + dummyConnector.getTotalMetricEvaluations()); |
| 120 | + } |
| 121 | + |
| 122 | + private List<String> loadFile(String filename) throws IOException { |
| 123 | + final List<String> map = new ArrayList<>(); |
| 124 | + try (Stream<String> stream = Files.lines(Paths.get(filename))) { |
| 125 | + stream.forEach(map::add); |
| 126 | + } |
| 127 | + return map; |
| 128 | + } |
| 129 | + |
| 130 | + private ConcurrentLinkedQueue<TargetAndFlag> createFlagTargetVariationPermutations( |
| 131 | + List<String> flags, List<String> targets) { |
| 132 | + final ConcurrentLinkedQueue<TargetAndFlag> targetAndFlags = new ConcurrentLinkedQueue<>(); |
| 133 | + |
| 134 | + for (String flag : flags) { |
| 135 | + for (String target : targets) { |
| 136 | + for (int v = 0; v < VARIATION_COUNT; v++) { // variations per flag/target combination |
| 137 | + targetAndFlags.add(new TargetAndFlag(target, flag, "variation" + v)); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return targetAndFlags; |
| 142 | + } |
| 143 | + |
| 144 | + @EqualsAndHashCode |
| 145 | + @AllArgsConstructor |
| 146 | + static class TargetAndFlag { |
| 147 | + String target, flag, variation; |
| 148 | + } |
| 149 | + |
| 150 | + static class DummyMetricsCallback implements MetricsCallback { |
| 151 | + @Override |
| 152 | + public void onMetricsReady() { |
| 153 | + System.out.println("onMetricsReady"); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void onMetricsError(@NonNull String error) { |
| 158 | + System.out.println("onMetricsError " + error); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void onMetricsFailure() { |
| 163 | + System.out.println("onMetricsFailure"); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments