55import com .launchdarkly .client .EventProcessor ;
66import com .launchdarkly .client .EventProcessorInternals ;
77import com .launchdarkly .client .LDConfig ;
8- import com .launchdarkly .client .LDUser ;
98import com .launchdarkly .client .interfaces .EventSender ;
109import com .launchdarkly .client .interfaces .EventSenderFactory ;
1110import com .launchdarkly .client .interfaces .HttpConfiguration ;
1716
1817import java .io .IOException ;
1918import java .net .URI ;
19+ import java .util .ArrayList ;
20+ import java .util .List ;
2021import java .util .Random ;
2122
23+ import static com .launchdarkly .sdk .server .TestValues .BASIC_USER ;
24+ import static com .launchdarkly .sdk .server .TestValues .CUSTOM_EVENT ;
25+ import static com .launchdarkly .sdk .server .TestValues .TEST_EVENTS_COUNT ;
26+
2227public class EventProcessorBenchmarks {
2328 private static final int EVENT_BUFFER_SIZE = 1000 ;
2429 private static final int FLAG_COUNT = 10 ;
@@ -30,7 +35,8 @@ public static class BenchmarkInputs {
3035 // Initialization of the things in BenchmarkInputs does not count as part of a benchmark.
3136 final EventProcessor eventProcessor ;
3237 final EventSender eventSender ;
33- final LDUser basicUser ;
38+ final List <Event .FeatureRequest > featureRequestEventsWithoutTracking = new ArrayList <>();
39+ final List <Event .FeatureRequest > featureRequestEventsWithTracking = new ArrayList <>();
3440 final Random random ;
3541
3642 public BenchmarkInputs () {
@@ -44,9 +50,30 @@ public BenchmarkInputs() {
4450 .eventSender (new MockEventSenderFactory ())
4551 .createEventProcessor (TestValues .SDK_KEY , new LDConfig .Builder ().build ());
4652
47- basicUser = new LDUser ("userkey" );
48-
4953 random = new Random ();
54+
55+ for (int i = 0 ; i < TEST_EVENTS_COUNT ; i ++) {
56+ String flagKey = "flag" + random .nextInt (FLAG_COUNT );
57+ int version = random .nextInt (FLAG_VERSIONS ) + 1 ;
58+ int variation = random .nextInt (FLAG_VARIATIONS );
59+ for (boolean trackEvents : new boolean [] { false , true }) {
60+ Event .FeatureRequest event = new Event .FeatureRequest (
61+ System .currentTimeMillis (),
62+ flagKey ,
63+ BASIC_USER ,
64+ version ,
65+ variation ,
66+ LDValue .of (variation ),
67+ LDValue .ofNull (),
68+ null ,
69+ null ,
70+ trackEvents ,
71+ null ,
72+ false
73+ );
74+ (trackEvents ? featureRequestEventsWithTracking : featureRequestEventsWithoutTracking ).add (event );
75+ }
76+ }
5077 }
5178
5279 public String randomFlagKey () {
@@ -64,22 +91,7 @@ public int randomFlagVariation() {
6491
6592 @ Benchmark
6693 public void summarizeFeatureRequestEvents (BenchmarkInputs inputs ) throws Exception {
67- for (int i = 0 ; i < 1000 ; i ++) {
68- int variation = inputs .randomFlagVariation ();
69- Event .FeatureRequest event = new Event .FeatureRequest (
70- System .currentTimeMillis (),
71- inputs .randomFlagKey (),
72- inputs .basicUser ,
73- inputs .randomFlagVersion (),
74- variation ,
75- LDValue .of (variation ),
76- LDValue .ofNull (),
77- null ,
78- null ,
79- false , // trackEvents == false: only summary counts are generated
80- null ,
81- false
82- );
94+ for (Event .FeatureRequest event : inputs .featureRequestEventsWithoutTracking ) {
8395 inputs .eventProcessor .sendEvent (event );
8496 }
8597 inputs .eventProcessor .flush ();
@@ -88,22 +100,7 @@ public void summarizeFeatureRequestEvents(BenchmarkInputs inputs) throws Excepti
88100
89101 @ Benchmark
90102 public void featureRequestEventsWithFullTracking (BenchmarkInputs inputs ) throws Exception {
91- for (int i = 0 ; i < 1000 ; i ++) {
92- int variation = inputs .randomFlagVariation ();
93- Event .FeatureRequest event = new Event .FeatureRequest (
94- System .currentTimeMillis (),
95- inputs .randomFlagKey (),
96- inputs .basicUser ,
97- inputs .randomFlagVersion (),
98- variation ,
99- LDValue .of (variation ),
100- LDValue .ofNull (),
101- null ,
102- null ,
103- true , // trackEvents == true: the full events are included in the output
104- null ,
105- false
106- );
103+ for (Event .FeatureRequest event : inputs .featureRequestEventsWithTracking ) {
107104 inputs .eventProcessor .sendEvent (event );
108105 }
109106 inputs .eventProcessor .flush ();
@@ -112,16 +109,8 @@ public void featureRequestEventsWithFullTracking(BenchmarkInputs inputs) throws
112109
113110 @ Benchmark
114111 public void customEvents (BenchmarkInputs inputs ) throws Exception {
115- LDValue data = LDValue .of ("data" );
116- for (int i = 0 ; i < 1000 ; i ++) {
117- Event .Custom event = new Event .Custom (
118- System .currentTimeMillis (),
119- "event-key" ,
120- inputs .basicUser ,
121- data ,
122- null
123- );
124- inputs .eventProcessor .sendEvent (event );;
112+ for (int i = 0 ; i < TEST_EVENTS_COUNT ; i ++) {
113+ inputs .eventProcessor .sendEvent (CUSTOM_EVENT );
125114 }
126115 inputs .eventProcessor .flush ();
127116 EventProcessorInternals .waitUntilInactive (inputs .eventProcessor );
0 commit comments