Skip to content

Commit 1e96ae2

Browse files
committed
Changed Engine.GetTotalBytes to static, and only set it once.
1 parent b3f2aaf commit 1e96ae2

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/BenchmarkDotNet/Engines/Engine.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class Engine : IEngine
5252
// These must be static since more than one Engine is used.
5353
private static long survivedBytes;
5454
private static bool survivedBytesMeasured;
55-
private Func<long> GetTotalBytes { get; }
55+
private static Func<long> GetTotalBytes { get; set; }
5656

5757
internal Engine(
5858
IHost host,
@@ -93,9 +93,11 @@ internal Engine(
9393
actualStage = new EngineActualStage(this);
9494

9595
random = new Random(12345); // we are using constant seed to try to get repeatable results
96-
if (includeSurvivedMemory && !survivedBytesMeasured)
96+
97+
if (includeSurvivedMemory && GetTotalBytes is null)
9798
{
98-
GetTotalBytes = GetTotalBytesFunc();
99+
// CreateGetTotalBytesFunc enables monitoring, so we only call it if we need to measure survived memory.
100+
GetTotalBytes = CreateGetTotalBytesFunc();
99101

100102
// Necessary for CORE runtimes.
101103
// Measure bytes to allow GC monitor to make its allocations.
@@ -106,9 +108,8 @@ internal Engine(
106108
}
107109
}
108110

109-
private Func<long> GetTotalBytesFunc()
111+
private static Func<long> CreateGetTotalBytesFunc()
110112
{
111-
// Only enable monitoring if memory diagnoser with survived memory is applied.
112113
// Don't try to measure in Mono, Monitoring is not available, and GC.GetTotalMemory is very inaccurate.
113114
if (RuntimeInformation.IsMono)
114115
return () => 0;

tests/BenchmarkDotNet.IntegrationTests/MemoryDiagnoserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ private void AssertSurvived(IToolchain toolchain, Type benchmarkType, Dictionary
416416

417417
foreach (var benchmarkSurvivedValidator in benchmarkSurvivedValidators)
418418
{
419-
var survivedBenchmarks = benchmarks.BenchmarksCases.Where(benchmark => benchmark.Descriptor.WorkloadMethodDisplayInfo == benchmarkSurvivedValidator.Key).ToArray();
419+
var survivedBenchmarks = benchmarks.BenchmarksCases.Where(benchmark => benchmark.Descriptor.WorkloadMethodDisplayInfo == benchmarkSurvivedValidator.Key);
420420

421421
foreach (var benchmark in survivedBenchmarks)
422422
{

0 commit comments

Comments
 (0)