Skip to content

Commit 8d69c7c

Browse files
author
Sergey Mashkov
committed
IO: channel benchmark
1 parent 9e9929d commit 8d69c7c

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

benchmarks/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ dependencies {
1010
jmh 'com.typesafe.akka:akka-actor:2.0.2'
1111
jmh project(':kotlinx-coroutines-core')
1212
jmh project(':kotlinx-coroutines-core').sourceSets.test.output
13+
jmh project(':kotlinx-coroutines-io')
1314
}
1415

1516
jmh.jmhVersion = '1.19'
1617

1718
jmhJar.archiveName = 'benchmarks.jar'
19+
20+
//jmh {
21+
// include = ['.*Channel.*Benchmark']
22+
// exclude = ['.*Guice.*', '.*PingPong.*']
23+
//}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package benchmarks
2+
3+
import kotlinx.coroutines.experimental.*
4+
import kotlinx.coroutines.experimental.io.*
5+
import org.openjdk.jmh.annotations.*
6+
import java.io.*
7+
import java.nio.*
8+
import java.util.concurrent.*
9+
10+
@Warmup(iterations = 5)
11+
@Measurement(iterations = 5)
12+
@BenchmarkMode(Mode.AverageTime)
13+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
14+
@State(Scope.Benchmark)
15+
@Fork(1)
16+
open class ChannelCopyBenchmark {
17+
private val ABC = "ABC".repeat(100).toByteArray()
18+
private val buffer = ByteArray(4096)
19+
private val ioe = IOException()
20+
21+
@Benchmark
22+
fun javaPipeConnectFirst() {
23+
val pipeIn = PipedInputStream()
24+
val pipeOut = PipedOutputStream()
25+
26+
pipeIn.connect(pipeOut)
27+
28+
pipeOut.write(ABC)
29+
var read = 0
30+
while (read < ABC.size) {
31+
val rc = pipeIn.read(buffer)
32+
if (rc == -1) break
33+
read += rc
34+
}
35+
36+
pipeOut.close()
37+
pipeIn.close()
38+
}
39+
40+
@Benchmark
41+
fun cioChannelCopy() = runBlocking {
42+
val pIn = ByteChannel(true)
43+
val pOut = ByteChannel(true)
44+
45+
pOut.writeFully(ABC)
46+
pOut.close()
47+
48+
pOut.copyAndClose(pIn)
49+
50+
var read = 0
51+
while (read < ABC.size) {
52+
val rc = pIn.readAvailable(buffer)
53+
if (rc == -1) break
54+
read += rc
55+
}
56+
57+
read
58+
}
59+
60+
@Benchmark
61+
fun cioJoinToClosed() = runBlocking {
62+
val pIn = ByteChannel(true)
63+
val pOut = ByteChannel(true)
64+
65+
pOut.writeFully(ABC)
66+
pOut.close()
67+
68+
pOut.joinTo(pIn, true)
69+
70+
var read = 0
71+
while (read < ABC.size) {
72+
val rc = pIn.readAvailable(buffer)
73+
if (rc == -1) break
74+
read += rc
75+
}
76+
77+
read
78+
}
79+
80+
@Benchmark
81+
fun cioJoinToBeforeWrite() = runBlocking {
82+
val pIn = ByteChannel(true)
83+
val pOut = ByteChannel(true)
84+
85+
launch(coroutineContext) {
86+
pOut.joinTo(pIn, true)
87+
}
88+
89+
yield()
90+
91+
pOut.writeFully(ABC)
92+
pOut.close()
93+
94+
var read = 0
95+
while (read < ABC.size) {
96+
val rc = pIn.readAvailable(buffer)
97+
if (rc == -1) break
98+
read += rc
99+
}
100+
101+
read
102+
}
103+
104+
@Benchmark
105+
fun cioCopyToInLaunch() = runBlocking {
106+
val pIn = ByteChannel(true)
107+
val pOut = ByteChannel(true)
108+
109+
launch(coroutineContext) {
110+
pOut.copyTo(pIn)
111+
pIn.close()
112+
}
113+
114+
yield()
115+
116+
pOut.writeFully(ABC)
117+
pOut.close()
118+
119+
var read = 0
120+
while (read < ABC.size) {
121+
val rc = pIn.readAvailable(buffer)
122+
if (rc == -1) break
123+
read += rc
124+
}
125+
126+
read
127+
}
128+
129+
@Benchmark
130+
fun cioJustWrite() = runBlocking {
131+
val c = ByteChannel()
132+
c.writeFully(ABC)
133+
c.close(ioe)
134+
}
135+
136+
@Benchmark
137+
fun cioReadAndWrite() = runBlocking {
138+
val c = ByteChannel(true)
139+
c.writeFully(ABC)
140+
c.readAvailable(buffer)
141+
c.close()
142+
}
143+
144+
@Benchmark
145+
fun justRunBlocking() = runBlocking {
146+
}
147+
148+
@Benchmark
149+
fun runBlockingAndLaunch() = runBlocking {
150+
launch(coroutineContext) {
151+
yield()
152+
}
153+
154+
yield()
155+
}
156+
157+
// @Benchmark
158+
fun javaPipeConnectAfterWrite() {
159+
val pipeIn = PipedInputStream()
160+
val pipeOut = PipedOutputStream()
161+
162+
pipeOut.write("ABC".toByteArray())
163+
pipeIn.connect(pipeOut)
164+
pipeIn.read(buffer)
165+
166+
pipeOut.close()
167+
pipeIn.close()
168+
}
169+
}

0 commit comments

Comments
 (0)