From 7c28874ef9856542407294fe6f1bd8fdc0d9ffe9 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Tue, 23 Jul 2024 08:18:39 +0100 Subject: [PATCH 1/4] Update runtime metrics tests to avoid failures on some platforms --- .../tests/RuntimeMetricsTests.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs b/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs index c4d38f82303122..d6abb2a3127a7f 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs @@ -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; @@ -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 s_forceGc = () => { for (var gen = 0; gen <= GC.MaxGeneration; gen++) @@ -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 measurement in measurements.Where(m => m.Value >= 1)) + foreach (Measurement measurement in measurements) { var tags = measurement.Tags.ToArray(); var tag = tags.SingleOrDefault(k => k.Key == "gc.heap.generation"); @@ -228,9 +232,9 @@ static void AssertExceptions(IReadOnlyList> 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 }, From 7156ef10870f8667310901aebc80c4cb87fb1ead Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Tue, 23 Jul 2024 15:22:34 +0100 Subject: [PATCH 2/4] Fix incorrect assertion in last commit --- .../tests/RuntimeMetricsTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs b/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs index d6abb2a3127a7f..00e4910ffea279 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs @@ -224,7 +224,7 @@ static void AssertExceptions(IReadOnlyList> measurements, int } } - public static IEnumerable LongMeasurements => new List + public static IEnumerable Measurements => new List { new object[] { "dotnet.process.memory.working_set", s_longGreaterThanZero, null }, new object[] { "dotnet.assembly.count", s_longGreaterThanZero, null }, @@ -234,7 +234,7 @@ static void AssertExceptions(IReadOnlyList> measurements, int 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_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.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 }, @@ -243,7 +243,7 @@ static void AssertExceptions(IReadOnlyList> measurements, int }; [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] - [MemberData(nameof(LongMeasurements))] + [MemberData(nameof(Measurements))] public void ValidateMeasurements(string metricName, Func? valueAssertion, Func? beforeRecord) where T : struct { From 0c3145d685cc66327ea44f760c926df78b0f828e Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Wed, 24 Jul 2024 05:53:11 +0100 Subject: [PATCH 3/4] Skip CPU time on mobile --- .../tests/RuntimeMetricsTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs b/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs index 00e4910ffea279..c1c82de1d16f7c 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs @@ -116,7 +116,7 @@ public void GcCollectionsCount() } } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMobile))] public void CpuTime() { using InstrumentRecorder instrumentRecorder = new("dotnet.process.cpu.time"); @@ -125,7 +125,7 @@ public void CpuTime() bool[] foundCpuModes = [false, false]; - foreach (Measurement measurement in instrumentRecorder.GetMeasurements().Where(m => m.Value >= 0)) + foreach (Measurement measurement in instrumentRecorder.GetMeasurements()) { var tags = measurement.Tags.ToArray(); var tag = tags.SingleOrDefault(k => k.Key == "cpu.mode"); From 7109a74a14993e6a1fe5bb3cb5ca638d265d4e9c Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Tue, 30 Jul 2024 13:58:47 -0700 Subject: [PATCH 4/4] Skip test on Mobile platforms --- .../tests/RuntimeMetricsTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs b/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs index c1c82de1d16f7c..8049d1b5e25d26 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs @@ -242,7 +242,7 @@ static void AssertExceptions(IReadOnlyList> measurements, int new object[] { "dotnet.timer.count", s_longGreaterThanOrEqualToZero, null }, }; - [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMobile))] [MemberData(nameof(Measurements))] public void ValidateMeasurements(string metricName, Func? valueAssertion, Func? beforeRecord) where T : struct