Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: kotlinx serialization for GraphQLServerRequest #1937

Merged

Conversation

samuelAndalon
Copy link
Contributor

@samuelAndalon samuelAndalon commented Mar 11, 2024

📝 Description

Switch from jackson to kotlinx.serialization for serialization/deserialization of GraphQLServerRequest types.

After running benchmarks we where able to identify that deserializing GraphQLServerRequest with kotlinx.serialization is quite faster than doing it with jackson, the reason ? possibly because jackson relies on reflections to identify deserialization process.

On the other hand, serialization/deserialization of GraphQLServerReponse type is still faster if done with jackson, possibly because of how kotlinx.serialization library was designed and the poor support for serializing Any type:
Kotlin/kotlinx.serialization#296, which causes a lot of memory comsumption.

As part of this PR also including the benchmarks. For that, i created a separate set of types that are marked with both jackson and kotlinx.serialization annotations.

Benchmarks results:
Executed on a MacBookPro 2.6 GHz 6-Core Intel Core i7.

GraphQLServerRequest Deserialization

GraphQLBatchRequest
4 batched operations, each operation is aprox: 30kb
image

GraphQLRequest
image

GraphQLServerResponse Serialization

GraphQLBatchResponse
image

GraphQLResponse
image

@samuelAndalon samuelAndalon enabled auto-merge (squash) March 12, 2024 07:23
@samuelAndalon
Copy link
Contributor Author

samuelAndalon commented Mar 12, 2024

Important to mention that Ktor server will still use jackson as engine for deserialization:
https://github.com/ExpediaGroup/graphql-kotlin/blob/master/servers/graphql-kotlin-ktor-server/src/main/kotlin/com/expediagroup/graphql/server/ktor/GraphQLRoutes.kt#L70

Spring webflux server will use kotlinx as if server detects both set of annotations jackson, kotlinx, will use kotlinx.

In a separate PR we can make this configurable.

@samuelAndalon samuelAndalon merged commit 06435c9 into ExpediaGroup:master Mar 14, 2024
13 checks passed
@samuelAndalon samuelAndalon deleted the feat/serialization-benchmarks branch March 14, 2024 22:49
samuelAndalon added a commit that referenced this pull request Apr 1, 2024
Switch from `jackson` to `kotlinx.serialization` for
serialization/deserialization of `GraphQLServerRequest` types.

After running benchmarks we where able to identify that deserializing
`GraphQLServerRequest` with `kotlinx.serialization` is quite faster than
doing it with `jackson`, the reason ? possibly because jackson relies on
reflections to identify deserialization process.

On the other hand, serialization/deserialization of
`GraphQLServerReponse` type is still faster if done with `jackson`,
possibly because of how `kotlinx.serialization` library was designed and
the poor support for serializing `Any` type:
Kotlin/kotlinx.serialization#296, which causes
a lot of memory comsumption.

As part of this PR also including the benchmarks. For that, i created a
separate set of types that are marked with both `jackson` and
`kotlinx.serialization` annotations.

Benchmarks results:
Executed on a MacBookPro 2.6 GHz 6-Core Intel Core i7.

`GraphQLBatchRequest`
4 batched operations, each operation is aprox: 30kb
<img width="1260" alt="image"
src="https://github.com/ExpediaGroup/graphql-kotlin/assets/6611331/06e5b218-a35e-4baa-a25e-2be1b3c27a95">

`GraphQLRequest`
<img width="1231" alt="image"
src="https://github.com/ExpediaGroup/graphql-kotlin/assets/6611331/e5ecba01-fd41-4872-b3e8-5519414cc918">

`GraphQLBatchResponse`
<img width="1240" alt="image"
src="https://github.com/ExpediaGroup/graphql-kotlin/assets/6611331/ee84bfa4-d7d1-46b4-b4a8-b3c220998a03">

