-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fastjson2 for serialization of GraphQLResponse and deserializat…
…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 <samvazquez@expediagroup.com>
- Loading branch information
1 parent
1dbdf28
commit cca3202
Showing
25 changed files
with
320 additions
and
621 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...-kotlin-server/src/benchmarks/kotlin/GraphQLServerRequestBatchDeserializationBenchmark.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2024 Expedia, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expediagroup.graphql.server | ||
|
||
import com.alibaba.fastjson2.JSON | ||
import com.alibaba.fastjson2.JSONWriter | ||
import com.alibaba.fastjson2.to | ||
import com.expediagroup.graphql.server.types.GraphQLServerRequest | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import org.openjdk.jmh.annotations.Benchmark | ||
import org.openjdk.jmh.annotations.Fork | ||
import org.openjdk.jmh.annotations.Measurement | ||
import org.openjdk.jmh.annotations.Scope | ||
import org.openjdk.jmh.annotations.Setup | ||
import org.openjdk.jmh.annotations.State | ||
import org.openjdk.jmh.annotations.Warmup | ||
import java.util.concurrent.TimeUnit | ||
|
||
@State(Scope.Benchmark) | ||
@Fork(value = 5, jvmArgsAppend = ["--add-modules=jdk.incubator.vector", "-Dfastjson2.readerVector=true"]) | ||
@Warmup(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 4, time = 5, timeUnit = TimeUnit.SECONDS) | ||
open class GraphQLServerRequestBatchDeserializationBenchmark { | ||
private val mapper = jacksonObjectMapper() | ||
private lateinit var request: String | ||
private lateinit var batchRequest: String | ||
|
||
@Setup | ||
fun setUp() { | ||
JSON.config(JSONWriter.Feature.WriteNulls) | ||
val loader = this::class.java.classLoader | ||
val operation = loader.getResource("StarWarsDetails.graphql")!!.readText().replace("\n", "\\n") | ||
val variables = loader.getResource("StarWarsDetailsVariables.json")!!.readText() | ||
batchRequest = """ | ||
[ | ||
{ "operationName": "StarWarsDetails", "query": "$operation", "variables": $variables }, | ||
{ "operationName": "StarWarsDetails", "query": "$operation", "variables": $variables }, | ||
{ "operationName": "StarWarsDetails", "query": "$operation", "variables": $variables }, | ||
{ "operationName": "StarWarsDetails", "query": "$operation", "variables": $variables } | ||
] | ||
""".trimIndent() | ||
} | ||
|
||
@Benchmark | ||
fun JacksonDeserializeGraphQLBatchRequest(): GraphQLServerRequest = mapper.readValue(batchRequest) | ||
|
||
@Benchmark | ||
fun FastJsonDeserializeGraphQLBatchRequest(): GraphQLServerRequest = batchRequest.to<GraphQLServerRequest>() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
...graphql-kotlin-server/src/benchmarks/kotlin/GraphQLServerRequestSerializationBenchmark.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.