1 /* 2 * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 package kotlinx.benchmarks.json 5 6 import kotlinx.serialization.* 7 import kotlinx.serialization.json.* 8 import org.openjdk.jmh.annotations.* 9 import java.util.concurrent.* 10 11 @Warmup(iterations = 5, time = 1) 12 @Measurement(iterations = 5, time = 1) 13 @BenchmarkMode(Mode.Throughput) 14 @OutputTimeUnit(TimeUnit.MICROSECONDS) 15 @State(Scope.Benchmark) 16 @Fork(2) 17 open class CoerceInputValuesBenchmark { 18 19 @Serializable 20 class Holder( 21 val i1: Int, 22 val i2: Int, 23 val i3: Int, 24 val i4: Int, 25 val i5: Int, 26 val i6: Int, 27 val i7: Int, 28 val i8: Int, 29 val i9: Int, 30 val i10: Int 31 ) 32 33 @Serializable 34 class NullableHolder( 35 val i1: Int?, 36 val i2: Int?, 37 val i3: Int?, 38 val i4: Int?, 39 val i5: Int?, 40 val i6: Int?, 41 val i7: Int?, 42 val i8: Int?, 43 val i9: Int?, 44 val i10: Int? 45 ) 46 47 private val str = """{"i1":1,"i2":1,"i3":1,"i4":1,"i5":1,"i6":1,"i7":1,"i8":1,"i9":1,"i10":1}""" 48 <lambda>null49 private val json = Json { coerceInputValues = false } <lambda>null50 private val coercingJson = Json { coerceInputValues = true } 51 52 @Benchmark testNullableCoercingnull53 fun testNullableCoercing() = coercingJson.decodeFromString(NullableHolder.serializer(), str) 54 55 @Benchmark 56 fun testNullableRegular() = json.decodeFromString(NullableHolder.serializer(), str) 57 58 @Benchmark 59 fun testNonNullableCoercing() = coercingJson.decodeFromString(Holder.serializer(), str) 60 61 @Benchmark 62 fun testNonNullableRegular() = json.decodeFromString(Holder.serializer(), str) 63 } 64