-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: kotlinx serialization for GraphQLServerRequest (#1937)
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">
- Loading branch information
1 parent
2fb8447
commit e65437a
Showing
14 changed files
with
2,354 additions
and
23 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
74 changes: 74 additions & 0 deletions
74
...aphql-kotlin-server/src/benchmarks/kotlin/GraphQLServerRequestDeserializationBenchmark.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,74 @@ | ||
/* | ||
* 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.expediagroup.graphql.server.testtypes.GraphQLServerRequest | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import kotlinx.serialization.json.Json | ||
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(5) | ||
@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) | ||
open class GraphQLServerRequestDeserializationBenchmark { | ||
private val mapper = jacksonObjectMapper() | ||
private lateinit var request: String | ||
private lateinit var batchRequest: String | ||
|
||
@Setup | ||
fun setUp() { | ||
val loader = this::class.java.classLoader | ||
val operation = loader.getResource("StarWarsDetails.graphql")!!.readText().replace("\n", "\\n") | ||
val variables = loader.getResource("StarWarsDetailsVariables.json")!!.readText() | ||
request = """ | ||
{ | ||
"operationName": "StarWarsDetails", | ||
"query": "$operation", | ||
"variables": $variables | ||
} | ||
""".trimIndent() | ||
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 JacksonDeserializeGraphQLRequest(): GraphQLServerRequest = mapper.readValue(request) | ||
|
||
@Benchmark | ||
fun JacksonDeserializeGraphQLBatchRequest(): GraphQLServerRequest = mapper.readValue(batchRequest) | ||
|
||
@Benchmark | ||
fun KSerializationDeserializeGraphQLRequest(): GraphQLServerRequest = Json.decodeFromString(request) | ||
|
||
@Benchmark | ||
fun KSerializationDeserializeGraphQLBatchRequest(): GraphQLServerRequest = Json.decodeFromString(batchRequest) | ||
} |
70 changes: 70 additions & 0 deletions
70
...graphql-kotlin-server/src/benchmarks/kotlin/GraphQLServerRequestSerializationBenchmark.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,70 @@ | ||
/* | ||
* 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.expediagroup.graphql.server.testtypes.GraphQLBatchRequest | ||
import com.expediagroup.graphql.server.testtypes.GraphQLRequest | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
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(5) | ||
@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS) | ||
open class GraphQLServerRequestSerializationBenchmark { | ||
private val mapper = jacksonObjectMapper() | ||
private lateinit var request: GraphQLRequest | ||
private lateinit var batchRequest: GraphQLBatchRequest | ||
|
||
@Setup | ||
fun setUp() { | ||
val loader = this::class.java.classLoader | ||
val operation = loader.getResource("StarWarsDetails.graphql")!!.readText().replace("\n", "\\n") | ||
val variables = mapper.readValue<Map<String, Any?>>( | ||
loader.getResourceAsStream("StarWarsDetailsVariables.json")!! | ||
) | ||
request = GraphQLRequest(operation, "StarWarsDetails", variables) | ||
batchRequest = GraphQLBatchRequest( | ||
GraphQLRequest(operation, "StarWarsDetails", variables), | ||
GraphQLRequest(operation, "StarWarsDetails", variables), | ||
GraphQLRequest(operation, "StarWarsDetails", variables), | ||
GraphQLRequest(operation, "StarWarsDetails", variables) | ||
) | ||
} | ||
|
||
@Benchmark | ||
fun JacksonSerializeGraphQLRequest(): String = mapper.writeValueAsString(request) | ||
|
||
@Benchmark | ||
fun JacksonSerializeGraphQLBatchRequest(): String = mapper.writeValueAsString(batchRequest) | ||
|
||
@Benchmark | ||
fun KSerializationSerializeGraphQLRequest(): String = Json.encodeToString(request) | ||
|
||
@Benchmark | ||
fun KSerializationSerializeGraphQLBatchRequest(): String = Json.encodeToString(batchRequest) | ||
} |
65 changes: 65 additions & 0 deletions
65
...phql-kotlin-server/src/benchmarks/kotlin/GraphQLServerResponseDeserializationBenchmark.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,65 @@ | ||
/* | ||
* 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.expediagroup.graphql.server.testtypes.GraphQLServerResponse | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import kotlinx.serialization.json.Json | ||
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(5) | ||
@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS) | ||
open class GraphQLServerResponseDeserializationBenchmark { | ||
private val mapper = jacksonObjectMapper() | ||
private lateinit var response: String | ||
private lateinit var batchResponse: String | ||
|
||
@Setup | ||
fun setUp() { | ||
response = this::class.java.classLoader.getResource("StarWarsDetailsResponse.json")!!.readText() | ||
batchResponse = """ | ||
[ | ||
$response, | ||
$response, | ||
$response, | ||
$response | ||
] | ||
""".trimIndent() | ||
} | ||
|
||
@Benchmark | ||
fun JacksonDeserializeGraphQLResponse(): GraphQLServerResponse = mapper.readValue(response) | ||
|
||
@Benchmark | ||
fun JacksonDeserializeGraphQLBatchResponse(): GraphQLServerResponse = mapper.readValue(batchResponse) | ||
|
||
@Benchmark | ||
fun KSerializationDeserializeGraphQLResponse(): GraphQLServerResponse = Json.decodeFromString(response) | ||
|
||
@Benchmark | ||
fun KSerializationDeserializeGraphQLBatchResponse(): GraphQLServerResponse = Json.decodeFromString(batchResponse) | ||
} |
72 changes: 72 additions & 0 deletions
72
...raphql-kotlin-server/src/benchmarks/kotlin/GraphQLServerResponseSerializationBenchmark.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,72 @@ | ||
/* | ||
* 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.expediagroup.graphql.server.testtypes.GraphQLBatchResponse | ||
import com.expediagroup.graphql.server.testtypes.GraphQLResponse | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
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(5) | ||
@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS) | ||
open class GraphQLServerResponseSerializationBenchmark { | ||
private val mapper = jacksonObjectMapper() | ||
private lateinit var response: GraphQLResponse | ||
private lateinit var batchResponse: GraphQLBatchResponse | ||
|
||
@Setup | ||
fun setUp() { | ||
val data = mapper.readValue<Map<String, Any?>>( | ||
this::class.java.classLoader.getResourceAsStream("StarWarsDetailsResponse.json")!! | ||
) | ||
response = GraphQLResponse( | ||
mapper.readValue<Map<String, Any?>>( | ||
this::class.java.classLoader.getResourceAsStream("StarWarsDetailsResponse.json")!! | ||
) | ||
) | ||
batchResponse = GraphQLBatchResponse( | ||
GraphQLResponse(data), | ||
GraphQLResponse(data), | ||
GraphQLResponse(data), | ||
GraphQLResponse(data) | ||
) | ||
} | ||
|
||
@Benchmark | ||
fun JacksonSerializeGraphQLResponse(): String = mapper.writeValueAsString(response) | ||
|
||
@Benchmark | ||
fun JacksonSerializeGraphQLBatchResponse(): String = mapper.writeValueAsString(batchResponse) | ||
|
||
@Benchmark | ||
fun KSerializationSerializeGraphQLResponse(): String = Json.encodeToString(response) | ||
|
||
@Benchmark | ||
fun KSerializationSerializeGraphQLBatchResponse(): String = Json.encodeToString(batchResponse) | ||
} |
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.