Skip to content

Commit

Permalink
test: add benchmarks to compare with msgpack-java
Browse files Browse the repository at this point in the history
  • Loading branch information
esensar committed Dec 24, 2024
1 parent cfb9f2e commit 0a5ad92
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
2 changes: 2 additions & 0 deletions serialization-msgpack/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ kotlin {
val jvmBenchmark by getting {
dependencies {
implementation(kotlinx("benchmark-runtime", Dependencies.Versions.benchmark))
implementation("org.msgpack:msgpack-core:0.9.8")
implementation("org.msgpack:jackson-dataformat-msgpack:0.9.8")
}
}
jsTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package com.ensarsarajcic.kotlinx.serialization.msgpack

import com.ensarsarajcic.kotlinx.serialization.msgpack.internal.BasicMsgPackDecoder
import com.ensarsarajcic.kotlinx.serialization.msgpack.stream.toMsgPackBuffer
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.BenchmarkMode
import kotlinx.benchmark.BenchmarkTimeUnit
Expand All @@ -12,29 +16,42 @@ import kotlinx.benchmark.Scope
import kotlinx.benchmark.State
import kotlinx.serialization.Serializable
import kotlinx.serialization.modules.SerializersModule
import org.msgpack.jackson.dataformat.MessagePackFactory

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(BenchmarkTimeUnit.NANOSECONDS)
@Measurement(iterations = 20, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
@State(Scope.Benchmark)
open class DeserializeBenchmarks {
@Serializable
data class SampleClassWithNestedClass(
val testString: String,
val testInt: Int,
val testBoolean: Boolean,
val testNested: NestedClass,
val secondNested: NestedClass? = null,
@JsonIgnoreProperties(ignoreUnknown = true)
class SampleClassWithNestedClass(
@JsonProperty("testString")
var testString: String,
@JsonProperty("testInt")
var testInt: Int,
@JsonProperty("testBoolean")
var testBoolean: Boolean,
@JsonProperty("testNested")
var testNested: NestedClass,
@JsonProperty("secondNested")
var secondNested: NestedClass? = null,
) {
constructor() : this("", 0, false, NestedClass(null), null)

@Serializable
data class NestedClass(
val testInt: Int? = null,
)
@JsonIgnoreProperties(ignoreUnknown = true)
class NestedClass(
@JsonProperty("testInt")
var testInt: Int? = null,
) {
constructor() : this(null)
}
}

// The actual benchmark method
@Benchmark
fun benchmarkMethod() {
fun benchmarkKotlinxSerializationMsgpack() {
val decoder =
BasicMsgPackDecoder(
MsgPackConfiguration.default.copy(ignoreUnknownKeys = true),
Expand All @@ -45,6 +62,16 @@ open class DeserializeBenchmarks {
)
SampleClassWithNestedClass.serializer().deserialize(decoder)
}

@Benchmark
fun benchmarkMsgpackJava() {
val objectMapper = ObjectMapper(MessagePackFactory())
objectMapper.readValue(
@Suppress("ktlint:standard:max-line-length")
"85aa74657374537472696e67a3646566a774657374496e747bab74657374426f6f6c65616ec3aa746573744e657374656483aa74657374537472696e67a3646566ab74657374426f6f6c65616ec3ae616e6f74686572556e6b6e6f776ea474657374ac7365636f6e644e657374656481ab74657374426f6f6c65616ec2".hexStringToByteArray(),
object : TypeReference<SampleClassWithNestedClass>() {},
)
}
}

fun String.hexStringToByteArray() = ByteArray(this.length / 2) { this.substring(it * 2, it * 2 + 2).toInt(16).toByte() }
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ensarsarajcic.kotlinx.serialization.msgpack

import com.fasterxml.jackson.databind.ObjectMapper
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.BenchmarkMode
import kotlinx.benchmark.BenchmarkTimeUnit
Expand All @@ -11,6 +12,7 @@ import kotlinx.benchmark.State
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToByteArray
import org.msgpack.jackson.dataformat.MessagePackFactory

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(BenchmarkTimeUnit.NANOSECONDS)
Expand Down Expand Up @@ -45,7 +47,7 @@ open class SerializeBenchmarks {

// The actual benchmark method
@Benchmark
fun benchmarkMethod() {
fun benchmarkKotlinxSerializationMsgpack() {
val instance =
SampleClassWithNestedClass(
testString = "testString",
Expand All @@ -68,4 +70,30 @@ open class SerializeBenchmarks {
)
MsgPack.encodeToByteArray(instance)
}

@Benchmark
fun benchmarkMsgpackJava() {
val instance =
SampleClassWithNestedClass(
testString = "testString",
testInt = 10,
testBoolean = true,
testNested = SampleClassWithNestedClass.NestedClass(5),
testSample = SampleClass(testString = "testString2", testInt = 17, testBoolean = false),
testSampleList =
listOf(
SampleClass("testString3", testInt = 25, testBoolean = true),
SampleClass("testString4", testInt = 100, testBoolean = false),
),
testSampleMap =
mapOf(
"testString5" to SampleClass("testString5", testInt = 12, testBoolean = false),
"testString6" to SampleClass("testString6", testInt = 15, testBoolean = true),
),
extraBytes = byteArrayOf(0x12, 0x13, 0x14, 0x15),
secondNested = SampleClassWithNestedClass.NestedClass(null),
)
val objectMapper = ObjectMapper(MessagePackFactory())
objectMapper.writeValueAsBytes(instance)
}
}

0 comments on commit 0a5ad92

Please sign in to comment.