|
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | 5 | using System.Runtime.CompilerServices; |
| 6 | +using System.Threading.Tasks.Sources; |
| 7 | +using System.Threading.Tasks.Tests; |
6 | 8 | using Microsoft.Xunit.Performance; |
7 | 9 | using Xunit; |
8 | 10 |
|
@@ -44,6 +46,23 @@ public async Task Await_FromCompletedTask() |
44 | 46 | } |
45 | 47 | } |
46 | 48 |
|
| 49 | + [Benchmark(InnerIterationCount = 10_000_000), MeasureGCAllocations] |
| 50 | + public async Task Await_FromCompletedValueTaskSource() |
| 51 | + { |
| 52 | + ValueTask<int> vt = new ValueTask<int>(ManualResetValueTaskSource.Completed<int>(42), 0); |
| 53 | + foreach (BenchmarkIteration iteration in Benchmark.Iterations) |
| 54 | + { |
| 55 | + long iters = Benchmark.InnerIterationCount; |
| 56 | + using (iteration.StartMeasurement()) |
| 57 | + { |
| 58 | + for (long i = 0; i < iters; i++) |
| 59 | + { |
| 60 | + await vt; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
47 | 66 | [Benchmark(InnerIterationCount = 10_000_000), MeasureGCAllocations] |
48 | 67 | public async Task CreateAndAwait_FromResult() |
49 | 68 | { |
@@ -110,6 +129,40 @@ public async Task CreateAndAwait_FromCompletedTask_ConfigureAwait() |
110 | 129 | } |
111 | 130 | } |
112 | 131 |
|
| 132 | + [Benchmark(InnerIterationCount = 10_000_000), MeasureGCAllocations] |
| 133 | + public async Task CreateAndAwait_FromCompletedValueTaskSource() |
| 134 | + { |
| 135 | + IValueTaskSource<int> vts = ManualResetValueTaskSource.Completed(42); |
| 136 | + foreach (BenchmarkIteration iteration in Benchmark.Iterations) |
| 137 | + { |
| 138 | + long iters = Benchmark.InnerIterationCount; |
| 139 | + using (iteration.StartMeasurement()) |
| 140 | + { |
| 141 | + for (long i = 0; i < iters; i++) |
| 142 | + { |
| 143 | + await new ValueTask<int>(vts, 0); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + [Benchmark(InnerIterationCount = 10_000_000), MeasureGCAllocations] |
| 150 | + public async Task CreateAndAwait_FromCompletedValueTaskSource_ConfigureAwait() |
| 151 | + { |
| 152 | + IValueTaskSource<int> vts = ManualResetValueTaskSource.Completed(42); |
| 153 | + foreach (BenchmarkIteration iteration in Benchmark.Iterations) |
| 154 | + { |
| 155 | + long iters = Benchmark.InnerIterationCount; |
| 156 | + using (iteration.StartMeasurement()) |
| 157 | + { |
| 158 | + for (long i = 0; i < iters; i++) |
| 159 | + { |
| 160 | + await new ValueTask<int>(vts, 0).ConfigureAwait(false); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
113 | 166 | [Benchmark(InnerIterationCount = 1_000_000), MeasureGCAllocations] |
114 | 167 | public async Task CreateAndAwait_FromYieldingAsyncMethod() |
115 | 168 | { |
@@ -179,6 +232,23 @@ public void Copy_PassAsArgumentAndReturn_FromTask() |
179 | 232 | } |
180 | 233 | } |
181 | 234 |
|
| 235 | + [Benchmark(InnerIterationCount = 10_000_000), MeasureGCAllocations] |
| 236 | + public void Copy_PassAsArgumentAndReturn_FromValueTaskSource() |
| 237 | + { |
| 238 | + ValueTask<int> vt = new ValueTask<int>(ManualResetValueTaskSource.Completed(42), 0); |
| 239 | + foreach (BenchmarkIteration iteration in Benchmark.Iterations) |
| 240 | + { |
| 241 | + long iters = Benchmark.InnerIterationCount; |
| 242 | + using (iteration.StartMeasurement()) |
| 243 | + { |
| 244 | + for (long i = 0; i < iters; i++) |
| 245 | + { |
| 246 | + vt = ReturnValueTask(vt); |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + |
182 | 252 | [MethodImpl(MethodImplOptions.NoInlining)] |
183 | 253 | private static ValueTask<int> ReturnValueTask(ValueTask<int> vt) => vt; |
184 | 254 |
|
|
0 commit comments