|
| 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.Segment; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import okhttp3.Interceptor; |
| 12 | +import okhttp3.MediaType; |
| 13 | +import okhttp3.Protocol; |
| 14 | +import okhttp3.Response; |
| 15 | +import okhttp3.ResponseBody; |
| 16 | +import org.openjdk.jmh.annotations.Benchmark; |
| 17 | +import org.openjdk.jmh.annotations.Param; |
| 18 | +import org.openjdk.jmh.annotations.Scope; |
| 19 | +import org.openjdk.jmh.annotations.Setup; |
| 20 | +import org.openjdk.jmh.annotations.State; |
| 21 | +import org.openjdk.jmh.annotations.TearDown; |
| 22 | + |
| 23 | +/** |
| 24 | + * Benchmarks a locally evaluating SDK client against environment documents tiered by size. |
| 25 | + */ |
| 26 | +@State(Scope.Benchmark) |
| 27 | +public class SdkBenchmark { |
| 28 | + private static final MediaType JSON = MediaType.get("application/json"); |
| 29 | + private static final String IDENTITY_OVERRIDES_IDENTIFIER = "identity-override-1"; |
| 30 | + private static final String IDENTIFIER = "user-1"; |
| 31 | + private static final Map<String, Object> TRAITS = Map.of( |
| 32 | + "plan", "plan-7", |
| 33 | + "age", 30, |
| 34 | + "country", "GB", |
| 35 | + "version", "2.5.1", |
| 36 | + "email", "jane@example.com", |
| 37 | + "user_id", 12, |
| 38 | + "beta", true, |
| 39 | + "score", 95, |
| 40 | + "tier", "pro", |
| 41 | + "device", "iPhone 15"); |
| 42 | + |
| 43 | + @Param({"small", "medium", "large"}) |
| 44 | + public String environment; |
| 45 | + |
| 46 | + private FlagsmithClient client; |
| 47 | + |
| 48 | + /** |
| 49 | + * Build a locally evaluating client serving the environment document from memory. |
| 50 | + */ |
| 51 | + @Setup |
| 52 | + public void buildClient() throws IOException { |
| 53 | + byte[] environmentDocument; |
| 54 | + try (InputStream stream = getClass().getResourceAsStream( |
| 55 | + "/environments/" + environment + ".json")) { |
| 56 | + environmentDocument = stream.readAllBytes(); |
| 57 | + } |
| 58 | + |
| 59 | + client = FlagsmithClient.newBuilder() |
| 60 | + .setApiKey("ser.benchmark") |
| 61 | + .withConfiguration(FlagsmithConfig.newBuilder() |
| 62 | + .baseUri("http://flagsmith.benchmark/api/v1/") |
| 63 | + .addHttpInterceptor(serveEnvironmentDocument(environmentDocument)) |
| 64 | + .withLocalEvaluation(true) |
| 65 | + .withEnvironmentRefreshIntervalSeconds(86400) // Refreshed by benchmarks only |
| 66 | + .build()) |
| 67 | + .build(); |
| 68 | + } |
| 69 | + |
| 70 | + @TearDown |
| 71 | + public void closeClient() { |
| 72 | + client.close(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get a feature value from the environment flags. |
| 77 | + */ |
| 78 | + @Benchmark |
| 79 | + public Object getEnvironmentFlags() throws FlagsmithClientError { |
| 80 | + return client.getEnvironmentFlags().getFeatureValue("feature_1"); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Check a feature for an identity with identity overrides. |
| 85 | + */ |
| 86 | + @Benchmark |
| 87 | + public boolean getIdentityFlagsWithIdentityOverrides() throws FlagsmithClientError { |
| 88 | + return client.getIdentityFlags(IDENTITY_OVERRIDES_IDENTIFIER).isFeatureEnabled("feature_8"); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get a feature value for an identity without traits. |
| 93 | + */ |
| 94 | + @Benchmark |
| 95 | + public Object getIdentityFlagsNoTraits() throws FlagsmithClientError { |
| 96 | + return client.getIdentityFlags(IDENTIFIER).getFeatureValue("feature_1"); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get a feature value for an identity with traits. |
| 101 | + */ |
| 102 | + @Benchmark |
| 103 | + public Object getIdentityFlagsWithTraits() throws FlagsmithClientError { |
| 104 | + return client.getIdentityFlags(IDENTIFIER, TRAITS).getFeatureValue("feature_1"); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * List the segments of an identity with traits. |
| 109 | + */ |
| 110 | + @Benchmark |
| 111 | + public List<Segment> getIdentitySegmentsWithTraits() throws FlagsmithClientError { |
| 112 | + return client.getIdentitySegments(IDENTIFIER, TRAITS); |
| 113 | + } |
| 114 | + |
| 115 | + private static Interceptor serveEnvironmentDocument(byte[] environmentDocument) { |
| 116 | + return chain -> new Response.Builder() |
| 117 | + .request(chain.request()) |
| 118 | + .protocol(Protocol.HTTP_1_1) |
| 119 | + .code(200) |
| 120 | + .message("OK") |
| 121 | + .body(ResponseBody.create(environmentDocument, JSON)) |
| 122 | + .build(); |
| 123 | + } |
| 124 | +} |
0 commit comments