Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update runtime metrics tests to avoid failures on some platforms #105300

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -112,7 +116,7 @@ public void GcCollectionsCount()
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMobile))]
public void CpuTime()
{
using InstrumentRecorder<double> instrumentRecorder = new("dotnet.process.cpu.time");
Expand All @@ -121,7 +125,7 @@ public void CpuTime()

bool[] foundCpuModes = [false, false];

foreach (Measurement<double> measurement in instrumentRecorder.GetMeasurements().Where(m => m.Value >= 0))
foreach (Measurement<double> measurement in instrumentRecorder.GetMeasurements())
{
var tags = measurement.Tags.ToArray();
var tag = tags.SingleOrDefault(k => k.Key == "cpu.mode");
Expand Down Expand Up @@ -220,26 +224,26 @@ static void AssertExceptions(IReadOnlyList<Measurement<long>> measurements, int
}
}

public static IEnumerable<object[]> LongMeasurements => new List<object[]>
public static IEnumerable<object[]> Measurements => new List<object[]>
{
new object[] { "dotnet.process.memory.working_set", s_longGreaterThanZero, null },
new object[] { "dotnet.assembly.count", s_longGreaterThanZero, null },
new object[] { "dotnet.process.cpu.count", s_longGreaterThanZero, null },
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_doubleGreaterThanZero : s_doubleGreaterThanOrEqualToZero, 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 },
new object[] { "dotnet.thread_pool.queue.length", s_longGreaterThanOrEqualToZero, null },
new object[] { "dotnet.timer.count", s_longGreaterThanOrEqualToZero, null },
};

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
[MemberData(nameof(LongMeasurements))]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMobile))]
[MemberData(nameof(Measurements))]
public void ValidateMeasurements<T>(string metricName, Func<T, (bool, string?)>? valueAssertion, Func<bool>? beforeRecord)
where T : struct
{
Expand Down
Loading