From 812eeeb55a0d3962010bd7804ddf00dcac73dc92 Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Thu, 29 Aug 2024 15:45:38 +0700 Subject: [PATCH] disable task wait warnings in benchmark code --- .../DataModelPerformanceTests.cs | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs b/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs index eccfd90..48fb253 100644 --- a/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs +++ b/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs @@ -60,7 +60,7 @@ private void StopTrace() private static async Task MeasureTime(Func action, int iterations = 10) { - TimeSpan total = TimeSpan.Zero; + var total = TimeSpan.Zero; for (var i = 0; i < iterations; i++) { var start = Stopwatch.GetTimestamp(); @@ -74,7 +74,7 @@ private static async Task MeasureTime(Func action, int iteration public async Task SimpleAddChangePerformanceTest() { //disable validation because it's slow - var dataModelTest = new DataModelTestBase(false, alwaysValidate: false); + var dataModelTest = new DataModelTestBase(alwaysValidate: false); // warmup the code, this causes jit to run and keeps our actual test below consistent await dataModelTest.WriteNextChange(dataModelTest.SetWord(Guid.NewGuid(), "entity 0")); var runtimeAddChange1Snapshot = await MeasureTime(() => dataModelTest.WriteNextChange(dataModelTest.SetWord(Guid.NewGuid(), "entity 1")).AsTask()); @@ -131,24 +131,21 @@ private class XUnitBenchmarkLogger(ITestOutputHelper output) : ILogger { public string Id => nameof(XUnitBenchmarkLogger); public int Priority => 0; - private StringBuilder? sb; + private StringBuilder? _sb; public void Write(LogKind logKind, string text) { - if (sb == null) - { - sb = new StringBuilder(); - } + _sb ??= new StringBuilder(); - sb.Append(text); + _sb.Append(text); } public void WriteLine() { - if (sb is not null) + if (_sb is not null) { - output.WriteLine(sb.ToString()); - sb.Clear(); + output.WriteLine(_sb.ToString()); + _sb.Clear(); } else output.WriteLine(string.Empty); @@ -156,10 +153,10 @@ public void WriteLine() public void WriteLine(LogKind logKind, string text) { - if (sb is not null) + if (_sb is not null) { - output.WriteLine(sb.Append(text).ToString()); - sb.Clear(); + output.WriteLine(_sb.Append(text).ToString()); + _sb.Clear(); } else output.WriteLine(text); @@ -167,15 +164,17 @@ public void WriteLine(LogKind logKind, string text) public void Flush() { - if (sb is not null) + if (_sb is not null) { - output.WriteLine(sb.ToString()); - sb.Clear(); + output.WriteLine(_sb.ToString()); + _sb.Clear(); } } } } +// disable warning about waiting for sync code, benchmarkdotnet does not support async code, and it doesn't deadlock when waiting. +#pragma warning disable VSTHRD002 [SimpleJob(RunStrategy.Throughput, warmupCount: 2)] public class DataModelPerformanceBenchmarks { @@ -229,4 +228,5 @@ public void GlobalCleanup() { _templateModel.DisposeAsync().GetAwaiter().GetResult(); } -} \ No newline at end of file +} +#pragma warning restore VSTHRD002 \ No newline at end of file