Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@
<PackageVersion Include="xunit.v3.assert" Version="3.2.2" />
<PackageVersion Include="xunit.v3.extensibility.core" Version="3.2.2" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

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

</Project>
</Project>
14 changes: 14 additions & 0 deletions TUnit.SourceGenerator.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using BenchmarkDotNet.Running;
using Microsoft.Build.Locator;
using TUnit.SourceGenerator.Benchmarks;

MSBuildLocator.RegisterDefaults();

if (args is { Length: > 0 })
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
else
{
BenchmarkRunner.Run<TestMetadataGeneratorBenchmarks>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" />
<PackageReference Include="Microsoft.Build" VersionOverride="17.7.2" ExcludeAssets="runtime" PrivateAssets="all" />
<PackageReference Include="Microsoft.Build.Locator" VersionOverride="1.7.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" VersionOverride="5.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" VersionOverride="5.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="all" VersionOverride="5.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" VersionOverride="5.0.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.Common" PrivateAssets="all" VersionOverride="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.csproj" />
<ProjectReference Include="..\TUnit.Core\TUnit.Core.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using BenchmarkDotNet.Attributes;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
using TUnit.Core.SourceGenerator.Generators;
using TUnit.SourceGenerator.Benchmarks;

namespace TUnit.SourceGenerator.Benchmarks;

[MemoryDiagnoser]
[InProcess]
public class TestMetadataGeneratorBenchmarks
{
private const string SampleProjectPath = "../TUnit.TestProject/TUnit.TestProject.csproj";

private MSBuildWorkspace? _workspace;
private GeneratorDriver? _sampleDriver;
private Compilation? _sampleCompilation;

[GlobalSetup(Target = nameof(Compile))]
public void SetupCompile() =>
(_sampleCompilation, _sampleDriver, _workspace) =
WorkspaceHelper.SetupAsync<TestMetadataGenerator>(SampleProjectPath)
.GetAwaiter()
.GetResult();

[Benchmark]
public GeneratorDriver Compile() => _sampleDriver!.RunGeneratorsAndUpdateCompilation(_sampleCompilation!, out _, out _);

[GlobalCleanup]
public void Cleanup()
{
_workspace?.Dispose();
}
}
56 changes: 56 additions & 0 deletions TUnit.SourceGenerator.Benchmarks/WorkspaceHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Loggers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.MSBuild;

namespace TUnit.SourceGenerator.Benchmarks;

public static class WorkspaceHelper
{
private static string GetDirectoryRelativePath(string projectPath, [CallerFilePath] string callerFilePath = null!) =>
Path.Combine(Path.GetDirectoryName(callerFilePath)!, projectPath);

public static async Task<(Compilation, CSharpGeneratorDriver, MSBuildWorkspace)> SetupAsync<TSourceGenerator>( string projectPath)
where TSourceGenerator : IIncrementalGenerator, new()
{
MSBuildWorkspace workspace = MSBuildWorkspace.Create();
workspace.RegisterWorkspaceFailedHandler(args =>
{
ConsoleLogger.Default.WriteLineError("-------------------------");
ConsoleLogger.Default.WriteLineError(args.Diagnostic.ToString());
ConsoleLogger.Default.WriteLineError("-------------------------");
});

var projectFile = GetDirectoryRelativePath(projectPath);

if (!File.Exists(projectFile))
throw new Exception($"Project doesn't exist at {projectFile}");

ConsoleLogger.Default.WriteLine($"Project exists at {projectFile}");

Project project;
try
{
ConsoleLogger.Default.WriteLine("Loading project\n");
project = await workspace.OpenProjectAsync(projectFile);
ConsoleLogger.Default.WriteLine("\nLoaded project");
}
catch (Exception ex)
{
ConsoleLogger.Default.WriteError($"Error: {ex.Message}");
throw;
}

var compilation = await project.GetCompilationAsync();
if (compilation == null)
throw new InvalidOperationException("Compilation returned null");

var generator = new TSourceGenerator().AsSourceGenerator();

var driver =
CSharpGeneratorDriver.Create([generator], parseOptions: (CSharpParseOptions) project.ParseOptions!);

return (compilation, driver, workspace);
}
}
14 changes: 14 additions & 0 deletions TUnit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TUnit.FsCheck", "TUnit.FsCh
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TUnit.Example.FsCheck.TestProject", "TUnit.Example.FsCheck.TestProject\TUnit.Example.FsCheck.TestProject.csproj", "{3428D7AD-B362-4647-B1B0-72674CF3BC7C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TUnit.SourceGenerator.Benchmarks", "TUnit.SourceGenerator.Benchmarks\TUnit.SourceGenerator.Benchmarks.csproj", "{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -897,6 +899,18 @@ Global
{3428D7AD-B362-4647-B1B0-72674CF3BC7C}.Release|x64.Build.0 = Release|Any CPU
{3428D7AD-B362-4647-B1B0-72674CF3BC7C}.Release|x86.ActiveCfg = Release|Any CPU
{3428D7AD-B362-4647-B1B0-72674CF3BC7C}.Release|x86.Build.0 = Release|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|x64.ActiveCfg = Debug|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|x64.Build.0 = Debug|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|x86.ActiveCfg = Debug|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Debug|x86.Build.0 = Debug|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|Any CPU.Build.0 = Release|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|x64.ActiveCfg = Release|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|x64.Build.0 = Release|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|x86.ActiveCfg = Release|Any CPU
{F686AD4B-FC90-48B9-84C9-C7B16C2E13E5}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading