growthbook / growthbook-sdk-java

The Java SDK for GrowthBook (JVM, Android)
https://docs.growthbook.io/lib/java
MIT License
7 stars 10 forks source link

Add support for setting JSON Feature Object in GBContext builder #77

Closed gaurav-rana closed 6 hours ago

gaurav-rana commented 6 days ago

The current GBContext implementation is suboptimal for java backend integrations. We have to create a new GBContext for each request, and because the CustomGbContextBuilder only supports StringType featureJson, we have to pass featureJson as string and it parses the JSON (converting from String to JSONType) for each and every request. This can be avoided by letting the application maintain JSON and set JSON directly.

Flame graph with the current GBContext:

image

As we can see, around 42% of total CPU is spent inside parsing this JSON for each and every request, this overhead can be clearly avoided by letting the client set the JSON directly instead of string.

I've added the support in this PR https://github.com/growthbook/growthbook-sdk-java/pull/76

Bohdan-Kim commented 5 days ago

Passing features not through String type but through JsonObject type was suggested in the PR. We can't remove old way through String type because it leads to rewriting all tests, but to add also passing through JsonObject type is OK. So, JsonObject type from Gson library was added to public class GbContext of the GrowthBook Java SDK. It can lead to forcing users of the SDK to include Gson dependency in their apps. In order not to include Gson in target builds if features are passed through String type compileOnly can be used:

compileOnly 'com.google.code.gson:gson:2.9.1'
Bohdan-Kim commented 5 days ago

The pull request is not merged in main branch yet but the changes can be accessed using this:

    <dependency>
        <groupId>com.github.growthbook</groupId>
        <artifactId>growthbook-sdk-java</artifactId>
        <version>release~0.9.7-SNAPSHOT</version>
    </dependency>
vazarkevych commented 5 days ago

Hi, @gaurav-rana. We have added the necessary changes and successfully merged your pull request.

gaurav-rana commented 5 days ago

Great Thanks! I'm waiting for the release 0.9.7 before i upgrade the SDK in my app.

Bohdan-Kim commented 6 hours ago

0.9.7 version is available on JitPack

gaurav-rana commented 6 hours ago

Thanks, closing this as completed.