Skip to content
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
merged 4 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.0" />
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
<PackageReference Include="System.Runtime.Serialization.Json" Version="4.3.0" />
<PackageReference Include="System.Threading.Channels" Version="4.5.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.1" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
Expand Down
119 changes: 119 additions & 0 deletions src/benchmarks/corefx/System.Threading.Channels/Perf.Channel.cs
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++)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1_000_000 [](start = 32, length = 9)

This magic number is in 6 different places in the source file. Could we make it a const int?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorive done!

{
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);
}
}));
}
}
}