Skip to content

Commit 4a5b79b

Browse files
committed
Add benchmarks on java.io/nio integration and byte array reading
1 parent 4513ac4 commit 4a5b79b

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

benchmarks/src/commonMain/kotlin/BufferOps.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,43 @@ open class BufferGetBenchmark {
340340

341341
@Benchmark
342342
fun get() = buffer[offset]
343+
}
344+
345+
open class BufferReadWriteByteArray: BufferRWBenchmarkBase() {
346+
private var inputArray = ByteArray(0)
347+
private var outputArray = ByteArray(0)
348+
349+
@Param("1", "1024", (SEGMENT_SIZE_IN_BYTES * 3).toString())
350+
var size: Int = 0
351+
352+
@Setup
353+
fun allocateArrays() {
354+
inputArray = ByteArray(size)
355+
outputArray = ByteArray(size)
356+
}
357+
358+
@Benchmark
359+
fun benchmark(blackhole: Blackhole) {
360+
buffer.write(inputArray)
361+
buffer.readFully(outputArray)
362+
blackhole.consume(outputArray)
363+
}
364+
}
365+
366+
open class BufferReadNewByteArray: BufferRWBenchmarkBase() {
367+
private var inputArray = ByteArray(0)
368+
369+
@Param("1", "1024", (SEGMENT_SIZE_IN_BYTES * 3).toString())
370+
var size: Int = 0
371+
372+
@Setup
373+
fun allocateArray() {
374+
inputArray = ByteArray(size)
375+
}
376+
377+
@Benchmark
378+
fun benchmark(): ByteArray {
379+
buffer.write(inputArray)
380+
return buffer.readByteArray(size.toLong())
381+
}
343382
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.io.benchmarks
7+
8+
import java.nio.ByteBuffer
9+
10+
import kotlinx.benchmark.*
11+
12+
open class ByteBufferReadWrite: BufferRWBenchmarkBase() {
13+
private var inputBuffer = ByteBuffer.allocate(0)
14+
private var outputBuffer = ByteBuffer.allocate(0)
15+
16+
@Param("1", "1024", (SEGMENT_SIZE_IN_BYTES * 3).toString())
17+
var size: Int = 0
18+
19+
@Setup
20+
fun allocateBuffers() {
21+
inputBuffer = ByteBuffer.allocate(size)
22+
inputBuffer.put(ByteArray(size))
23+
outputBuffer = ByteBuffer.allocate(size)
24+
}
25+
26+
@Benchmark
27+
fun benchmark(): ByteBuffer {
28+
inputBuffer.rewind()
29+
outputBuffer.clear()
30+
buffer.write(inputBuffer)
31+
while (buffer.read(outputBuffer) > 0) {
32+
// do nothing
33+
}
34+
return outputBuffer
35+
}
36+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.io.benchmarks
7+
8+
import kotlinx.benchmark.*
9+
10+
open class InputStreamByteRead: BufferRWBenchmarkBase() {
11+
private val stream = buffer.inputStream()
12+
13+
@Benchmark
14+
fun benchmark(): Int {
15+
buffer.writeByte(0)
16+
return stream.read()
17+
}
18+
}
19+
20+
open class OutputStreamByteWrite: BufferRWBenchmarkBase() {
21+
private val stream = buffer.outputStream()
22+
23+
@Benchmark
24+
fun benchmark(): Byte {
25+
stream.write(0)
26+
return buffer.readByte()
27+
}
28+
}
29+
30+
abstract class StreamByteArrayBenchmarkBase: BufferRWBenchmarkBase() {
31+
protected var inputArray = ByteArray(0)
32+
protected var outputArray = ByteArray(0)
33+
34+
@Param("1", "128", SEGMENT_SIZE_IN_BYTES.toString())
35+
var size: Int = 0
36+
37+
@Setup
38+
fun setupArray() {
39+
inputArray = ByteArray(size)
40+
outputArray = ByteArray(size)
41+
}
42+
}
43+
44+
open class InputStreamByteArrayRead: StreamByteArrayBenchmarkBase() {
45+
private val stream = buffer.inputStream()
46+
47+
@Benchmark
48+
fun benchmark(blackhole: Blackhole) {
49+
buffer.write(inputArray)
50+
var offset = 0
51+
while (offset < outputArray.size) {
52+
offset += stream.read(outputArray, offset, outputArray.size - offset)
53+
}
54+
blackhole.consume(outputArray)
55+
}
56+
}
57+
58+
open class OutputStreamByteArrayWrite: StreamByteArrayBenchmarkBase() {
59+
private val stream = buffer.outputStream()
60+
61+
@Benchmark
62+
fun benchmark(blackhole: Blackhole) {
63+
stream.write(outputArray)
64+
buffer.readFully(inputArray)
65+
blackhole.consume(inputArray)
66+
}
67+
}

0 commit comments

Comments
 (0)