Skip to content

Commit 5094b76

Browse files
committed
benchmark the sdk interface
1 parent 35f7576 commit 5094b76

6 files changed

Lines changed: 76276 additions & 67 deletions

File tree

‎pom.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,20 @@
420420
</sources>
421421
</configuration>
422422
</execution>
423+
<execution>
424+
<id>add-jmh-resources</id>
425+
<phase>generate-resources</phase>
426+
<goals>
427+
<goal>add-resource</goal>
428+
</goals>
429+
<configuration>
430+
<resources>
431+
<resource>
432+
<directory>${project.basedir}/src/jmh/resources</directory>
433+
</resource>
434+
</resources>
435+
</configuration>
436+
</execution>
423437
</executions>
424438
</plugin>
425439
<plugin>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.flagsmith;
2+
3+
import com.flagsmith.config.FlagsmithConfig;
4+
import com.flagsmith.exceptions.FlagsmithClientError;
5+
import com.flagsmith.flagengine.EvaluationContext;
6+
import com.flagsmith.models.Flags;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.util.Map;
10+
import okhttp3.Interceptor;
11+
import okhttp3.MediaType;
12+
import okhttp3.Protocol;
13+
import okhttp3.Response;
14+
import okhttp3.ResponseBody;
15+
import org.openjdk.jmh.annotations.Benchmark;
16+
import org.openjdk.jmh.annotations.Param;
17+
import org.openjdk.jmh.annotations.Scope;
18+
import org.openjdk.jmh.annotations.Setup;
19+
import org.openjdk.jmh.annotations.State;
20+
import org.openjdk.jmh.annotations.TearDown;
21+
22+
/**
23+
* Benchmarks a locally evaluating SDK client against environment documents tiered by size.
24+
*/
25+
@State(Scope.Benchmark)
26+
public class SDKBenchmark {
27+
private static final MediaType JSON = MediaType.get("application/json");
28+
private static final Map<String, Object> SEGMENT_TRAITS = Map.of(
29+
"plan", "plan-7",
30+
"age", 30,
31+
"country", "GB",
32+
"version", "2.5.1",
33+
"email", "jane@example.com",
34+
"user_id", 12,
35+
"beta", true,
36+
"score", 95,
37+
"tier", "pro",
38+
"device", "iPhone 15");
39+
40+
@Param({"small", "medium", "large"})
41+
public String environment;
42+
43+
private FlagsmithClient client;
44+
45+
/**
46+
* Build a locally evaluating client serving the environment document from memory.
47+
*/
48+
@Setup
49+
public void buildClient() throws IOException {
50+
byte[] environmentDocument;
51+
try (InputStream stream = getClass().getResourceAsStream(
52+
"/environments/" + environment + ".json")) {
53+
environmentDocument = stream.readAllBytes();
54+
}
55+
56+
client = FlagsmithClient.newBuilder()
57+
.setApiKey("ser.benchmark")
58+
.withConfiguration(FlagsmithConfig.newBuilder()
59+
.baseUri("http://flagsmith.benchmark/api/v1/")
60+
.addHttpInterceptor(serveEnvironmentDocument(environmentDocument))
61+
.withLocalEvaluation(true)
62+
.withEnvironmentRefreshIntervalSeconds(86400) // Refreshed by benchmarks only
63+
.build())
64+
.build();
65+
}
66+
67+
@TearDown
68+
public void closeClient() {
69+
client.close();
70+
}
71+
72+
/**
73+
* Fetch, parse and map the environment document, replacing the client's cached copy.
74+
*/
75+
@Benchmark
76+
public EvaluationContext updateEnvironment() {
77+
client.updateEnvironment();
78+
return client.getEvaluationContext();
79+
}
80+
81+
/**
82+
* Evaluate environment flags from the cached environment document.
83+
*/
84+
@Benchmark
85+
public Flags getEnvironmentFlags() throws FlagsmithClientError {
86+
return client.getEnvironmentFlags();
87+
}
88+
89+
/**
90+
* Evaluate flags for an identity matched by an identity overrides pseudo-segment.
91+
*/
92+
@Benchmark
93+
public Flags getIdentityFlagsWithIdentityOverrides() throws FlagsmithClientError {
94+
return client.getIdentityFlags("identity-override-1");
95+
}
96+
97+
/**
98+
* Evaluate flags for an identity whose traits match segments.
99+
*/
100+
@Benchmark
101+
public Flags getIdentityFlagsWithSegmentTraits() throws FlagsmithClientError {
102+
return client.getIdentityFlags("user-1", SEGMENT_TRAITS);
103+
}
104+
105+
private static Interceptor serveEnvironmentDocument(byte[] environmentDocument) {
106+
return chain -> new Response.Builder()
107+
.request(chain.request())
108+
.protocol(Protocol.HTTP_1_1)
109+
.code(200)
110+
.message("OK")
111+
.body(ResponseBody.create(environmentDocument, JSON))
112+
.build();
113+
}
114+
}

‎src/jmh/java/com/flagsmith/flagengine/EngineBenchmark.java‎

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)