Skip to content

Commit

Permalink
Improve AsyncBenchmarksTests.TaskReturningMethodsAreAwaited
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed May 19, 2021
1 parent 77b58de commit 2f4b794
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions tests/BenchmarkDotNet.IntegrationTests/AsyncBenchmarksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ namespace BenchmarkDotNet.IntegrationTests
{
public class AsyncBenchmarksTests : BenchmarkTestExecutor
{
public AsyncBenchmarksTests(ITestOutputHelper output) : base(output)
{
}
public AsyncBenchmarksTests(ITestOutputHelper output) : base(output) { }

[Fact]
public void TaskReturningMethodsAreAwaited()
{
var summary = CanExecute<TaskDelayMethods>();

foreach (var report in summary.Reports)
foreach (var measurement in report.AllMeasurements)
{
foreach (var measurement in report.AllMeasurements)
{
Assert.True(measurement.Nanoseconds * 1.03 > TaskDelayMethods.NanosecondsDelay,
$"{report.BenchmarkCase.Descriptor.GetFilterName()} has not been awaited, took {measurement.Nanoseconds}ns, while it should take more than {TaskDelayMethods.NanosecondsDelay}ns");
}
double actual = measurement.Nanoseconds;
const double minExpected = TaskDelayMethods.NanosecondsDelay - TaskDelayMethods.MaxTaskDelayResolutionInNanoseconds;
string name = report.BenchmarkCase.Descriptor.GetFilterName();
Assert.True(actual > minExpected, $"{name} has not been awaited, took {actual}ns, while it should take more than {minExpected}ns");
}
}

Expand All @@ -32,6 +30,9 @@ public class TaskDelayMethods

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

// The default frequency of the Windows System Timer is 64Hz, so the Task.Delay error is up to 15.625ms.
internal const int MaxTaskDelayResolutionInNanoseconds = 1_000_000_000 / 64;

[Benchmark]
public Task ReturningTask() => Task.Delay(MillisecondsDelay);

Expand Down

0 comments on commit 2f4b794

Please sign in to comment.