@@ -4,8 +4,27 @@ import kotlinx.coroutines.experimental.*
4
4
import kotlinx.coroutines.experimental.io.*
5
5
import org.openjdk.jmh.annotations.*
6
6
import java.io.*
7
- import java.nio.*
8
7
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
+ */
9
28
10
29
@Warmup(iterations = 5 )
11
30
@Measurement(iterations = 5 )
@@ -133,6 +152,13 @@ open class ChannelCopyBenchmark {
133
152
c.close(ioe)
134
153
}
135
154
155
+ @Benchmark
156
+ fun cioJustWriteUnintercepted () = runForSureNoSuspend {
157
+ val c = ByteChannel ()
158
+ c.writeFully(ABC )
159
+ c.close(ioe)
160
+ }
161
+
136
162
@Benchmark
137
163
fun cioReadAndWrite () = runBlocking {
138
164
val c = ByteChannel (true )
@@ -141,6 +167,14 @@ open class ChannelCopyBenchmark {
141
167
c.close()
142
168
}
143
169
170
+ @Benchmark
171
+ fun cioReadAndWriteUnintercepted () = runForSureNoSuspend {
172
+ val c = ByteChannel (true )
173
+ c.writeFully(ABC )
174
+ c.readAvailable(buffer)
175
+ c.close()
176
+ }
177
+
144
178
@Benchmark
145
179
fun justRunBlocking () = runBlocking {
146
180
}
@@ -154,16 +188,20 @@ open class ChannelCopyBenchmark {
154
188
yield ()
155
189
}
156
190
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
+ }
161
196
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
165
200
166
- pipeOut.close()
167
- pipeIn.close()
201
+ override fun resume (value : Unit ) {
202
+ }
203
+
204
+ override fun resumeWithException (exception : Throwable ) {
205
+ }
168
206
}
169
207
}
0 commit comments