|
| 1 | +package com.launchdarkly.sdk.server; |
| 2 | + |
| 3 | +import com.launchdarkly.sdk.LDValue; |
| 4 | +import com.launchdarkly.sdk.server.interfaces.Event; |
| 5 | +import com.launchdarkly.sdk.server.interfaces.EventProcessor; |
| 6 | +import com.launchdarkly.sdk.server.interfaces.EventSender; |
| 7 | +import com.launchdarkly.sdk.server.interfaces.EventSenderFactory; |
| 8 | +import com.launchdarkly.sdk.server.interfaces.HttpConfiguration; |
| 9 | + |
| 10 | +import org.openjdk.jmh.annotations.Benchmark; |
| 11 | +import org.openjdk.jmh.annotations.Scope; |
| 12 | +import org.openjdk.jmh.annotations.State; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.net.URI; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Random; |
| 19 | +import java.util.concurrent.CountDownLatch; |
| 20 | + |
| 21 | +import static com.launchdarkly.sdk.server.TestValues.BASIC_USER; |
| 22 | +import static com.launchdarkly.sdk.server.TestValues.CUSTOM_EVENT; |
| 23 | +import static com.launchdarkly.sdk.server.TestValues.TEST_EVENTS_COUNT; |
| 24 | + |
| 25 | +public class EventProcessorBenchmarks { |
| 26 | + private static final int EVENT_BUFFER_SIZE = 1000; |
| 27 | + private static final int FLAG_COUNT = 10; |
| 28 | + private static final int FLAG_VERSIONS = 3; |
| 29 | + private static final int FLAG_VARIATIONS = 2; |
| 30 | + |
| 31 | + @State(Scope.Thread) |
| 32 | + public static class BenchmarkInputs { |
| 33 | + // Initialization of the things in BenchmarkInputs does not count as part of a benchmark. |
| 34 | + final EventProcessor eventProcessor; |
| 35 | + final MockEventSender eventSender; |
| 36 | + final List<Event.FeatureRequest> featureRequestEventsWithoutTracking = new ArrayList<>(); |
| 37 | + final List<Event.FeatureRequest> featureRequestEventsWithTracking = new ArrayList<>(); |
| 38 | + final Random random; |
| 39 | + |
| 40 | + public BenchmarkInputs() { |
| 41 | + // MockEventSender does no I/O - it discards every event payload. So we are benchmarking |
| 42 | + // all of the event processing steps up to that point, including the formatting of the |
| 43 | + // JSON data in the payload. |
| 44 | + eventSender = new MockEventSender(); |
| 45 | + |
| 46 | + eventProcessor = Components.sendEvents() |
| 47 | + .capacity(EVENT_BUFFER_SIZE) |
| 48 | + .eventSender(new MockEventSenderFactory(eventSender)) |
| 49 | + .createEventProcessor(TestComponents.clientContext(TestValues.SDK_KEY, LDConfig.DEFAULT)); |
| 50 | + |
| 51 | + random = new Random(); |
| 52 | + |
| 53 | + for (int i = 0; i < TEST_EVENTS_COUNT; i++) { |
| 54 | + String flagKey = "flag" + random.nextInt(FLAG_COUNT); |
| 55 | + int version = random.nextInt(FLAG_VERSIONS) + 1; |
| 56 | + int variation = random.nextInt(FLAG_VARIATIONS); |
| 57 | + for (boolean trackEvents: new boolean[] { false, true }) { |
| 58 | + Event.FeatureRequest event = new Event.FeatureRequest( |
| 59 | + System.currentTimeMillis(), |
| 60 | + flagKey, |
| 61 | + BASIC_USER, |
| 62 | + version, |
| 63 | + variation, |
| 64 | + LDValue.of(variation), |
| 65 | + LDValue.ofNull(), |
| 66 | + null, |
| 67 | + null, |
| 68 | + trackEvents, |
| 69 | + 0, |
| 70 | + false |
| 71 | + ); |
| 72 | + (trackEvents ? featureRequestEventsWithTracking : featureRequestEventsWithoutTracking).add(event); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public String randomFlagKey() { |
| 78 | + return "flag" + random.nextInt(FLAG_COUNT); |
| 79 | + } |
| 80 | + |
| 81 | + public int randomFlagVersion() { |
| 82 | + return random.nextInt(FLAG_VERSIONS) + 1; |
| 83 | + } |
| 84 | + |
| 85 | + public int randomFlagVariation() { |
| 86 | + return random.nextInt(FLAG_VARIATIONS); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Benchmark |
| 91 | + public void summarizeFeatureRequestEvents(BenchmarkInputs inputs) throws Exception { |
| 92 | + for (Event.FeatureRequest event: inputs.featureRequestEventsWithoutTracking) { |
| 93 | + inputs.eventProcessor.sendEvent(event); |
| 94 | + } |
| 95 | + inputs.eventProcessor.flush(); |
| 96 | + inputs.eventSender.awaitEvents(); |
| 97 | + } |
| 98 | + |
| 99 | + @Benchmark |
| 100 | + public void featureRequestEventsWithFullTracking(BenchmarkInputs inputs) throws Exception { |
| 101 | + for (Event.FeatureRequest event: inputs.featureRequestEventsWithTracking) { |
| 102 | + inputs.eventProcessor.sendEvent(event); |
| 103 | + } |
| 104 | + inputs.eventProcessor.flush(); |
| 105 | + inputs.eventSender.awaitEvents(); |
| 106 | + } |
| 107 | + |
| 108 | + @Benchmark |
| 109 | + public void customEvents(BenchmarkInputs inputs) throws Exception { |
| 110 | + for (int i = 0; i < TEST_EVENTS_COUNT; i++) { |
| 111 | + inputs.eventProcessor.sendEvent(CUSTOM_EVENT); |
| 112 | + } |
| 113 | + inputs.eventProcessor.flush(); |
| 114 | + inputs.eventSender.awaitEvents(); |
| 115 | + } |
| 116 | + |
| 117 | + private static final class MockEventSender implements EventSender { |
| 118 | + private static final Result RESULT = new Result(true, false, null); |
| 119 | + private static final CountDownLatch counter = new CountDownLatch(1); |
| 120 | + |
| 121 | + @Override |
| 122 | + public void close() throws IOException {} |
| 123 | + |
| 124 | + @Override |
| 125 | + public Result sendEventData(EventDataKind arg0, String arg1, int arg2, URI arg3) { |
| 126 | + counter.countDown(); |
| 127 | + return RESULT; |
| 128 | + } |
| 129 | + |
| 130 | + public void awaitEvents() throws InterruptedException { |
| 131 | + counter.await(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static final class MockEventSenderFactory implements EventSenderFactory { |
| 136 | + private final MockEventSender instance; |
| 137 | + |
| 138 | + MockEventSenderFactory(MockEventSender instance) { |
| 139 | + this.instance = instance; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public EventSender createEventSender(String arg0, HttpConfiguration arg1) { |
| 144 | + return instance; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments