Skip to content

Commit

Permalink
Changed remaining RuntimeInformation properties to readonly fields.
Browse files Browse the repository at this point in the history
Disabled allocating tests.
  • Loading branch information
timcassell committed Apr 25, 2024
1 parent 9476e9b commit 4d86cd1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Engines/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private ClockSpan Measure(Action<long> action, long invokeCount)
Thread.Sleep(TimeSpan.FromMilliseconds(500));
}

// GC collect before measuring allocations, as we do not collect during the measurement.
// GC collect before measuring allocations.
ForceGcCollect();
var gcStats = MeasureWithGc(data.InvokeCount / data.UnrollFactor);

Expand Down
2 changes: 0 additions & 2 deletions src/BenchmarkDotNet/Engines/GcStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ public static GcStats FromForced(int forcedFullGarbageCollections)
if (RuntimeInformation.IsWasm)
return null;

// Calling GC.Collect() before calling GC.GetTotalAllocatedBytes appears to interfere with the results for some reason,
// so we just call the API without forcing a collection.
#if NET6_0_OR_GREATER
return GC.GetTotalAllocatedBytes(precise: true);
#else
Expand Down
6 changes: 3 additions & 3 deletions src/BenchmarkDotNet/Portability/RuntimeInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ private static bool IsAotMethod()
&& Environment.GetEnvironmentVariable("DOTNET_TieredCompilation") != "0"
&& (!AppContext.TryGetSwitch("System.Runtime.TieredCompilation", out isEnabled) || isEnabled));

public static bool IsRunningInContainer => string.Equals(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"), "true");
public static readonly bool IsRunningInContainer = string.Equals(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"), "true");

internal static string ExecutableExtension => IsWindows() ? ".exe" : string.Empty;
internal static readonly string ExecutableExtension = IsWindows() ? ".exe" : string.Empty;

internal static string ScriptFileExtension => IsWindows() ? ".bat" : ".sh";
internal static readonly string ScriptFileExtension = IsWindows() ? ".bat" : ".sh";

internal static string GetArchitecture() => GetCurrentPlatform().ToString();

Expand Down
17 changes: 11 additions & 6 deletions tests/BenchmarkDotNet.IntegrationTests/MemoryDiagnoserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace BenchmarkDotNet.IntegrationTests
{
// TODO: re-enable allocating tests after https://github.com/dotnet/runtime/issues/101536 is fixed.
public class MemoryDiagnoserTests
{
private readonly ITestOutputHelper output;
Expand All @@ -50,7 +51,7 @@ public class AccurateAllocations
[Benchmark] public Task<int> AllocateTask() => Task.FromResult<int>(-12345);
}

[Theory, MemberData(nameof(GetToolchains))]
[Theory(Skip = "System.Runtime.InteropServices.RuntimeInformation allocates unexpectedly"), MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void MemoryDiagnoserIsAccurate(IToolchain toolchain)
{
Expand All @@ -70,7 +71,8 @@ public void MemoryDiagnoserIsAccurate(IToolchain toolchain)
});
}

[FactEnvSpecific("We don't want to test NativeAOT twice (for .NET Framework 4.6.2 and .NET 7.0)", EnvRequirement.DotNetCoreOnly)]
[Fact(Skip = "System.Runtime.InteropServices.RuntimeInformation allocates unexpectedly")]
//[FactEnvSpecific("We don't want to test NativeAOT twice (for .NET Framework 4.6.2 and .NET 8.0)", EnvRequirement.DotNetCoreOnly)]
public void MemoryDiagnoserSupportsNativeAOT()
{
if (RuntimeInformation.IsMacOS())
Expand All @@ -79,7 +81,8 @@ public void MemoryDiagnoserSupportsNativeAOT()
MemoryDiagnoserIsAccurate(NativeAotToolchain.Net80);
}

[FactEnvSpecific("We don't want to test MonoVM twice (for .NET Framework 4.6.2 and .NET 8.0)", EnvRequirement.DotNetCoreOnly)]
[Fact(Skip = "System.Runtime.InteropServices.RuntimeInformation allocates unexpectedly")]
//[FactEnvSpecific("We don't want to test MonoVM twice (for .NET Framework 4.6.2 and .NET 8.0)", EnvRequirement.DotNetCoreOnly)]
public void MemoryDiagnoserSupportsModernMono()
{
MemoryDiagnoserIsAccurate(MonoToolchain.Mono80);
Expand Down Expand Up @@ -207,7 +210,7 @@ public void WithOperationsPerInvoke()
private void DoNotInline(object left, object right) { }
}

[Theory, MemberData(nameof(GetToolchains))]
[Theory(Skip = "System.Runtime.InteropServices.RuntimeInformation allocates unexpectedly"), MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void AllocatedMemoryShouldBeScaledForOperationsPerInvoke(IToolchain toolchain)
{
Expand All @@ -233,7 +236,8 @@ public byte[] SixtyFourBytesArray()
}
}

[TheoryEnvSpecific("Full Framework cannot measure precisely enough for low invocation counts.", EnvRequirement.DotNetCoreOnly), MemberData(nameof(GetToolchains))]
[Theory(Skip = "System.Runtime.InteropServices.RuntimeInformation allocates unexpectedly"), MemberData(nameof(GetToolchains))]
//[TheoryEnvSpecific("Full Framework cannot measure precisely enough for low invocation counts.", EnvRequirement.DotNetCoreOnly), MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void AllocationQuantumIsNotAnIssueForNetCore21Plus(IToolchain toolchain)
{
Expand Down Expand Up @@ -298,7 +302,8 @@ public void Allocate()
}
}

[TheoryEnvSpecific("Full Framework cannot measure precisely enough", EnvRequirement.DotNetCoreOnly)]
[Theory(Skip = "System.Runtime.InteropServices.RuntimeInformation allocates unexpectedly"), MemberData(nameof(GetToolchains))]
//[TheoryEnvSpecific("Full Framework cannot measure precisely enough", EnvRequirement.DotNetCoreOnly)]
[MemberData(nameof(GetToolchains))]
[Trait(Constants.Category, Constants.BackwardCompatibilityCategory)]
public void MemoryDiagnoserIsAccurateForMultiThreadedBenchmarks(IToolchain toolchain)
Expand Down

0 comments on commit 4d86cd1

Please sign in to comment.