• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.protobuf
5 
6 import kotlinx.serialization.Serializable
7 import kotlinx.serialization.protobuf.*
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(1)
17 open class ProtoBaseline {
18 
19     @Serializable
20     class Holder(val a: Int, val b: Int, val c: Long, val d: Double)
21 
22     @Serializable
23     class HolderExplicit(@ProtoNumber(1) val a: Int, @ProtoNumber(2) val b: Int, @ProtoNumber(3) val c: Long, @ProtoNumber(4) val d: Double)
24 
25     private val holder = Holder(1, 2, 3L, 4.0)
26     private val holderBytes = ProtoBuf.encodeToByteArray(Holder.serializer(), holder)
27 
28     private val holderExplicit = HolderExplicit(1, 2, 3L, 4.0)
29     private val holderHolderExplicitBytes = ProtoBuf.encodeToByteArray(HolderExplicit.serializer(), holderExplicit)
30 
31     @Benchmark
toBytesnull32     fun toBytes() = ProtoBuf.encodeToByteArray(Holder.serializer(), holder)
33 
34     @Benchmark
35     fun fromBytes() = ProtoBuf.decodeFromByteArray(Holder.serializer(), holderBytes)
36 
37     @Benchmark
38     fun toBytesExplicit() = ProtoBuf.encodeToByteArray(HolderExplicit.serializer(), holderExplicit)
39 
40     @Benchmark
41     fun fromBytesExplicit() = ProtoBuf.decodeFromByteArray(HolderExplicit.serializer(), holderHolderExplicitBytes)
42 }
43