Skip to content

Commit 25bc365

Browse files
author
Sergey Mashkov
committed
IO: add more benchmarks
1 parent 4fe8eec commit 25bc365

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

benchmarks/src/jmh/kotlin/benchmarks/ChannelCopyBenchmark.kt

+48-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,27 @@ import kotlinx.coroutines.experimental.*
44
import kotlinx.coroutines.experimental.io.*
55
import org.openjdk.jmh.annotations.*
66
import java.io.*
7-
import java.nio.*
87
import java.util.concurrent.*
8+
import kotlin.coroutines.experimental.*
9+
import kotlin.coroutines.experimental.intrinsics.*
10+
11+
12+
/*
13+
# Run complete. Total time: 00:01:52
14+
15+
Benchmark Mode Cnt Score Error Units
16+
ChannelCopyBenchmark.cioChannelCopy avgt 5 828,087 ± 11,568 ns/op
17+
ChannelCopyBenchmark.cioCopyToInLaunch avgt 5 2016,028 ± 15,846 ns/op
18+
ChannelCopyBenchmark.cioJoinToBeforeWrite avgt 5 1413,410 ± 20,460 ns/op
19+
ChannelCopyBenchmark.cioJoinToClosed avgt 5 892,200 ± 113,468 ns/op
20+
ChannelCopyBenchmark.cioJustWrite avgt 5 478,995 ± 106,499 ns/op
21+
ChannelCopyBenchmark.cioJustWriteUnintercepted avgt 5 175,821 ± 21,018 ns/op
22+
ChannelCopyBenchmark.cioReadAndWrite avgt 5 513,968 ± 5,142 ns/op
23+
ChannelCopyBenchmark.cioReadAndWriteUnintercepted avgt 5 250,731 ± 9,800 ns/op
24+
ChannelCopyBenchmark.javaPipeConnectFirst avgt 5 239,269 ± 11,470 ns/op
25+
ChannelCopyBenchmark.justRunBlocking avgt 5 228,704 ± 4,349 ns/op
26+
ChannelCopyBenchmark.runBlockingAndLaunch avgt 5 833,390 ± 14,968 ns/op
27+
*/
928

1029
@Warmup(iterations = 5)
1130
@Measurement(iterations = 5)
@@ -133,6 +152,13 @@ open class ChannelCopyBenchmark {
133152
c.close(ioe)
134153
}
135154

155+
@Benchmark
156+
fun cioJustWriteUnintercepted() = runForSureNoSuspend {
157+
val c = ByteChannel()
158+
c.writeFully(ABC)
159+
c.close(ioe)
160+
}
161+
136162
@Benchmark
137163
fun cioReadAndWrite() = runBlocking {
138164
val c = ByteChannel(true)
@@ -141,6 +167,14 @@ open class ChannelCopyBenchmark {
141167
c.close()
142168
}
143169

170+
@Benchmark
171+
fun cioReadAndWriteUnintercepted() = runForSureNoSuspend {
172+
val c = ByteChannel(true)
173+
c.writeFully(ABC)
174+
c.readAvailable(buffer)
175+
c.close()
176+
}
177+
144178
@Benchmark
145179
fun justRunBlocking() = runBlocking {
146180
}
@@ -154,16 +188,20 @@ open class ChannelCopyBenchmark {
154188
yield()
155189
}
156190

157-
// @Benchmark
158-
fun javaPipeConnectAfterWrite() {
159-
val pipeIn = PipedInputStream()
160-
val pipeOut = PipedOutputStream()
191+
private fun runForSureNoSuspend(block: suspend () -> Unit) {
192+
if (block.startCoroutineUninterceptedOrReturn(EmptyContinuation) === COROUTINE_SUSPENDED) {
193+
throw IllegalStateException("Unexpected suspend")
194+
}
195+
}
161196

162-
pipeOut.write("ABC".toByteArray())
163-
pipeIn.connect(pipeOut)
164-
pipeIn.read(buffer)
197+
object EmptyContinuation : Continuation<Unit> {
198+
override val context: CoroutineContext
199+
get() = EmptyCoroutineContext
165200

166-
pipeOut.close()
167-
pipeIn.close()
201+
override fun resume(value: Unit) {
202+
}
203+
204+
override fun resumeWithException(exception: Throwable) {
205+
}
168206
}
169207
}

0 commit comments

Comments
 (0)