Skip to content

Commit 2f4b794

Browse files
Improve AsyncBenchmarksTests.TaskReturningMethodsAreAwaited
1 parent 77b58de commit 2f4b794

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

tests/BenchmarkDotNet.IntegrationTests/AsyncBenchmarksTests.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@ namespace BenchmarkDotNet.IntegrationTests
77
{
88
public class AsyncBenchmarksTests : BenchmarkTestExecutor
99
{
10-
public AsyncBenchmarksTests(ITestOutputHelper output) : base(output)
11-
{
12-
}
10+
public AsyncBenchmarksTests(ITestOutputHelper output) : base(output) { }
1311

1412
[Fact]
1513
public void TaskReturningMethodsAreAwaited()
1614
{
1715
var summary = CanExecute<TaskDelayMethods>();
1816

1917
foreach (var report in summary.Reports)
18+
foreach (var measurement in report.AllMeasurements)
2019
{
21-
foreach (var measurement in report.AllMeasurements)
22-
{
23-
Assert.True(measurement.Nanoseconds * 1.03 > TaskDelayMethods.NanosecondsDelay,
24-
$"{report.BenchmarkCase.Descriptor.GetFilterName()} has not been awaited, took {measurement.Nanoseconds}ns, while it should take more than {TaskDelayMethods.NanosecondsDelay}ns");
25-
}
20+
double actual = measurement.Nanoseconds;
21+
const double minExpected = TaskDelayMethods.NanosecondsDelay - TaskDelayMethods.MaxTaskDelayResolutionInNanoseconds;
22+
string name = report.BenchmarkCase.Descriptor.GetFilterName();
23+
Assert.True(actual > minExpected, $"{name} has not been awaited, took {actual}ns, while it should take more than {minExpected}ns");
2624
}
2725
}
2826

@@ -32,6 +30,9 @@ public class TaskDelayMethods
3230

3331
internal const double NanosecondsDelay = MillisecondsDelay * 1e+6;
3432

33+
// The default frequency of the Windows System Timer is 64Hz, so the Task.Delay error is up to 15.625ms.
34+
internal const int MaxTaskDelayResolutionInNanoseconds = 1_000_000_000 / 64;
35+
3536
[Benchmark]
3637
public Task ReturningTask() => Task.Delay(MillisecondsDelay);
3738

0 commit comments

Comments
 (0)