summaryrefslogtreecommitdiff
path: root/benchmark
diff options
context:
space:
mode:
authorSergey Shanshin <sergey.shanshin@jetbrains.com>2022-06-30 15:43:42 +0300
committerGitHub <noreply@github.com>2022-06-30 15:43:42 +0300
commit5e8ccad1f70a9457e0ffe6ae6b10a0bd0eaaa618 (patch)
treee522ba5e8da0cb4701f4a259d9f2ba5b43b42bf2 /benchmark
parent605a35faa87534e24482aa00a52a2a71bebd51a3 (diff)
downloadkotlinx.serialization-5e8ccad1f70a9457e0ffe6ae6b10a0bd0eaaa618.tar.gz
Add Okio integration (#1901)
* Add okio integration as separate module json-okio * Extract separate module json-tests that tests both Java streams and okio * Rewrite Java stream writer so that it uses much faster in-place UTF8 encoder instead of java.io.Writer * Add benchmarks for streams and okio * Disable targets for tests that are not supported by okio (they can't be run on LinuxX64-86 hosts anyway)
Diffstat (limited to 'benchmark')
-rw-r--r--benchmark/build.gradle21
-rw-r--r--benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/JacksonComparisonBenchmark.kt23
-rw-r--r--benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/TwitterBenchmark.kt10
3 files changed, 47 insertions, 7 deletions
diff --git a/benchmark/build.gradle b/benchmark/build.gradle
index 0935e5a3..827726bf 100644
--- a/benchmark/build.gradle
+++ b/benchmark/build.gradle
@@ -13,19 +13,26 @@ sourceCompatibility = 1.8
targetCompatibility = 1.8
jmh.jmhVersion = "1.22"
+processJmhResources {
+ doFirst {
+ duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
+ }
+}
+
jmhJar {
- baseName 'benchmarks'
- classifier = null
- version = null
- destinationDir = file("$rootDir")
+ archiveBaseName.set('benchmarks')
+ archiveVersion.set('')
+ destinationDirectory = file("$rootDir")
}
dependencies {
implementation 'org.openjdk.jmh:jmh-core:1.22'
- implementation 'com.google.guava:guava:24.1.1-jre'
- implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.1'
- implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.12.1'
+ implementation 'com.google.guava:guava:31.1-jre'
+ implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
+ implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3'
+ implementation "com.squareup.okio:okio:$okio_version"
implementation project(':kotlinx-serialization-core')
implementation project(':kotlinx-serialization-json')
+ implementation project(':kotlinx-serialization-json-okio')
implementation project(':kotlinx-serialization-protobuf')
}
diff --git a/benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/JacksonComparisonBenchmark.kt b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/JacksonComparisonBenchmark.kt
index b8125001..783888b4 100644
--- a/benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/JacksonComparisonBenchmark.kt
+++ b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/JacksonComparisonBenchmark.kt
@@ -4,7 +4,11 @@ import com.fasterxml.jackson.databind.*
import com.fasterxml.jackson.module.kotlin.*
import kotlinx.serialization.*
import kotlinx.serialization.json.*
+import kotlinx.serialization.json.okio.encodeToSink
+import okio.blackholeSink
+import okio.buffer
import org.openjdk.jmh.annotations.*
+import java.io.OutputStream
import java.util.concurrent.*
@Warmup(iterations = 7, time = 1)
@@ -63,6 +67,13 @@ open class JacksonComparisonBenchmark {
cookies = "_ga=GA1.2.971852807.1546968515"
)
+ private val devNullSink = blackholeSink().buffer()
+ private val devNullStream = object : OutputStream() {
+ override fun write(b: Int) {}
+ override fun write(b: ByteArray) {}
+ override fun write(b: ByteArray, off: Int, len: Int) {}
+ }
+
private val stringData = Json.encodeToString(DefaultPixelEvent.serializer(), data)
@Serializable
@@ -83,12 +94,24 @@ open class JacksonComparisonBenchmark {
fun kotlinToString(): String = Json.encodeToString(DefaultPixelEvent.serializer(), data)
@Benchmark
+ fun kotlinToStream() = Json.encodeToStream(DefaultPixelEvent.serializer(), data, devNullStream)
+
+ @Benchmark
+ fun kotlinToOkio() = Json.encodeToSink(DefaultPixelEvent.serializer(), data, devNullSink)
+
+ @Benchmark
fun kotlinToStringWithEscapes(): String = Json.encodeToString(DefaultPixelEvent.serializer(), dataWithEscapes)
@Benchmark
fun kotlinSmallToString(): String = Json.encodeToString(SmallDataClass.serializer(), smallData)
@Benchmark
+ fun kotlinSmallToStream() = Json.encodeToStream(SmallDataClass.serializer(), smallData, devNullStream)
+
+ @Benchmark
+ fun kotlinSmallToOkio() = Json.encodeToSink(SmallDataClass.serializer(), smallData, devNullSink)
+
+ @Benchmark
fun jacksonFromString(): DefaultPixelEvent = objectMapper.readValue(stringData, DefaultPixelEvent::class.java)
@Benchmark
diff --git a/benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/TwitterBenchmark.kt b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/TwitterBenchmark.kt
index 5c930ec6..90889fe8 100644
--- a/benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/TwitterBenchmark.kt
+++ b/benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/TwitterBenchmark.kt
@@ -3,6 +3,7 @@ package kotlinx.benchmarks.json
import kotlinx.benchmarks.model.*
import kotlinx.serialization.json.*
import org.openjdk.jmh.annotations.*
+import java.io.OutputStream
import java.util.concurrent.*
@Warmup(iterations = 7, time = 1)
@@ -24,6 +25,12 @@ open class TwitterBenchmark {
private val jsonImplicitNulls = Json { explicitNulls = false }
+ private val devNullStream = object : OutputStream() {
+ override fun write(b: Int) {}
+ override fun write(b: ByteArray) {}
+ override fun write(b: ByteArray, off: Int, len: Int) {}
+ }
+
@Setup
fun init() {
require(twitter == Json.decodeFromString(Twitter.serializer(), Json.encodeToString(Twitter.serializer(), twitter)))
@@ -38,4 +45,7 @@ open class TwitterBenchmark {
@Benchmark
fun encodeTwitter() = Json.encodeToString(Twitter.serializer(), twitter)
+
+ @Benchmark
+ fun encodeTwitterStream() = Json.encodeToStream(Twitter.serializer(), twitter, devNullStream)
}