Skip to content

Instantly share code, notes, and snippets.

@sandwwraith
Created December 12, 2023 16:58
Show Gist options
  • Save sandwwraith/2ebed6c997264c730d880ca4ef75bd46 to your computer and use it in GitHub Desktop.
Save sandwwraith/2ebed6c997264c730d880ca4ef75bd46 to your computer and use it in GitHub Desktop.
Subject: [PATCH] Cbor benchmarks
---
Index: benchmark/build.gradle
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/benchmark/build.gradle b/benchmark/build.gradle
--- a/benchmark/build.gradle (revision d3039fb81642908fff40285b9092cb424fdeded1)
+++ b/benchmark/build.gradle (date 1702397355468)
@@ -49,4 +49,5 @@
implementation project(':kotlinx-serialization-json')
implementation project(':kotlinx-serialization-json-okio')
implementation project(':kotlinx-serialization-protobuf')
+ implementation project(':kotlinx-serialization-cbor')
}
Index: benchmark/src/jmh/kotlin/kotlinx/benchmarks/cbor/CborBaseline.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/benchmark/src/jmh/kotlin/kotlinx/benchmarks/cbor/CborBaseline.kt b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/cbor/CborBaseline.kt
new file mode 100644
--- /dev/null (date 1702397706918)
+++ b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/cbor/CborBaseline.kt (date 1702397706918)
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ */
+
+package kotlinx.benchmarks.cbor
+
+import kotlinx.serialization.Serializable
+import kotlinx.serialization.cbor.*
+import org.openjdk.jmh.annotations.*
+import java.util.concurrent.*
+
+@Serializable
+data class KTestAllTypes(
+ val i32: Int,
+ val i64: Long,
+ val f: Float,
+ val d: Double,
+ val s: String,
+ val b: Boolean = false,
+)
+
+@Serializable
+data class KTestOuterMessage(
+ val a: Int,
+ val b: Double,
+ val inner: KTestAllTypes,
+ val s: String,
+ val ss: List<String>
+)
+
+@Warmup(iterations = 5, time = 1)
+@Measurement(iterations = 10, time = 1)
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@State(Scope.Benchmark)
+@Fork(1)
+open class CborBaseline {
+ val baseMessage = KTestOuterMessage(
+ 42,
+ 256123123412.0,
+ s = "string",
+ ss = listOf("a", "b", "c"),
+ inner = KTestAllTypes(-123124512, 36253671257312, Float.MIN_VALUE, -23e15, "foobarbaz")
+ )
+
+ val cbor = Cbor {
+ encodeDefaults = true
+ writeKeyTags = false
+ writeValueTags = false
+ writeDefiniteLengths = false
+ preferCborLabelsOverNames = false
+ }
+
+ val baseBytes = cbor.encodeToByteArray(KTestOuterMessage.serializer(), baseMessage)
+
+ @Benchmark
+ fun toBytes() = cbor.encodeToByteArray(KTestOuterMessage.serializer(), baseMessage)
+
+ @Benchmark
+ fun fromBytes() = cbor.decodeFromByteArray(KTestOuterMessage.serializer(), baseBytes)
+
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment