Skip to content

Commit

Permalink
Rebuild .Net Framework projects (#2370)
Browse files Browse the repository at this point in the history
* Added `CsProjWithRoslynFallbackToolchain` for .Net Framework projects to be able to be rebuilt per target framework.

* Move manual tests to separate solution.

* Test old frameworks and test that recompilation actually happens.

* Removed ClassicNetFramework project.
Moved manual test projects back to main sln.

* Removed `CsProjWithRoslynFallbackToolchain`, default to `CsProjClassicNetToolchain` instead.

* Use RoslynToolchain if dotnet SDK is not installed.

* Fix test.

* Revert xunit.runner.json.
Implement feedback.

* Try to fix json file

* Removed redundant commented line.
  • Loading branch information
timcassell authored Jul 27, 2023
1 parent 64d05c6 commit d93490d
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 8 deletions.
9 changes: 8 additions & 1 deletion BenchmarkDotNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "templates", "templates", "{
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkDotNet.Templates", "templates\BenchmarkDotNet.Templates.csproj", "{B620D10A-CD8E-4A34-8B27-FD6257E63AD0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkDotNet.Diagnostics.dotTrace", "src\BenchmarkDotNet.Diagnostics.dotTrace\BenchmarkDotNet.Diagnostics.dotTrace.csproj", "{C5BDA61F-3A56-4B59-901D-0A17E78F4076}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkDotNet.Diagnostics.dotTrace", "src\BenchmarkDotNet.Diagnostics.dotTrace\BenchmarkDotNet.Diagnostics.dotTrace.csproj", "{C5BDA61F-3A56-4B59-901D-0A17E78F4076}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkDotNet.IntegrationTests.ManualRunning.MultipleFrameworks", "tests\BenchmarkDotNet.IntegrationTests.ManualRunning.MultipleFrameworks\BenchmarkDotNet.IntegrationTests.ManualRunning.MultipleFrameworks.csproj", "{AACA2C63-A85B-47AB-99FC-72C3FF408B14}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -131,6 +133,10 @@ Global
{C5BDA61F-3A56-4B59-901D-0A17E78F4076}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5BDA61F-3A56-4B59-901D-0A17E78F4076}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C5BDA61F-3A56-4B59-901D-0A17E78F4076}.Release|Any CPU.Build.0 = Release|Any CPU
{AACA2C63-A85B-47AB-99FC-72C3FF408B14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AACA2C63-A85B-47AB-99FC-72C3FF408B14}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AACA2C63-A85B-47AB-99FC-72C3FF408B14}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AACA2C63-A85B-47AB-99FC-72C3FF408B14}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -155,6 +161,7 @@ Global
{D9F5065B-6190-431B-850C-117E3D64AB33} = {D6597E3A-6892-4A68-8E14-042FC941FDA2}
{B620D10A-CD8E-4A34-8B27-FD6257E63AD0} = {63B94FD6-3F3D-4E04-9727-48E86AC4384C}
{C5BDA61F-3A56-4B59-901D-0A17E78F4076} = {D6597E3A-6892-4A68-8E14-042FC941FDA2}
{AACA2C63-A85B-47AB-99FC-72C3FF408B14} = {14195214-591A-45B7-851A-19D3BA2413F9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4D9AF12B-1F7F-45A7-9E8C-E4E46ADCBD1F}
Expand Down
2 changes: 2 additions & 0 deletions src/BenchmarkDotNet/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
[assembly: InternalsVisibleTo("BenchmarkDotNet.Diagnostics.Windows,PublicKey=" + BenchmarkDotNetInfo.PublicKey)]
[assembly: InternalsVisibleTo("BenchmarkDotNet.Diagnostics.dotTrace,PublicKey=" + BenchmarkDotNetInfo.PublicKey)]
[assembly: InternalsVisibleTo("BenchmarkDotNet.IntegrationTests.ManualRunning,PublicKey=" + BenchmarkDotNetInfo.PublicKey)]
[assembly: InternalsVisibleTo("BenchmarkDotNet.IntegrationTests.ManualRunning.MultipleFrameworks,PublicKey=" + BenchmarkDotNetInfo.PublicKey)]
#else
[assembly: InternalsVisibleTo("BenchmarkDotNet.Tests")]
[assembly: InternalsVisibleTo("BenchmarkDotNet.IntegrationTests")]
[assembly: InternalsVisibleTo("BenchmarkDotNet.Diagnostics.Windows")]
[assembly: InternalsVisibleTo("BenchmarkDotNet.Diagnostics.dotTrace")]
[assembly: InternalsVisibleTo("BenchmarkDotNet.IntegrationTests.ManualRunning")]
[assembly: InternalsVisibleTo("BenchmarkDotNet.IntegrationTests.ManualRunning.MultipleFrameworks")]
#endif
20 changes: 14 additions & 6 deletions src/BenchmarkDotNet/Toolchains/ToolchainExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Helpers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Running;
Expand Down Expand Up @@ -34,12 +35,19 @@ internal static IToolchain GetToolchain(this Runtime runtime, Descriptor descrip
switch (runtime)
{
case ClrRuntime clrRuntime:
if (RuntimeInformation.IsNetCore || preferMsBuildToolchains)
return clrRuntime.RuntimeMoniker != RuntimeMoniker.NotRecognized
? GetToolchain(clrRuntime.RuntimeMoniker)
: CsProjClassicNetToolchain.From(clrRuntime.MsBuildMoniker);

return RoslynToolchain.Instance;
if (!preferMsBuildToolchains && RuntimeInformation.IsFullFramework &&
// If dotnet SDK is not installed, we use RoslynToolchain.
(!HostEnvironmentInfo.GetCurrent().IsDotNetCliInstalled()
// Integration tests take too much time, because each benchmark run rebuilds the test suite and BenchmarkDotNet itself.
// To reduce the total duration of the CI workflows, we just use RoslynToolchain.
|| XUnitHelper.IsIntegrationTest.Value))
{
return RoslynToolchain.Instance;
}

return clrRuntime.RuntimeMoniker != RuntimeMoniker.NotRecognized
? GetToolchain(clrRuntime.RuntimeMoniker)
: CsProjClassicNetToolchain.From(clrRuntime.MsBuildMoniker);

case MonoRuntime mono:
if (RuntimeInformation.IsAndroid())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<AssemblyTitle>BenchmarkDotNet.IntegrationTests.ManualRunning.MultipleFrameworks</AssemblyTitle>
<!-- We test the oldest frameworks supported by BDN (net461 and netcoreapp2.0), as well as newer versions of those frameworks. -->
<TargetFrameworks>net461;net48;netcoreapp2.0;net7.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AssemblyName>BenchmarkDotNet.IntegrationTests.ManualRunning.MultipleFrameworks</AssemblyName>
<PackageId>BenchmarkDotNet.IntegrationTests.ManualRunning.MultipleFrameworks</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\BenchmarkDotNet.IntegrationTests\BenchmarkTestExecutor.cs" Link="BenchmarkTestExecutor.cs" />
<Compile Include="..\BenchmarkDotNet.IntegrationTests\Xunit\MisconfiguredEnvironmentException.cs" Link="MisconfiguredEnvironmentException.cs" />
<Compile Include="..\BenchmarkDotNet.IntegrationTests\Xunit\Extensions.cs" Link="Extensions.cs" />
<Compile Include="..\BenchmarkDotNet.IntegrationTests\TestConfigs.cs" Link="TestConfigs.cs" />
<Compile Include="..\BenchmarkDotNet.Tests\Loggers\OutputLogger.cs" Link="OutputLogger.cs" />
<Compile Include="..\BenchmarkDotNet.Tests\XUnit\SmartAssert.cs" Link="SmartAssert.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.Platforms" Version="5.0.0" />
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
<ItemGroup>
<Content Include="..\BenchmarkDotNet.IntegrationTests.ManualRunning\xunit.runner.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\BenchmarkDotNet.Annotations\BenchmarkDotNet.Annotations.csproj" />
<ProjectReference Include="..\..\src\BenchmarkDotNet\BenchmarkDotNet.csproj" />
</ItemGroup>
<ItemGroup>
<!-- Microsoft.NET.Test.Sdk breaks netcoreapp2.0 -->
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.3" Condition=" '$(TargetFramework)' != 'netcoreapp2.0' " />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Jobs;
using Xunit;

namespace BenchmarkDotNet.IntegrationTests
{
public class MultipleFrameworksTest : BenchmarkTestExecutor
{
private const string TfmEnvVarName = "TfmEnvVarName";

[Theory]
[InlineData(RuntimeMoniker.Net461)]
[InlineData(RuntimeMoniker.Net48)]
[InlineData(RuntimeMoniker.NetCoreApp20)]
[InlineData(RuntimeMoniker.Net70)]
public void EachFrameworkIsRebuilt(RuntimeMoniker runtime)
{
var config = ManualConfig.CreateEmpty().AddJob(Job.Dry.WithRuntime(runtime.GetRuntime()).WithEnvironmentVariable(TfmEnvVarName, runtime.ToString()));
CanExecute<ValuePerTfm>(config);
}

public class ValuePerTfm
{
private const RuntimeMoniker moniker =
#if NET461
RuntimeMoniker.Net461;
#elif NET48
RuntimeMoniker.Net48;
#elif NETCOREAPP2_0
RuntimeMoniker.NetCoreApp20;
#elif NET7_0
RuntimeMoniker.Net70;
#else
RuntimeMoniker.NotRecognized;
#endif

[Benchmark]
public void ThrowWhenWrong()
{
if (Environment.GetEnvironmentVariable(TfmEnvVarName) != moniker.ToString())
{
throw new InvalidOperationException($"Has not been recompiled, the value was {Environment.GetEnvironmentVariable(TfmEnvVarName)}");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<Compile Include="..\BenchmarkDotNet.IntegrationTests\Xunit\MisconfiguredEnvironmentException.cs" Link="MisconfiguredEnvironmentException.cs" />
<Compile Include="..\BenchmarkDotNet.IntegrationTests\Xunit\Extensions.cs" Link="Extensions.cs" />
<Compile Include="..\BenchmarkDotNet.IntegrationTests\TestConfigs.cs" Link="TestConfigs.cs" />
<Compile Include="..\BenchmarkDotNet.Tests\Loggers\OutputLogger.cs" Link="OutputLogger.cs" />
<Compile Include="..\BenchmarkDotNet.Tests\XUnit\*.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="xunit.runner.json">
Expand All @@ -25,7 +27,6 @@
<ItemGroup>
<ProjectReference Include="..\..\src\BenchmarkDotNet\BenchmarkDotNet.csproj" />
<ProjectReference Include="..\..\src\BenchmarkDotNet.Diagnostics.Windows\BenchmarkDotNet.Diagnostics.Windows.csproj" />
<ProjectReference Include="..\BenchmarkDotNet.Tests\BenchmarkDotNet.Tests.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
Expand Down

0 comments on commit d93490d

Please sign in to comment.