Skip to content

Commit 52ceada

Browse files
feat: add source generator benchmarks (#4579)
1 parent 7a4a3de commit 52ceada

File tree

7 files changed

+145
-2
lines changed

7 files changed

+145
-2
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@
101101
<PackageVersion Include="xunit.v3.assert" Version="3.2.2" />
102102
<PackageVersion Include="xunit.v3.extensibility.core" Version="3.2.2" />
103103
</ItemGroup>
104-
</Project>
104+
</Project>

TUnit.Core.SourceGenerator.Roslyn47/TUnit.Core.SourceGenerator.Roslyn47.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
<Import Project="..\SourceGenerator.props" />
88

9-
</Project>
9+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using BenchmarkDotNet.Running;
2+
using Microsoft.Build.Locator;
3+
using TUnit.SourceGenerator.Benchmarks;
4+
5+
MSBuildLocator.RegisterDefaults();
6+
7+
if (args is { Length: > 0 })
8+
{
9+
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
10+
}
11+
else
12+
{
13+
BenchmarkRunner.Run<TestMetadataGeneratorBenchmarks>();
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" />
12+
<PackageReference Include="Microsoft.Build" VersionOverride="17.7.2" ExcludeAssets="runtime" PrivateAssets="all" />
13+
<PackageReference Include="Microsoft.Build.Locator" VersionOverride="1.7.1" />
14+
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" VersionOverride="5.0.0" />
15+
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" VersionOverride="5.0.0" />
16+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="all" VersionOverride="5.0.0" />
17+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" VersionOverride="5.0.0"/>
18+
<PackageReference Include="Microsoft.CodeAnalysis.Common" PrivateAssets="all" VersionOverride="5.0.0" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.csproj" />
23+
<ProjectReference Include="..\TUnit.Core\TUnit.Core.csproj" />
24+
</ItemGroup>
25+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using BenchmarkDotNet.Attributes;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.MSBuild;
4+
using TUnit.Core.SourceGenerator.Generators;
5+
using TUnit.SourceGenerator.Benchmarks;
6+
7+
namespace TUnit.SourceGenerator.Benchmarks;
8+
9+
[MemoryDiagnoser]
10+
[InProcess]
11+
public class TestMetadataGeneratorBenchmarks
12+
{
13+
private const string SampleProjectPath = "../TUnit.TestProject/TUnit.TestProject.csproj";
14+
15+
private MSBuildWorkspace? _workspace;
16+
private GeneratorDriver? _sampleDriver;
17+
private Compilation? _sampleCompilation;
18+
19+
[GlobalSetup(Target = nameof(Compile))]
20+
public void SetupCompile() =>
21+
(_sampleCompilation, _sampleDriver, _workspace) =
22+
WorkspaceHelper.SetupAsync<TestMetadataGenerator>(SampleProjectPath)
23+
.GetAwaiter()
24+
.GetResult();
25+
26+
[Benchmark]
27+
public GeneratorDriver Compile() => _sampleDriver!.RunGeneratorsAndUpdateCompilation(_sampleCompilation!, out _, out _);
28+
29+
[GlobalCleanup]
30+
public void Cleanup()
31+
{
32+
_workspace?.Dispose();
33+
}
34+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Runtime.CompilerServices;
2+
using BenchmarkDotNet.Loggers;
3+
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.CSharp;
5+
using Microsoft.CodeAnalysis.MSBuild;
6+
7+
namespace TUnit.SourceGenerator.Benchmarks;
8+
9+
public static class WorkspaceHelper
10+
{
11+
private static string GetDirectoryRelativePath(string projectPath, [CallerFilePath] string callerFilePath = null!) =>
12+
Path.Combine(Path.GetDirectoryName(callerFilePath)!, projectPath);
13+
14+
public static async Task<(Compilation, CSharpGeneratorDriver, MSBuildWorkspace)> SetupAsync<TSourceGenerator>( string projectPath)
15+
where TSourceGenerator : IIncrementalGenerator, new()
16+
{
17+
MSBuildWorkspace workspace = MSBuildWorkspace.Create();
18+
workspace.RegisterWorkspaceFailedHandler(args =>
19+
{
20+
ConsoleLogger.Default.WriteLineError("-------------------------");
21+
ConsoleLogger.Default.WriteLineError(args.Diagnostic.ToString());
22+
ConsoleLogger.Default.WriteLineError("-------------------------");
23+
});
24+
25+
var projectFile = GetDirectoryRelativePath(projectPath);
26+
27+
if (!File.Exists(projectFile))
28+
throw new Exception($"Project doesn't exist at {projectFile}");
29+
30+
ConsoleLogger.Default.WriteLine($"Project exists at {projectFile}");
31+
32+
Project project;
33+
try
34+
{
35+
ConsoleLogger.Default.WriteLine("Loading project\n");
36+
project = await workspace.OpenProjectAsync(projectFile);
37+
ConsoleLogger.Default.WriteLine("\nLoaded project");
38+
}
39+
catch (Exception ex)
40+
{
41+
ConsoleLogger.Default.WriteError($"Error: {ex.Message}");
42+
throw;
43+
}
44+
45+
var compilation = await project.GetCompilationAsync();
46+
if (compilation == null)
47+
throw new InvalidOperationException("Compilation returned null");
48+
49+
var generator = new TSourceGenerator().AsSourceGenerator();
50+
51+
var driver =
52+
CSharpGeneratorDriver.Create([generator], parseOptions: (CSharpParseOptions) project.ParseOptions!);
53+
54+
return (compilation, driver, workspace);
55+
}
56+
}

TUnit.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TUnit.FsCheck", "TUnit.FsCh
155155
EndProject
156156
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TUnit.Example.FsCheck.TestProject", "TUnit.Example.FsCheck.TestProject\TUnit.Example.FsCheck.TestProject.csproj", "{3428D7AD-B362-4647-B1B0-72674CF3BC7C}"
157157
EndProject
158+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TUnit.SourceGenerator.Benchmarks", "TUnit.SourceGenerator.Benchmarks\TUnit.SourceGenerator.Benchmarks.csproj", "{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}"
159+
EndProject
158160
Global
159161
GlobalSection(SolutionConfigurationPlatforms) = preSolution
160162
Debug|Any CPU = Debug|Any CPU
@@ -897,6 +899,18 @@ Global
897899
{3428D7AD-B362-4647-B1B0-72674CF3BC7C}.Release|x64.Build.0 = Release|Any CPU
898900
{3428D7AD-B362-4647-B1B0-72674CF3BC7C}.Release|x86.ActiveCfg = Release|Any CPU
899901
{3428D7AD-B362-4647-B1B0-72674CF3BC7C}.Release|x86.Build.0 = Release|Any CPU
902+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
903+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
904+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|x64.ActiveCfg = Debug|Any CPU
905+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|x64.Build.0 = Debug|Any CPU
906+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|x86.ActiveCfg = Debug|Any CPU
907+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|x86.Build.0 = Debug|Any CPU
908+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
909+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|Any CPU.Build.0 = Release|Any CPU
910+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|x64.ActiveCfg = Release|Any CPU
911+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|x64.Build.0 = Release|Any CPU
912+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|x86.ActiveCfg = Release|Any CPU
913+
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|x86.Build.0 = Release|Any CPU
900914
EndGlobalSection
901915
GlobalSection(SolutionProperties) = preSolution
902916
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)