Skip to content

Commit

Permalink
Add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
timcassell committed Aug 11, 2023
1 parent 00bb520 commit 5f4298c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Tests.Loggers;
using BenchmarkDotNet.Tests.XUnit;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using BenchmarkDotNet.Toolchains.InProcess.NoEmit;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -86,6 +89,35 @@ public void Recursive()
[MethodImpl(MethodImplOptions.NoInlining)] public void Benchmark(bool justAnOverload) { } // we need to test overloads (#562)
}

public class WithCallsNoEmitFriendly
{
[Benchmark]
public void Benchmark()
{
// we should rather have test per use case
// but running so many tests for all JITs would take too much time
// so we have one method that does it all
Static();
Instance();
Recursive();

Benchmark(true);
}

[MethodImpl(MethodImplOptions.NoInlining)] public static void Static() { }

[MethodImpl(MethodImplOptions.NoInlining)] public void Instance() { }

[MethodImpl(MethodImplOptions.NoInlining)] // legacy JIT x64 was able to inline this method ;)
public void Recursive()
{
if (new Random(123).Next(0, 10) == 11) // never true, but JIT does not know it
Recursive();
}

[MethodImpl(MethodImplOptions.NoInlining)] public void Benchmark(bool justAnOverload) { } // we need to test overloads (#562)
}

[Theory]
[MemberData(nameof(GetAllJits))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
Expand All @@ -105,6 +137,33 @@ public void CanDisassembleAllMethodCalls(Jit jit, Platform platform, Runtime run
AssertDisassembled(disassemblyDiagnoser, $"{nameof(WithCalls.Recursive)}()");
}

// For some reason the GitHub runner with Core cannot fully disassemble, so we skip it.
[FactEnvSpecific("InProcess disassembly only supported on Windows x86/64.", EnvRequirement.WindowsOnly, EnvRequirement.X86X64Only, EnvRequirement.FullFrameworkOnly)]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void CanDisassembleAllMethodCallsInProcess()
{
var disassemblyDiagnoser = new DisassemblyDiagnoser(
new DisassemblyDiagnoserConfig(printSource: true, maxDepth: 3));

var config = ManualConfig.CreateEmpty()
.AddJob(Job.Dry
.WithStrategy(RunStrategy.ColdStart)
.WithToolchain(InProcessEmitToolchain.Instance)
)
.AddLogger(DefaultConfig.Instance.GetLoggers().ToArray())
.AddColumnProvider(DefaultColumnProviders.Instance)
.AddDiagnoser(disassemblyDiagnoser)
.AddLogger(new OutputLogger(Output));

CanExecute<WithCalls>(config);

AssertDisassembled(disassemblyDiagnoser, $"{nameof(WithCalls.Benchmark)}(Int32)");
AssertDisassembled(disassemblyDiagnoser, $"{nameof(WithCalls.Benchmark)}(Boolean)");
AssertDisassembled(disassemblyDiagnoser, $"{nameof(WithCalls.Static)}()");
AssertDisassembled(disassemblyDiagnoser, $"{nameof(WithCalls.Instance)}()");
AssertDisassembled(disassemblyDiagnoser, $"{nameof(WithCalls.Recursive)}()");
}

[Theory]
[MemberData(nameof(GetAllJits))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
Expand Down
3 changes: 2 additions & 1 deletion tests/BenchmarkDotNet.Tests/XUnit/EnvRequirement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public enum EnvRequirement
FullFrameworkOnly,
NonFullFramework,
DotNetCoreOnly,
DotNetCore30Only
DotNetCore30Only,
X86X64Only
}
2 changes: 2 additions & 0 deletions tests/BenchmarkDotNet.Tests/XUnit/EnvRequirementChecker.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using JetBrains.Annotations;
using BdnRuntimeInformation = BenchmarkDotNet.Portability.RuntimeInformation;
Expand All @@ -22,6 +23,7 @@ public static class EnvRequirementChecker
EnvRequirement.NonFullFramework => !BdnRuntimeInformation.IsFullFramework ? null : "Non-Full .NET Framework test",
EnvRequirement.DotNetCoreOnly => BdnRuntimeInformation.IsNetCore ? null : ".NET/.NET Core-only test",
EnvRequirement.DotNetCore30Only => IsRuntime(RuntimeMoniker.NetCoreApp30) ? null : ".NET Core 3.0-only test",
EnvRequirement.X86X64Only => BdnRuntimeInformation.GetCurrentPlatform() is Platform.X64 or Platform.X86 ? null : "x86- or x64-only test",
_ => throw new ArgumentOutOfRangeException(nameof(requirement), requirement, "Unknown value")
};

Expand Down

0 comments on commit 5f4298c

Please sign in to comment.