Skip to content

Commit 12617b2

Browse files
committed
Fixed nullability warnings for benchmark project
1 parent 943f68b commit 12617b2

File tree

8 files changed

+29
-39
lines changed

8 files changed

+29
-39
lines changed

src/Benchmarks/AutoLoggerMessageGenerator.Benchmarks/BenchmarkFiles/CustomNullLogger.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using System;
21
using Microsoft.Extensions.Logging;
32

43
public class CustomNullLogger<T> : ILogger
54
{
65
private int _counter = 0;
76
public static readonly CustomNullLogger<T> Instance = new();
87

9-
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
8+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception, string> formatter)
109
{
1110
_counter++;
1211
}
@@ -15,7 +14,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
1514

1615
public IDisposable BeginScope<TState>(TState state) where TState : notnull =>
1716
NullScope.Instance;
18-
17+
1918
private sealed class NullScope : IDisposable
2019
{
2120
public static NullScope Instance { get; } = new();

src/Benchmarks/AutoLoggerMessageGenerator.Benchmarks/BenchmarkFiles/ExecutionTimeBenchmark.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public partial class ExecutionTimeBenchmark
99
{
1010
private ILogger _logger = null!;
11-
private ConfigurationExample _configuration;
11+
private ConfigurationExample _configuration = null!;
1212

1313
[GlobalSetup]
1414
public void Setup()
@@ -126,5 +126,5 @@ [LogProperties] ConfigurationExample arg6
126126

127127
#endif
128128

129-
public record ConfigurationExample(int Id, string Name, ConfigurationExample NestedConfiguration);
129+
public record ConfigurationExample(int Id, string Name, ConfigurationExample? NestedConfiguration);
130130
}

src/Benchmarks/AutoLoggerMessageGenerator.Benchmarks/CommandRunner.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
using System;
21
using System.Diagnostics;
3-
using System.Threading;
4-
using System.Threading.Tasks;
52

63
namespace AutoLoggerMessageGenerator.Benchmarks;
74

85
internal class CommandRunner(ProcessPriorityClass priorityClass)
96
{
10-
public async Task RunAsync(string command, string args = null, CancellationToken cancellationToken = default)
7+
public async Task RunAsync(string command, string? args = null, CancellationToken cancellationToken = default)
118
{
129
var process = new Process();
1310

14-
process.StartInfo = new ProcessStartInfo(command, args)
11+
process.StartInfo = new ProcessStartInfo(command, args ?? string.Empty)
1512
{
1613
UseShellExecute = false,
1714
CreateNoWindow = true,

src/Benchmarks/AutoLoggerMessageGenerator.Benchmarks/MultiBenchmarkRunner.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
51
using System.Reflection;
6-
using System.Threading.Tasks;
72
using BenchmarkDotNet.Attributes;
83
using BenchmarkDotNet.Configs;
94
using BenchmarkDotNet.Exporters;
@@ -12,15 +7,15 @@
127

138
namespace AutoLoggerMessageGenerator.Benchmarks;
149

15-
internal class MultiBenchmarkRunner(ProjectConfiguration[] projectConfigurations, string[] args = null)
10+
internal class MultiBenchmarkRunner(ProjectConfiguration[] projectConfigurations, string[]? args = null)
1611
{
1712
private static readonly ManualConfig RunConfiguration = ManualConfig.CreateMinimumViable()
1813
.WithOptions(ConfigOptions.JoinSummary)
1914
.WithOptions(ConfigOptions.DisableLogFile)
2015
.AddColumn(new ProjectConfigurationColumn())
2116
.WithOrderer(new DefaultOrderer(SummaryOrderPolicy.Declared))
2217
.AddExporter(MarkdownExporter.GitHub);
23-
18+
2419
public async Task Run()
2520
{
2621
var buildResults = await BuildProjects();
@@ -30,7 +25,7 @@ public async Task Run()
3025

3126
CleanupGeneratedProjects(buildResults);
3227
}
33-
28+
3429
private async Task<LinkedList<ProjectBuilder.BuildResult>> BuildProjects()
3530
{
3631
if (!projectConfigurations.Any())
@@ -45,7 +40,7 @@ public async Task Run()
4540

4641
return buildResults;
4742
}
48-
43+
4944
private static TypeInfo[] FindAllBenchmarks(LinkedList<ProjectBuilder.BuildResult> buildResults)
5045
{
5146
var types = buildResults.Select(b => Assembly.LoadFrom(b.ExecutablePath))
@@ -54,7 +49,7 @@ private static TypeInfo[] FindAllBenchmarks(LinkedList<ProjectBuilder.BuildResul
5449
.ToArray();
5550
return types;
5651
}
57-
52+
5853
private static void CleanupGeneratedProjects(LinkedList<ProjectBuilder.BuildResult> buildResults)
5954
{
6055
foreach (var buildResult in buildResults)

src/Benchmarks/AutoLoggerMessageGenerator.Benchmarks/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections.Generic;
21
using AutoLoggerMessageGenerator.Benchmarks;
32

43
ProjectConfiguration[] projectConfigurations =

src/Benchmarks/AutoLoggerMessageGenerator.Benchmarks/ProjectBuilder.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
21
using System.Diagnostics;
3-
using System.IO;
4-
using System.Threading.Tasks;
52

63
namespace AutoLoggerMessageGenerator.Benchmarks;
74

@@ -12,7 +9,7 @@ public async Task<BuildResult> BuildAsync()
129
var baseDirectory = AppContext.BaseDirectory;
1310

1411
var workingDirectory = Path.Join(baseDirectory, projectConfiguration.Name);
15-
12+
1613
CreateWorkingDirectory(workingDirectory);
1714
await GenerateEntryPoint(workingDirectory);
1815
CopyBenchmarkFiles(workingDirectory);
@@ -28,15 +25,15 @@ public async Task<BuildResult> BuildAsync()
2825
ExecutablePath = Path.Join(workingDirectory, outputFolder, $"{projectConfiguration.Name}.dll"),
2926
};
3027
}
31-
28+
3229
private static void CreateWorkingDirectory(string workingDirectory)
3330
{
3431
if (Directory.Exists(workingDirectory))
3532
Directory.Delete(workingDirectory, recursive: true);
3633

3734
Directory.CreateDirectory(workingDirectory);
3835
}
39-
36+
4037
private static Task GenerateEntryPoint(string workingDirectory)
4138
{
4239
const string mainEntryPoint = """
@@ -48,25 +45,25 @@ public static void Main() {}
4845

4946
return File.WriteAllTextAsync(Path.Join(workingDirectory, "Program.cs"), mainEntryPoint);
5047
}
51-
48+
5249
private static void CopyBenchmarkFiles(string workingDirectory)
5350
{
5451
var benchmarkFiles = $"{AppContext.BaseDirectory}/BenchmarkFiles";
5552
foreach (string newPath in Directory.GetFiles(benchmarkFiles, "*.*",SearchOption.AllDirectories))
5653
File.Copy(newPath, newPath.Replace(benchmarkFiles, workingDirectory), true);
5754
}
58-
55+
5956
private async Task<(string OutputFolder, string projFilePath)> GenerateProjectFile(string projectName, string workingDirectory)
6057
{
6158
var telemetryConstant = projectConfiguration.References.Contains(PackagesProvider.MicrosoftExtensionsTelemetryPackage)
6259
? "TELEMETRY"
6360
: string.Empty;
6461

6562
var targetFramework = TargetFrameworkMonikerDetector.Detect();
66-
63+
6764
string outputFolder = Path.Join("bin", "output");
6865
const string interceptorNamespace = Constants.GeneratorNamespace;
69-
66+
7067
string projFileContent = $"""
7168
<Project Sdk="Microsoft.NET.Sdk">
7269
<PropertyGroup>
@@ -79,7 +76,7 @@ private static void CopyBenchmarkFiles(string workingDirectory)
7976
<RootNamespace>{projectName}</RootNamespace>
8077
<DefineConstants>$(DefineConstants);{telemetryConstant}</DefineConstants>
8178
</PropertyGroup>
82-
79+
8380
<PropertyGroup>
8481
<PlatformTarget>AnyCPU</PlatformTarget>
8582
<DebugType>pdbonly</DebugType>
@@ -89,7 +86,7 @@ private static void CopyBenchmarkFiles(string workingDirectory)
8986
<Configuration>Release</Configuration>
9087
<IsPackable>false</IsPackable>
9188
</PropertyGroup>
92-
89+
9390
<ItemGroup>
9491
{string.Join(Environment.NewLine, projectConfiguration.References)}
9592
</ItemGroup>
@@ -104,7 +101,7 @@ private static void CopyBenchmarkFiles(string workingDirectory)
104101

105102
internal class BuildResult
106103
{
107-
public string ProjectDirectory { get; init; }
108-
public string ExecutablePath { get; init; }
104+
public required string ProjectDirectory { get; init; }
105+
public required string ExecutablePath { get; init; }
109106
}
110107
}

src/Benchmarks/AutoLoggerMessageGenerator.Benchmarks/ProjectConfiguration.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Frozen;
2-
using System.Collections.Generic;
32

43
namespace AutoLoggerMessageGenerator.Benchmarks;
54

src/Benchmarks/AutoLoggerMessageGenerator.Benchmarks/TargetFrameworkMonikerDetector.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Reflection;
32
using System.Runtime.Versioning;
43

@@ -12,6 +11,9 @@ public static string Detect()
1211
.GetCustomAttribute<TargetFrameworkAttribute>()?
1312
.FrameworkName;
1413

14+
if (string.IsNullOrEmpty(frameworkName))
15+
throw new InvalidOperationException("Failed to detect target framework moniker");
16+
1517
return frameworkName switch
1618
{
1719
".NETFramework,Version=v4.6.1" => "net461",
@@ -31,8 +33,10 @@ public static string Detect()
3133
".NETCoreApp,Version=v7.0" => "net7.0",
3234
".NETCoreApp,Version=v8.0" => "net8.0",
3335
".NETCoreApp,Version=v9.0" => "net9.0",
34-
_ when frameworkName.StartsWith(".NETFramework") => frameworkName.Replace(".NETFramework,Version=v", string.Empty).Replace(".", string.Empty),
35-
_ when frameworkName.StartsWith(".NETCoreApp") => frameworkName.Replace(".NETCoreApp,Version=v", string.Empty),
36+
_ when frameworkName.StartsWith(".NETFramework") =>
37+
frameworkName.Replace(".NETFramework,Version=v", string.Empty).Replace(".", string.Empty),
38+
_ when frameworkName.StartsWith(".NETCoreApp") =>
39+
frameworkName.Replace(".NETCoreApp,Version=v", string.Empty),
3640
_ => throw new NotSupportedException($"Unsupported framework: {frameworkName}")
3741
};
3842
}

0 commit comments

Comments
 (0)