`GraphQLResponse`
<img width="1197" alt="image"
src="https://github.com/ExpediaGroup/graphql-kotlin/assets/6611331/c217e05f-45fc-460e-a059-7667975ee49f">
samuelAndalon added a commit that referenced this pull request Apr 1, 2024
…1937) (#1944)

Switch from `jackson` to `kotlinx.serialization` for
serialization/deserialization of `GraphQLServerRequest` types.

After running benchmarks we where able to identify that deserializing
`GraphQLServerRequest` with `kotlinx.serialization` is quite faster than
doing it with `jackson`, the reason ? possibly because jackson relies on
reflections to identify deserialization process.

On the other hand, serialization/deserialization of
`GraphQLServerReponse` type is still faster if done with `jackson`,
possibly because of how `kotlinx.serialization` library was designed and
the poor support for serializing `Any` type:
Kotlin/kotlinx.serialization#296, which causes
a lot of memory comsumption.

As part of this PR also including the benchmarks. For that, i created a
separate set of types that are marked with both `jackson` and
`kotlinx.serialization` annotations.

Benchmarks results:
Executed on a MacBookPro 2.6 GHz 6-Core Intel Core i7.

`GraphQLBatchRequest`
4 batched operations, each operation is aprox: 30kb <img width="1260"
alt="image"

src="https://github.com/ExpediaGroup/graphql-kotlin/assets/6611331/06e5b218-a35e-4baa-a25e-2be1b3c27a95">

`GraphQLRequest`
<img width="1231" alt="image"

src="https://github.com/ExpediaGroup/graphql-kotlin/assets/6611331/e5ecba01-fd41-4872-b3e8-5519414cc918">

`GraphQLBatchResponse`
<img width="1240" alt="image"

src="https://github.com/ExpediaGroup/graphql-kotlin/assets/6611331/ee84bfa4-d7d1-46b4-b4a8-b3c220998a03">

`GraphQLResponse`
<img width="1197" alt="image"

src="https://github.com/ExpediaGroup/graphql-kotlin/assets/6611331/c217e05f-45fc-460e-a059-7667975ee49f">

### 📝 Description


### 🔗 Related Issues
samuelAndalon added a commit that referenced this pull request Oct 3, 2024
…ion of GraphQLRequest (#2040)

### 📝 Description
[the previous integration of
kotlinx-serialization](#1937)
represented a performance improvement when deserializing requests,
however, responses are still being deserialized with jackson, recently
we found that fastjson2 is the [fastest serialization library for the
jvm](https://github.com/fabienrenaud/java-json-benchmark).

this PR adds an opt-in support for fastjson2 which provides an easy
integration with springboot codecs, the serializer relies on the same
jackson annotation, and just had to write a deserializer because of the
polymorphic nature of GraphQLRequest and GraphQLReponse

####  benchmark results

GraphQLRequest deserialization
<img width="1156" alt="image"
src="https://github.com/user-attachments/assets/8a1e56a2-0d6f-4118-904a-a5d584caac19">

GraphQLResponse serialization
<img width="1161" alt="image"
src="https://github.com/user-attachments/assets/cfa6c843-50de-42e2-8f8e-7722c722b048">

---------

Co-authored-by: Samuel Vazquez <[email protected]>
samuelAndalon added a commit that referenced this pull request Oct 4, 2024
…ion of GraphQLRequest (#2040)

[the previous integration of
kotlinx-serialization](#1937)
represented a performance improvement when deserializing requests,
however, responses are still being deserialized with jackson, recently
we found that fastjson2 is the [fastest serialization library for the
jvm](https://github.com/fabienrenaud/java-json-benchmark).

this PR adds an opt-in support for fastjson2 which provides an easy
integration with springboot codecs, the serializer relies on the same
jackson annotation, and just had to write a deserializer because of the
polymorphic nature of GraphQLRequest and GraphQLReponse

GraphQLRequest deserialization
<img width="1156" alt="image"
src="https://github.com/user-attachments/assets/8a1e56a2-0d6f-4118-904a-a5d584caac19">

GraphQLResponse serialization
<img width="1161" alt="image"
src="https://github.com/user-attachments/assets/cfa6c843-50de-42e2-8f8e-7722c722b048">

---------

Co-authored-by: Samuel Vazquez <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants