-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add channel benchmarks #4546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Add channel benchmarks #4546
Changes from all commits
c5c72a3
37be033
0383992
33f9e72
bf9af79
c406fbd
8425561
be3f273
b5c7bf7
f82a493
939c1d5
4647c52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| package benchmarks | ||
|
|
||
| import kotlinx.coroutines.* | ||
| import kotlinx.coroutines.channels.* | ||
| import org.openjdk.jmh.annotations.* | ||
| import java.util.concurrent.* | ||
|
|
||
| @Warmup(iterations = 7, time = 1) | ||
| @Measurement(iterations = 10, time = 1) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
| @State(Scope.Benchmark) | ||
| @Fork(1) | ||
| open class ChannelBenchmark { | ||
| // max coroutines launched per benchmark | ||
| // to allow for true parallelism | ||
| val cores = Runtime.getRuntime().availableProcessors() | ||
|
|
||
| // 4 KB, 40 KB, 400 KB, 4 MB, 40 MB, 400 MB | ||
murfel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Param(value = ["1000", "10000", "100000", "1000000", "10000000", "100000000"]) | ||
| var count: Int = 0 | ||
|
|
||
| // 1. Preallocate. | ||
| // 2. Different values to avoid helping the cache. | ||
| val list = List(100000000) { it } | ||
|
|
||
| @State(Scope.Benchmark) | ||
| open class UnlimitedChannelWrapper { | ||
| // 0, 4 MB, 40 MB, 400 MB | ||
murfel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Param(value = ["0", "1000000", "10000000", "100000000"]) | ||
| private var prefill = 0 | ||
|
|
||
| lateinit var channel: Channel<Int> | ||
|
|
||
| val list = List(100000000) { it } | ||
|
|
||
| @Setup(Level.Invocation) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why it has to be done before every benchmark function invocation and not once per trial / iteration?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a tradeoff. After each invocation, there could be a little extra items in the channel, which can accumulate with iterations. I can rewrite |
||
| fun createPrefilledChannel() { | ||
| channel = Channel(Channel.UNLIMITED) | ||
| repeat(prefill) { | ||
| channel.trySend(list[it]) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @Benchmark | ||
| fun sendUnlimited() = runBlocking { | ||
| runSend(count, Channel.UNLIMITED) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun sendConflated() = runBlocking { | ||
| runSend(count, Channel.CONFLATED) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun sendReceiveUnlimited(wrapper: UnlimitedChannelWrapper) = runBlocking { | ||
| runSendReceive(wrapper.channel, count) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun sendReceiveConflated() = runBlocking(Dispatchers.Default) { | ||
| runSendReceive(Channel(Channel.CONFLATED), count) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun sendReceiveRendezvous() = runBlocking(Dispatchers.Default) { | ||
| // NB: Rendezvous is partly benchmarking the scheduler, not the channel alone. | ||
| // So don't trust the Rendezvous results too much. | ||
| runSendReceive(Channel(Channel.RENDEZVOUS), count) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun oneSenderManyReceivers(wrapper: UnlimitedChannelWrapper) = runBlocking { | ||
| runSendReceive(wrapper.channel, count, 1, cores - 1) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun manySendersOneReceiver(wrapper: UnlimitedChannelWrapper) = runBlocking { | ||
| runSendReceive(wrapper.channel, count, cores - 1, 1) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun manySendersManyReceivers(wrapper: UnlimitedChannelWrapper) = runBlocking { | ||
| runSendReceive(wrapper.channel, count, cores / 2, cores / 2) | ||
| } | ||
|
|
||
| private suspend fun runSend(count: Int, capacity: Int) { | ||
| val channel = Channel<Int>(capacity) | ||
| repeat(count) { | ||
| channel.send(list[it]) | ||
| } | ||
| } | ||
|
|
||
| suspend fun <E> Channel<E>.forEach(action: (E) -> Unit) { | ||
murfel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (element in this) { | ||
| action(element) | ||
| } | ||
| } | ||
|
|
||
| // NB: not all parameter combinations make sense in general. | ||
| // E.g., for the rendezvous channel, senders should be equal to receivers. | ||
| // If they are non-equal, it's a special case of performance under contention. | ||
| private suspend inline fun runSendReceive(channel: Channel<Int>, count: Int, senders: Int = 1, receivers: Int = 1) { | ||
| //require (senders > 0 && receivers > 0) | ||
| //require (senders + receivers <= cores) // Can be used with more than num cores, but what would it measure? | ||
| // if the channel is prefilled, only receive the items that were sent by this function | ||
| val receiveAll = channel.isEmpty | ||
| // send almost `count` items, up to `senders - 1` items will not be sent (negligible) | ||
| val countPerSender = count / senders | ||
| // for prefilled channel only: up to `receivers - 1` items of the sent items will not be received | ||
| // (on top of the prefilled items which we do not aim to receive at all) (negligible) | ||
| val countPerReceiverAtLeast = countPerSender * senders / receivers | ||
| withContext(Dispatchers.Default) { | ||
| repeat(receivers) { | ||
| launch { | ||
| if (receiveAll) { | ||
| channel.forEach { | ||
| // possibly receive into the blackhole | ||
| } | ||
| } else { | ||
| repeat(countPerReceiverAtLeast) { | ||
| // possibly receive into the blackhole | ||
| channel.receive() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| coroutineScope { | ||
| repeat(senders) { | ||
| launch { | ||
| repeat(countPerSender) { | ||
| channel.send(list[it]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| channel.close() | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please elaborate what exactly you're trying to measure using these benchmarks?
Right now, it looks like "time required to create a new channel, send N messages into it (and, optionally, receive them), and then close the channel". However, I thought that initial idea was to measure the latency of sending (and receiving) a single message into the channel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do measure that, indirectly. Do you suggest to literally only send/receive one message per benchmark? Is that reliable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Direct measurements are always better than indirect. If the goal is to measure send/recv timing, let's measure it.
What makes you think it will be unreliable?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not always. See below. Also depends on how you define "better".
Again, I am measuring that. My way of measuring is a valid way of measuring. Having to assert that makes me feel dismissed.
We can explore other ways to measure, for sure.
What setup did you have in mind, something like this?
Or this? (no suspension, trySend/tryReceive)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The internal structure of the channel may be worth taking into account. For a prefilled channel with 32+ elements (32 is the default channel segment size), we can expect
sendandreceivenot to interact with one another at all, that is, the duration ofsendfollowed byreceiveshould be roughly the sum of durations ofsendandreceive, invoked independently. I imaginewrapper.channel.send(42)andblackhole.consume(wrapper.channel.receive())could stay in different benchmarks without affecting the results too much.For an empty channel, we could also try racing
sendandreceive.Using
runBlockingin a benchmark that's only doingsenddoesn't seem optimal to me, I can imagine the run time getting dominated by therunBlockingmachinery. I don't know what the proper way of doing this in JMH is, but I'd try a scheme like this:(haven't actually tested the code). Then, the scheme would be:
@fzhinkin , is there a standard mechanism that encapsulates this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running them in parallel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, if our goal is to measure a latency of a certain operation and we have facilities to do so, then it's better to do it directly (to an extent, benchmark's results are averages anyway). By doing so, we can ensure that all unnecessary setup and teardown code (like, creating a channel) won't skew results.
On the other hand, if the goal is to measure end-to-end latency, like "time to create a channel and send 100k messages over it", then sure, the current approach works for that (moreover, I don't see how to measure it otherwise).
See the comment, above. I was under the impression that the typical use case for channel is to be used indirectly (within a flow, for example), so for channels as they are we decided to measure a latency of a single operation to see how it will be affected by potential changes in the implementation.
I'm not saying that the way you're measuring it is invalid, but if there are facilities to measure latency of a single operation (well, the send-receive pair of operations), I'm voting for using it (unless there is an evidence that such a measurement is impossible or makes no sense).
Setup (and teardown) actions performed before (after) the whole run (or an individual iteration) should not affect measurements (as they are performed outside of the measurement scope); it will affect the measurements when performed for each benchmark function invocation.
I'm not sure if sending 400MB of data is a typical usage either. ;)
The benchmark function is continuously invoked over a configured period of time (you set it to 1 second).
If we reuse the same channel in each invocation, results will average over data structure amortization.
It's easier to focus on memory footprint as it is something we control directly (how many bytes we're allocating when performing an operation), rather than on GC pauses (they are a subject to various factors).
Both approaches look sane (assuming the wrapper is not recreated for every benchmark call) and we can do both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dkhalanskyjb, it feels like I didn't get you, but nevertheless: JMH provides some facilities to running benchmark methods concurrently and synchronize their execution:
https://github.com/openjdk/jmh/blob/master/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_15_Asymmetric.java
https://github.com/openjdk/jmh/blob/master/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_17_SyncIterations.java
As of
runBlocking, it would be nice to have a kx-benchmarks maintainer here, who would solve a problem with benchmarking suspend-API for us. Oh, wait... 😄