-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add reproducible BenchmarkDotNet suite #353
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
37 changes: 37 additions & 0 deletions
37
EnumerableAsyncProcessor.Pipeline/Modules/BuildBenchmarkProjectsModule.cs
This file contains hidden or 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,37 @@ | ||
| using ModularPipelines.Context; | ||
| using ModularPipelines.DotNet.Extensions; | ||
| using ModularPipelines.DotNet.Options; | ||
| using ModularPipelines.Git.Extensions; | ||
| using ModularPipelines.Models; | ||
| using ModularPipelines.Modules; | ||
| using ModularPipelines.Options; | ||
|
|
||
| namespace EnumerableAsyncProcessor.Pipeline.Modules; | ||
|
|
||
| public class BuildBenchmarkProjectsModule : Module<List<CommandResult>> | ||
| { | ||
| protected override async Task<List<CommandResult>?> ExecuteAsync( | ||
| IModuleContext context, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var results = new List<CommandResult>(); | ||
| var executionOptions = new CommandExecutionOptions | ||
| { | ||
| ThrowOnNonZeroExitCode = true, | ||
| }; | ||
|
|
||
| foreach (var benchmarkProjectFile in context | ||
| .Git().RootDirectory | ||
| .GetFiles(file => file.Path.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) | ||
| && file.Path.Contains("Benchmarks", StringComparison.OrdinalIgnoreCase))) | ||
| { | ||
| results.Add(await context.DotNet().Build(new DotNetBuildOptions | ||
| { | ||
| ProjectSolution = benchmarkProjectFile.Path, | ||
| Configuration = "Release", | ||
| }, executionOptions: executionOptions, cancellationToken: cancellationToken).ConfigureAwait(false)); | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\EnumerableAsyncProcessor\EnumerableAsyncProcessor.csproj" /> | ||
| <PackageReference Include="BenchmarkDotNet" Version="0.15.8" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
This file contains hidden or 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,134 @@ | ||
| using BenchmarkDotNet.Attributes; | ||
| using EnumerableAsyncProcessor.Extensions; | ||
|
|
||
| namespace EnumerableAsyncProcessor.Benchmarks; | ||
|
|
||
| [MemoryDiagnoser] | ||
| public class ProcessorBenchmarks | ||
| { | ||
| private const int Concurrency = 64; | ||
| private int[] _items = null!; | ||
| private Func<int, Task> _processItemAsync = null!; | ||
| private Func<int, Task<int>> _transformItemAsync = null!; | ||
|
|
||
| public enum SelectorWorkload | ||
| { | ||
| CompletedTask, | ||
| TaskYield | ||
| } | ||
|
|
||
| [Params(1_000, 10_000, 100_000)] | ||
| public int ItemCount { get; set; } | ||
|
|
||
| [Params(SelectorWorkload.CompletedTask, SelectorWorkload.TaskYield)] | ||
| public SelectorWorkload Workload { get; set; } | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| _items = Enumerable.Range(0, ItemCount).ToArray(); | ||
| _processItemAsync = Workload == SelectorWorkload.CompletedTask | ||
| ? ProcessCompletedItemAsync | ||
| : ProcessWithYieldAsync; | ||
| _transformItemAsync = Workload == SelectorWorkload.CompletedTask | ||
| ? TransformCompletedItemAsync | ||
| : TransformWithYieldAsync; | ||
| } | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public async Task UnboundedParallel() | ||
| { | ||
| await using var processor = _items | ||
| .ForEachAsync(_processItemAsync) | ||
| .ProcessInParallel(); | ||
|
|
||
| await processor.WaitAsync(); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task ThrottledParallel() | ||
| { | ||
| await using var processor = _items | ||
| .ForEachAsync(_processItemAsync) | ||
| .ProcessInParallel(maxConcurrency: Concurrency); | ||
|
|
||
| await processor.WaitAsync(); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task RateLimitedParallel() | ||
| { | ||
| await using var processor = _items | ||
| .ForEachAsync(_processItemAsync) | ||
| .ProcessInParallel(Concurrency); | ||
|
|
||
| await processor.WaitAsync(); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task TimedRateLimitedParallel() | ||
| { | ||
| await using var processor = _items | ||
| .ForEachAsync(_processItemAsync) | ||
| .ProcessInParallel(Concurrency, TimeSpan.Zero); | ||
|
|
||
| await processor.WaitAsync(); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task Batch() | ||
| { | ||
| await using var processor = _items | ||
| .ForEachAsync(_processItemAsync) | ||
| .ProcessInBatches(Concurrency); | ||
|
|
||
| await processor.WaitAsync(); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task OneAtATime() | ||
| { | ||
| await using var processor = _items | ||
| .ForEachAsync(_processItemAsync) | ||
| .ProcessOneAtATime(); | ||
|
|
||
| await processor.WaitAsync(); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task<int> ResultStreaming() | ||
| { | ||
| await using var processor = _items | ||
| .SelectAsync(_transformItemAsync) | ||
| .ProcessInParallel(maxConcurrency: Concurrency); | ||
|
|
||
| var checksum = 0; | ||
| await foreach (var result in processor.GetResultsAsyncEnumerable()) | ||
| { | ||
| checksum = unchecked(checksum + result); | ||
| } | ||
|
|
||
| return checksum; | ||
| } | ||
|
|
||
| private static Task ProcessCompletedItemAsync(int _) | ||
| { | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| private static Task<int> TransformCompletedItemAsync(int item) | ||
| { | ||
| return Task.FromResult(item); | ||
| } | ||
|
|
||
| private static async Task ProcessWithYieldAsync(int _) | ||
| { | ||
| await Task.Yield(); | ||
| } | ||
|
|
||
| private static async Task<int> TransformWithYieldAsync(int item) | ||
| { | ||
| await Task.Yield(); | ||
| return item; | ||
| } | ||
| } |
This file contains hidden or 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,3 @@ | ||
| using BenchmarkDotNet.Running; | ||
|
|
||
| BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); |
This file contains hidden or 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,26 @@ | ||
| # Benchmarks | ||
|
|
||
| The suite measures processor coordination overhead and allocations with completed-task and `Task.Yield` selectors at 1,000, 10,000, and 100,000 items. | ||
| The timed scenario uses a zero-duration window so the results isolate coordination overhead rather than wall-clock waiting. | ||
|
|
||
| Run every benchmark from the repository root: | ||
|
|
||
| ```shell | ||
| dotnet run -c Release --project benchmarks | ||
| ``` | ||
|
|
||
| `BenchmarkSwitcher` forwards BenchmarkDotNet command-line options. List or filter cases before a focused comparison: | ||
|
|
||
| ```shell | ||
| dotnet run -c Release --project benchmarks -- --list flat | ||
| dotnet run -c Release --project benchmarks -- --filter "*ThrottledParallel*" | ||
| ``` | ||
|
|
||
| `UnboundedParallel` is the in-process baseline for ratio columns. To compare code revisions, run the same filter in separate clean worktrees and give each run a distinct artifacts directory: | ||
|
|
||
| ```shell | ||
| dotnet run -c Release --project benchmarks -- --filter "*ResultStreaming*" --artifacts artifacts/baseline | ||
| dotnet run -c Release --project benchmarks -- --filter "*ResultStreaming*" --artifacts artifacts/candidate | ||
| ``` | ||
|
|
||
| Use Release builds on the same idle machine and compare the generated Markdown or CSV reports under each artifacts directory. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.