summaryrefslogtreecommitdiff
path: root/benchmark/src/jmh/kotlin/kotlinx/benchmarks/ProtoBaseline.kt
blob: b6f414c49ec2d2e224d0d94f7e05cf7fd16bd49e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.benchmarks

import kotlinx.serialization.*
import kotlinx.serialization.protobuf.*
import org.openjdk.jmh.annotations.*
import java.util.concurrent.*

@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
@Fork(1)
open class ProtoBaseline {

    @Serializable
    class Holder(val a: Int, val b: Int, val c: Long, val d: Double)

    @Serializable
    class HolderExplicit(@ProtoId(1) val a: Int, @ProtoId(2) val b: Int, @ProtoId(3) val c: Long, @ProtoId(4) val d: Double)

    private val holder = Holder(1, 2, 3L, 4.0)
    private val holderBytes = ProtoBuf.encodeToByteArray(Holder.serializer(), holder)

    private val holderExplicit = HolderExplicit(1, 2, 3L, 4.0)
    private val holderHolderExplicitBytes = ProtoBuf.encodeToByteArray(HolderExplicit.serializer(), holderExplicit)

    @Benchmark
    fun toBytes() = ProtoBuf.encodeToByteArray(Holder.serializer(), holder)

    @Benchmark
    fun fromBytes() = ProtoBuf.decodeFromByteArray(Holder.serializer(), holderBytes)

    @Benchmark
    fun toBytesExplicit() = ProtoBuf.encodeToByteArray(HolderExplicit.serializer(), holderExplicit)

    @Benchmark
    fun fromBytesExplicit() = ProtoBuf.decodeFromByteArray(HolderExplicit.serializer(), holderHolderExplicitBytes)
}