-
Notifications
You must be signed in to change notification settings - Fork 272
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
Port Channels benchmarks #79
Merged
+122
−0
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/benchmarks/corefx/System.Threading.Channels/Perf.Channel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using Benchmarks; | ||
|
||
namespace System.Threading.Channels.Tests | ||
{ | ||
public class UnboundedChannelPerfTests : PerfTests | ||
{ | ||
public sealed override Channel<int> CreateChannel() => Channel.CreateUnbounded<int>(); | ||
} | ||
|
||
public class SpscUnboundedChannelPerfTests : PerfTests | ||
{ | ||
public sealed override Channel<int> CreateChannel() => Channel.CreateUnbounded<int>(new UnboundedChannelOptions { SingleReader = true, SingleWriter = true }); | ||
} | ||
|
||
public class BoundedChannelPerfTests : PerfTests | ||
{ | ||
public sealed override Channel<int> CreateChannel() => Channel.CreateBounded<int>(10); | ||
} | ||
|
||
[BenchmarkCategory(Categories.CoreFX)] | ||
public abstract class PerfTests | ||
{ | ||
private Channel<int> _channel, _channel1, _channel2; | ||
private ChannelReader<int> _reader; | ||
private ChannelWriter<int> _writer; | ||
|
||
public abstract Channel<int> CreateChannel(); | ||
|
||
[GlobalSetup(Target = nameof(TryWriteThenTryRead) + "," + nameof(WriteAsyncThenReadAsync) + "," + nameof(ReadAsyncThenWriteAsync))] | ||
public void SingleChannelSetup() | ||
{ | ||
_channel = CreateChannel(); | ||
_reader = _channel.Reader; | ||
_writer = _channel.Writer; | ||
} | ||
|
||
[Benchmark] | ||
public void TryWriteThenTryRead() | ||
{ | ||
ChannelReader<int> reader = _reader; | ||
ChannelWriter<int> writer = _writer; | ||
|
||
for (int i = 0; i < 1_000_000; i++) | ||
{ | ||
writer.TryWrite(i); | ||
reader.TryRead(out _); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public async Task WriteAsyncThenReadAsync() | ||
{ | ||
ChannelReader<int> reader = _reader; | ||
ChannelWriter<int> writer = _writer; | ||
|
||
for (int i = 0; i < 1_000_000; i++) | ||
{ | ||
await writer.WriteAsync(i); | ||
await reader.ReadAsync(); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public async Task ReadAsyncThenWriteAsync() | ||
{ | ||
ChannelReader<int> reader = _reader; | ||
ChannelWriter<int> writer = _writer; | ||
|
||
for (int i = 0; i < 1_000_000; i++) | ||
{ | ||
ValueTask<int> r = reader.ReadAsync(); | ||
await writer.WriteAsync(42); | ||
await r; | ||
} | ||
} | ||
|
||
[GlobalSetup(Target = nameof(PingPong))] | ||
public void SetupTwoChannels() | ||
{ | ||
_channel1 = CreateChannel(); | ||
_channel2 = CreateChannel(); | ||
} | ||
|
||
[Benchmark] | ||
public async Task PingPong() | ||
{ | ||
Channel<int> channel1 = _channel1; | ||
Channel<int> channel2 = _channel2; | ||
|
||
await Task.WhenAll( | ||
Task.Run(async () => | ||
{ | ||
ChannelReader<int> reader = channel1.Reader; | ||
ChannelWriter<int> writer = channel2.Writer; | ||
for (int i = 0; i < 1_000_000; i++) | ||
{ | ||
await writer.WriteAsync(i); | ||
await reader.ReadAsync(); | ||
} | ||
}), | ||
Task.Run(async () => | ||
{ | ||
ChannelWriter<int> writer = channel1.Writer; | ||
ChannelReader<int> reader = channel2.Reader; | ||
for (int i = 0; i < 1_000_000; i++) | ||
{ | ||
await reader.ReadAsync(); | ||
await writer.WriteAsync(i); | ||
} | ||
})); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This
magic number
is in 6 different places in the source file. Could we make it aconst int
?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.
@jorive done!