-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d83fb45
commit 83cbb9f
Showing
7 changed files
with
97 additions
and
2 deletions.
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
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,55 @@ | ||
using System.Collections.Concurrent; | ||
using BenchmarkDotNet.Attributes; | ||
using K4os.Async.Toys; | ||
|
||
namespace Benchmarks; | ||
|
||
public class BatchBuilder1 | ||
{ | ||
private const int Concurrency = 64; | ||
private readonly SemaphoreSlim _semaphore = new(Concurrency); | ||
private const int OpCount = 10000; | ||
private readonly ConcurrentBag<int> _bag = new(); | ||
|
||
private readonly IBatchBuilder<int, int> _batchBuilder; | ||
|
||
public BatchBuilder1() | ||
{ | ||
_batchBuilder = BatchBuilder.Create<int, int, int>( | ||
request => request, | ||
response => response, | ||
NoOpN, | ||
new BatchBuilderSettings { BatchSize = 10, Concurrency = Concurrency }); | ||
} | ||
|
||
[Benchmark] | ||
public void Straight() | ||
{ | ||
Task.WhenAll(Enumerable.Range(0, OpCount).Select(NoOp1)).Wait(); | ||
} | ||
|
||
[Benchmark] | ||
public void AsBatch() | ||
{ | ||
Task.WhenAll(Enumerable.Range(0, OpCount).Select(_batchBuilder.Request)).Wait(); | ||
} | ||
|
||
private async Task<int[]> NoOpN(int[] requests) | ||
{ | ||
await _semaphore.WaitAsync(); | ||
await Delay(); | ||
foreach (var request in requests) _bag.Add(request); | ||
_semaphore.Release(); | ||
return requests; | ||
} | ||
|
||
private async Task NoOp1(int i) | ||
{ | ||
await _semaphore.WaitAsync(); | ||
await Delay(); | ||
_bag.Add(i); | ||
_semaphore.Release(); | ||
} | ||
|
||
private static Task Delay() => Task.Delay(1); | ||
} |
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,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\K4os.Async.Toys\K4os.Async.Toys.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,5 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
|
||
using BenchmarkDotNet.Running; | ||
|
||
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); |
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
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
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