|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using EnumerableAsyncProcessor.Extensions; |
| 3 | + |
| 4 | +namespace EnumerableAsyncProcessor.Benchmarks; |
| 5 | + |
| 6 | +[MemoryDiagnoser] |
| 7 | +public class ProcessorBenchmarks |
| 8 | +{ |
| 9 | + private const int Concurrency = 64; |
| 10 | + private int[] _items = null!; |
| 11 | + private Func<int, Task> _processItemAsync = null!; |
| 12 | + private Func<int, Task<int>> _transformItemAsync = null!; |
| 13 | + |
| 14 | + public enum SelectorWorkload |
| 15 | + { |
| 16 | + CompletedTask, |
| 17 | + TaskYield |
| 18 | + } |
| 19 | + |
| 20 | + [Params(1_000, 10_000, 100_000)] |
| 21 | + public int ItemCount { get; set; } |
| 22 | + |
| 23 | + [Params(SelectorWorkload.CompletedTask, SelectorWorkload.TaskYield)] |
| 24 | + public SelectorWorkload Workload { get; set; } |
| 25 | + |
| 26 | + [GlobalSetup] |
| 27 | + public void Setup() |
| 28 | + { |
| 29 | + _items = Enumerable.Range(0, ItemCount).ToArray(); |
| 30 | + _processItemAsync = Workload == SelectorWorkload.CompletedTask |
| 31 | + ? ProcessCompletedItemAsync |
| 32 | + : ProcessWithYieldAsync; |
| 33 | + _transformItemAsync = Workload == SelectorWorkload.CompletedTask |
| 34 | + ? TransformCompletedItemAsync |
| 35 | + : TransformWithYieldAsync; |
| 36 | + } |
| 37 | + |
| 38 | + [Benchmark(Baseline = true)] |
| 39 | + public async Task UnboundedParallel() |
| 40 | + { |
| 41 | + await using var processor = _items |
| 42 | + .ForEachAsync(_processItemAsync) |
| 43 | + .ProcessInParallel(); |
| 44 | + |
| 45 | + await processor.WaitAsync(); |
| 46 | + } |
| 47 | + |
| 48 | + [Benchmark] |
| 49 | + public async Task ThrottledParallel() |
| 50 | + { |
| 51 | + await using var processor = _items |
| 52 | + .ForEachAsync(_processItemAsync) |
| 53 | + .ProcessInParallel(maxConcurrency: Concurrency); |
| 54 | + |
| 55 | + await processor.WaitAsync(); |
| 56 | + } |
| 57 | + |
| 58 | + [Benchmark] |
| 59 | + public async Task RateLimitedParallel() |
| 60 | + { |
| 61 | + await using var processor = _items |
| 62 | + .ForEachAsync(_processItemAsync) |
| 63 | + .ProcessInParallel(Concurrency); |
| 64 | + |
| 65 | + await processor.WaitAsync(); |
| 66 | + } |
| 67 | + |
| 68 | + [Benchmark] |
| 69 | + public async Task TimedRateLimitedParallel() |
| 70 | + { |
| 71 | + await using var processor = _items |
| 72 | + .ForEachAsync(_processItemAsync) |
| 73 | + .ProcessInParallel(Concurrency, TimeSpan.Zero); |
| 74 | + |
| 75 | + await processor.WaitAsync(); |
| 76 | + } |
| 77 | + |
| 78 | + [Benchmark] |
| 79 | + public async Task Batch() |
| 80 | + { |
| 81 | + await using var processor = _items |
| 82 | + .ForEachAsync(_processItemAsync) |
| 83 | + .ProcessInBatches(Concurrency); |
| 84 | + |
| 85 | + await processor.WaitAsync(); |
| 86 | + } |
| 87 | + |
| 88 | + [Benchmark] |
| 89 | + public async Task OneAtATime() |
| 90 | + { |
| 91 | + await using var processor = _items |
| 92 | + .ForEachAsync(_processItemAsync) |
| 93 | + .ProcessOneAtATime(); |
| 94 | + |
| 95 | + await processor.WaitAsync(); |
| 96 | + } |
| 97 | + |
| 98 | + [Benchmark] |
| 99 | + public async Task<int> ResultStreaming() |
| 100 | + { |
| 101 | + await using var processor = _items |
| 102 | + .SelectAsync(_transformItemAsync) |
| 103 | + .ProcessInParallel(maxConcurrency: Concurrency); |
| 104 | + |
| 105 | + var checksum = 0; |
| 106 | + await foreach (var result in processor.GetResultsAsyncEnumerable()) |
| 107 | + { |
| 108 | + checksum = unchecked(checksum + result); |
| 109 | + } |
| 110 | + |
| 111 | + return checksum; |
| 112 | + } |
| 113 | + |
| 114 | + private static Task ProcessCompletedItemAsync(int _) |
| 115 | + { |
| 116 | + return Task.CompletedTask; |
| 117 | + } |
| 118 | + |
| 119 | + private static Task<int> TransformCompletedItemAsync(int item) |
| 120 | + { |
| 121 | + return Task.FromResult(item); |
| 122 | + } |
| 123 | + |
| 124 | + private static async Task ProcessWithYieldAsync(int _) |
| 125 | + { |
| 126 | + await Task.Yield(); |
| 127 | + } |
| 128 | + |
| 129 | + private static async Task<int> TransformWithYieldAsync(int item) |
| 130 | + { |
| 131 | + await Task.Yield(); |
| 132 | + return item; |
| 133 | + } |
| 134 | +} |
0 commit comments