Skip to content

Commit c86bcbb

Browse files
Update runtime metrics tests to avoid failures on some platforms (#105300)
* Update runtime metrics tests to avoid failures on some platforms * Fix incorrect assertion in last commit * Skip CPU time on mobile * Skip test on Mobile platforms --------- Co-authored-by: Tarek Mahmoud Sayed <[email protected]>
1 parent 50bc6a1 commit c86bcbb

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/libraries/System.Diagnostics.DiagnosticSource/tests/RuntimeMetricsTests.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Concurrent;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Runtime;
78
using System.Threading;
89
using Xunit;
910
using Xunit.Abstractions;
@@ -17,6 +18,9 @@ public class RuntimeMetricsTests(ITestOutputHelper output)
1718

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

21+
// On some platforms and AoT scenarios, the JIT may not be in use. Some assertions will consider zero as a valid in such cases.
22+
private static bool s_jitHasRun = JitInfo.GetCompiledMethodCount() > 0;
23+
2024
private static readonly Func<bool> s_forceGc = () =>
2125
{
2226
for (var gen = 0; gen <= GC.MaxGeneration; gen++)
@@ -69,7 +73,7 @@ public void GcCollectionsCount()
6973
Assert.True(measurements.Count >= gensExpected, $"Expected to find at least one measurement for each generation ({gensExpected}) " +
7074
$"but received {measurements.Count} measurements.");
7175

72-
foreach (Measurement<long> measurement in measurements.Where(m => m.Value >= 1))
76+
foreach (Measurement<long> measurement in measurements)
7377
{
7478
var tags = measurement.Tags.ToArray();
7579
var tag = tags.SingleOrDefault(k => k.Key == "gc.heap.generation");
@@ -112,7 +116,7 @@ public void GcCollectionsCount()
112116
}
113117
}
114118

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

122126
bool[] foundCpuModes = [false, false];
123127

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

223-
public static IEnumerable<object[]> LongMeasurements => new List<object[]>
227+
public static IEnumerable<object[]> Measurements => new List<object[]>
224228
{
225229
new object[] { "dotnet.process.memory.working_set", s_longGreaterThanZero, null },
226230
new object[] { "dotnet.assembly.count", s_longGreaterThanZero, null },
227231
new object[] { "dotnet.process.cpu.count", s_longGreaterThanZero, null },
228232
new object[] { "dotnet.gc.heap.total_allocated", s_longGreaterThanZero, null },
229233
new object[] { "dotnet.gc.last_collection.memory.committed_size", s_longGreaterThanZero, s_forceGc },
230234
new object[] { "dotnet.gc.pause.time", s_doubleGreaterThanOrEqualToZero, s_forceGc }, // may be zero if no GC has occurred
231-
new object[] { "dotnet.jit.compiled_il.size", s_longGreaterThanZero, null },
232-
new object[] { "dotnet.jit.compiled_methods", s_longGreaterThanZero, null },
233-
new object[] { "dotnet.jit.compilation.time", s_doubleGreaterThanZero, null },
235+
new object[] { "dotnet.jit.compiled_il.size", s_jitHasRun ? s_longGreaterThanZero : s_longGreaterThanOrEqualToZero, null },
236+
new object[] { "dotnet.jit.compiled_methods", s_jitHasRun ? s_longGreaterThanZero : s_longGreaterThanOrEqualToZero, null },
237+
new object[] { "dotnet.jit.compilation.time", s_jitHasRun ? s_doubleGreaterThanZero : s_doubleGreaterThanOrEqualToZero, null },
234238
new object[] { "dotnet.monitor.lock_contentions", s_longGreaterThanOrEqualToZero, null },
235239
new object[] { "dotnet.thread_pool.thread.count", s_longGreaterThanZero, null },
236240
new object[] { "dotnet.thread_pool.work_item.count", s_longGreaterThanOrEqualToZero, null },
237241
new object[] { "dotnet.thread_pool.queue.length", s_longGreaterThanOrEqualToZero, null },
238242
new object[] { "dotnet.timer.count", s_longGreaterThanOrEqualToZero, null },
239243
};
240244

241-
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
242-
[MemberData(nameof(LongMeasurements))]
245+
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMobile))]
246+
[MemberData(nameof(Measurements))]
243247
public void ValidateMeasurements<T>(string metricName, Func<T, (bool, string?)>? valueAssertion, Func<bool>? beforeRecord)
244248
where T : struct
245249
{

0 commit comments

Comments
 (0)