Skip to content

Commit 44f0ebb

Browse files
committed
Upgrade infra, benchmarks, introduce common benchmarks
1 parent 008ca5b commit 44f0ebb

File tree

5 files changed

+135
-26
lines changed

5 files changed

+135
-26
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package kotlinx.io.benchmarks
2+
3+
import kotlinx.io.core.*
4+
import org.jetbrains.gradle.benchmarks.*
5+
import kotlin.random.*
6+
7+
@State(Scope.Benchmark)
8+
class PacketReadBenchmark {
9+
private final val size = 32 * 1024 * 1024
10+
private final val array = ByteArray(size)
11+
private final val packet: ByteReadPacket
12+
13+
init {
14+
Random.nextBytes(array)
15+
packet = BytePacketBuilder().apply {
16+
writeFully(array)
17+
}.build()
18+
}
19+
20+
21+
@Benchmark
22+
fun copyAndRelease() {
23+
val input = packet.copy()
24+
input.release()
25+
}
26+
27+
@Benchmark
28+
fun myInput(): Long {
29+
val input = packet.copy()
30+
var c = 0L
31+
32+
repeat(size) {
33+
c += input.readByte().toLong() and 0xff
34+
}
35+
36+
return c
37+
}
38+
39+
@Benchmark
40+
fun myInputTakeWhile(): Long {
41+
val input = packet.copy()
42+
var c = 0L
43+
44+
input.takeWhile { buffer ->
45+
repeat(buffer.readRemaining) {
46+
c += buffer.readByte().toLong() and 0xff
47+
}
48+
true
49+
}
50+
51+
return c
52+
}
53+
54+
@Benchmark
55+
fun directArrayAccess(): Long {
56+
val input = array
57+
var c = 0L
58+
59+
for (i in 0 until size - 1) {
60+
c += input[i].toLong() and 0xff
61+
}
62+
63+
return c
64+
}
65+
66+
@Benchmark
67+
fun myInputReadLong(): Long {
68+
val input = packet.copy()
69+
var c = 0L
70+
71+
repeat(input.remaining.toInt() / 8) {
72+
c += input.readLong()
73+
}
74+
75+
return c
76+
}
77+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package kotlinx.io.benchmarks
2+
3+
import kotlinx.io.charsets.*
4+
import kotlinx.io.core.*
5+
import org.jetbrains.gradle.benchmarks.*
6+
7+
@State(Scope.Benchmark)
8+
class TextDecodeBenchmark {
9+
@Benchmark
10+
fun smallMbKt() = smallTextPacket.copy().readText()
11+
12+
@Benchmark
13+
fun smallMbStringCtor() = String(smallTextBytes, charset = Charsets.UTF_8)
14+
15+
@Benchmark
16+
fun largeMbKt() = largeTextPacket.copy().readText()
17+
18+
@Benchmark
19+
fun largeMbStringCtor() = String(largeTextBytes, charset = Charsets.UTF_8)
20+
21+
@Benchmark
22+
fun smallASCIIKt() = smallTextPacketASCII.copy().readText()
23+
24+
@Benchmark
25+
fun smallASCIIStringCtor() = String(smallTextBytesASCII, charset = Charsets.UTF_8)
26+
27+
@Benchmark
28+
fun largeASCIIKt() = largeTextPacketASCII.copy().readText()
29+
30+
@Benchmark
31+
fun largeASCIIStringCtor() = String(largeTextBytesASCII, charset = Charsets.UTF_8)
32+
33+
companion object {
34+
private val smallTextBytes = "\u0422\u0432\u0437.".toByteArray(Charsets.UTF_8)
35+
private val smallTextBytesASCII = "ABC.".toByteArray(Charsets.UTF_8)
36+
private val largeTextBytes = ByteArray(smallTextBytes.size * 10000) {
37+
smallTextBytes[it % smallTextBytes.size]
38+
}
39+
private val largeTextBytesASCII = ByteArray(smallTextBytesASCII.size * 10000) {
40+
smallTextBytesASCII[it % smallTextBytesASCII.size]
41+
}
42+
43+
private val smallTextPacket = buildPacket { writeFully(smallTextBytes) }
44+
private val smallTextPacketASCII = buildPacket { writeFully(smallTextBytesASCII) }
45+
private val largeTextPacket = buildPacket { writeFully(largeTextBytes) }
46+
private val largeTextPacketASCII = buildPacket { writeFully(largeTextBytesASCII) }
47+
}
48+
49+
}

benchmarks/jvmMain/src/kotlinx/io/benchmarks/PacketReadBenchmark.kt renamed to benchmarks/jvmMain/src/kotlinx/io/benchmarks/JvmPacketReadBenchmark.kt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
package kotlinx.io.benchmarks
22

33
import kotlinx.io.core.*
4-
import org.openjdk.jmh.annotations.*
4+
import org.jetbrains.gradle.benchmarks.*
55
import java.io.*
66
import java.nio.*
77
import java.util.*
8-
import java.util.concurrent.*
98

109
@State(Scope.Benchmark)
11-
@Fork(1)
12-
@Warmup(iterations = 10)
13-
@Measurement(iterations = 15)
14-
//@BenchmarkMode(Mode.Throughput, Mode.AverageTime)
15-
@BenchmarkMode(Mode.AverageTime)
16-
@OutputTimeUnit(TimeUnit.MILLISECONDS)
17-
class PacketReadBenchmark {
10+
class JvmPacketReadBenchmark {
1811
private final val size = 32 * 1024 * 1024
1912
private final val array = ByteArray(size)
2013
private final val packet: ByteReadPacket

benchmarks/jvmMain/src/kotlinx/io/benchmarks/TextDecodeBenchmark.kt renamed to benchmarks/jvmMain/src/kotlinx/io/benchmarks/JvmTextDecodeBenchmark.kt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
package kotlinx.io.benchmarks
22

33
import kotlinx.io.core.*
4-
import org.openjdk.jmh.annotations.*
5-
import java.util.concurrent.*
4+
import org.jetbrains.gradle.benchmarks.*
65

76
@State(Scope.Benchmark)
8-
@Fork(1)
9-
@Warmup(iterations = 10)
10-
@Measurement(iterations = 15)
11-
//@BenchmarkMode(Mode.Throughput, Mode.AverageTime)
12-
@BenchmarkMode(Mode.Throughput)
13-
@OutputTimeUnit(TimeUnit.MILLISECONDS)
14-
class TextDecodeBenchmark {
15-
16-
7+
class JvmTextDecodeBenchmark {
178
/*
189
# Results on unit 660
1910
# Run complete. Total time: 00:05:04

gradle.properties

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ version=0.2.0
33
org.gradle.parallel=true
44

55
# kotlin
6-
kotlin_version=1.3.21
6+
kotlin_version=1.3.30
77
kotlin.incremental.multiplatform=true
88
kotlin.code.style=official
99
org.jetbrains.kotlin.native.jvmArgs=-XX:TieredStopAtLevel=1
1010

1111
# publishing
1212
bintray_plugin_version=1.8.4-jetbrains-5
1313
# kotlin libraries
14-
atomic_fu_version=0.12.1
15-
coroutines_version=1.1.1
14+
atomic_fu_version=0.12.3
15+
coroutines_version=1.2.0
1616

1717
# js
1818
node_version = 10.14.1
@@ -21,8 +21,7 @@ mocha_version=4.1.0
2121
mocha_headless_chrome_version=1.8.2
2222
mocha_teamcity_reporter_version=2.2.2
2323
source_map_support_version=0.5.3
24-
gradle_node_version=1.2.0
2524

2625
#infra
27-
infra_version=0.1.0-dev-42
28-
benchmarks_version=0.1.7-dev-14
26+
infra_version=0.1.0-dev-44
27+
benchmarks_version=0.1.7-dev-20

0 commit comments

Comments
 (0)