Skip to content

Commit

Permalink
Update runtime metrics tests to avoid failures on some platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon committed Jul 23, 2024
1 parent 7c3286f commit 58e2a81
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Threading;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -17,6 +18,9 @@ public class RuntimeMetricsTests(ITestOutputHelper output)

private static readonly string[] s_genNames = ["gen0", "gen1", "gen2", "loh", "poh"];

// On some platforms and AoT scenarios, the JIT may not be in use. Some assertions will consider zero as a valid in such cases.
private static bool s_jitHasRun = JitInfo.GetCompiledMethodCount() > 0;

private static readonly Func<bool> s_forceGc = () =>
{
for (var gen = 0; gen <= GC.MaxGeneration; gen++)
Expand Down Expand Up @@ -69,7 +73,7 @@ public void GcCollectionsCount()
Assert.True(measurements.Count >= gensExpected, $"Expected to find at least one measurement for each generation ({gensExpected}) " +
$"but received {measurements.Count} measurements.");

foreach (Measurement<long> measurement in measurements.Where(m => m.Value >= 1))
foreach (Measurement<long> measurement in measurements)
{
var tags = measurement.Tags.ToArray();
var tag = tags.SingleOrDefault(k => k.Key == "gc.heap.generation");
Expand Down Expand Up @@ -228,9 +232,9 @@ static void AssertExceptions(IReadOnlyList<Measurement<long>> measurements, int
new object[] { "dotnet.gc.heap.total_allocated", s_longGreaterThanZero, null },
new object[] { "dotnet.gc.last_collection.memory.committed_size", s_longGreaterThanZero, s_forceGc },
new object[] { "dotnet.gc.pause.time", s_doubleGreaterThanOrEqualToZero, s_forceGc }, // may be zero if no GC has occurred
new object[] { "dotnet.jit.compiled_il.size", s_longGreaterThanZero, null },
new object[] { "dotnet.jit.compiled_methods", s_longGreaterThanZero, null },
new object[] { "dotnet.jit.compilation.time", s_doubleGreaterThanZero, null },
new object[] { "dotnet.jit.compiled_il.size", s_jitHasRun ? s_longGreaterThanZero : s_longGreaterThanOrEqualToZero, null },
new object[] { "dotnet.jit.compiled_methods", s_jitHasRun ? s_longGreaterThanZero : s_longGreaterThanOrEqualToZero, null },
new object[] { "dotnet.jit.compilation.time", s_jitHasRun ? s_longGreaterThanZero : s_longGreaterThanOrEqualToZero, null },
new object[] { "dotnet.monitor.lock_contentions", s_longGreaterThanOrEqualToZero, null },
new object[] { "dotnet.thread_pool.thread.count", s_longGreaterThanZero, null },
new object[] { "dotnet.thread_pool.work_item.count", s_longGreaterThanOrEqualToZero, null },
Expand Down

0 comments on commit 58e2a81

Please sign in to comment.