4
4
using System . Collections . Concurrent ;
5
5
using System . Collections . Generic ;
6
6
using System . Linq ;
7
+ using System . Runtime ;
7
8
using System . Threading ;
8
9
using Xunit ;
9
10
using Xunit . Abstractions ;
@@ -17,6 +18,9 @@ public class RuntimeMetricsTests(ITestOutputHelper output)
17
18
18
19
private static readonly string [ ] s_genNames = [ "gen0" , "gen1" , "gen2" , "loh" , "poh" ] ;
19
20
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
+
20
24
private static readonly Func < bool > s_forceGc = ( ) =>
21
25
{
22
26
for ( var gen = 0 ; gen <= GC . MaxGeneration ; gen ++ )
@@ -69,7 +73,7 @@ public void GcCollectionsCount()
69
73
Assert . True ( measurements . Count >= gensExpected , $ "Expected to find at least one measurement for each generation ({ gensExpected } ) " +
70
74
$ "but received { measurements . Count } measurements.") ;
71
75
72
- foreach ( Measurement < long > measurement in measurements . Where ( m => m . Value >= 1 ) )
76
+ foreach ( Measurement < long > measurement in measurements )
73
77
{
74
78
var tags = measurement . Tags . ToArray ( ) ;
75
79
var tag = tags . SingleOrDefault ( k => k . Key == "gc.heap.generation" ) ;
@@ -112,7 +116,7 @@ public void GcCollectionsCount()
112
116
}
113
117
}
114
118
115
- [ ConditionalFact ( typeof ( PlatformDetection ) , nameof ( PlatformDetection . IsNotBrowser ) ) ]
119
+ [ ConditionalFact ( typeof ( PlatformDetection ) , nameof ( PlatformDetection . IsNotMobile ) ) ]
116
120
public void CpuTime ( )
117
121
{
118
122
using InstrumentRecorder < double > instrumentRecorder = new ( "dotnet.process.cpu.time" ) ;
@@ -121,7 +125,7 @@ public void CpuTime()
121
125
122
126
bool [ ] foundCpuModes = [ false , false ] ;
123
127
124
- foreach ( Measurement < double > measurement in instrumentRecorder . GetMeasurements ( ) . Where ( m => m . Value >= 0 ) )
128
+ foreach ( Measurement < double > measurement in instrumentRecorder . GetMeasurements ( ) )
125
129
{
126
130
var tags = measurement . Tags . ToArray ( ) ;
127
131
var tag = tags . SingleOrDefault ( k => k . Key == "cpu.mode" ) ;
@@ -220,26 +224,26 @@ static void AssertExceptions(IReadOnlyList<Measurement<long>> measurements, int
220
224
}
221
225
}
222
226
223
- public static IEnumerable < object [ ] > LongMeasurements => new List < object [ ] >
227
+ public static IEnumerable < object [ ] > Measurements => new List < object [ ] >
224
228
{
225
229
new object [ ] { "dotnet.process.memory.working_set" , s_longGreaterThanZero , null } ,
226
230
new object [ ] { "dotnet.assembly.count" , s_longGreaterThanZero , null } ,
227
231
new object [ ] { "dotnet.process.cpu.count" , s_longGreaterThanZero , null } ,
228
232
new object [ ] { "dotnet.gc.heap.total_allocated" , s_longGreaterThanZero , null } ,
229
233
new object [ ] { "dotnet.gc.last_collection.memory.committed_size" , s_longGreaterThanZero , s_forceGc } ,
230
234
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 } ,
234
238
new object [ ] { "dotnet.monitor.lock_contentions" , s_longGreaterThanOrEqualToZero , null } ,
235
239
new object [ ] { "dotnet.thread_pool.thread.count" , s_longGreaterThanZero , null } ,
236
240
new object [ ] { "dotnet.thread_pool.work_item.count" , s_longGreaterThanOrEqualToZero , null } ,
237
241
new object [ ] { "dotnet.thread_pool.queue.length" , s_longGreaterThanOrEqualToZero , null } ,
238
242
new object [ ] { "dotnet.timer.count" , s_longGreaterThanOrEqualToZero , null } ,
239
243
} ;
240
244
241
- [ ConditionalTheory ( typeof ( PlatformDetection ) , nameof ( PlatformDetection . IsNotBrowser ) ) ]
242
- [ MemberData ( nameof ( LongMeasurements ) ) ]
245
+ [ ConditionalTheory ( typeof ( PlatformDetection ) , nameof ( PlatformDetection . IsNotMobile ) ) ]
246
+ [ MemberData ( nameof ( Measurements ) ) ]
243
247
public void ValidateMeasurements < T > ( string metricName , Func < T , ( bool , string ? ) > ? valueAssertion , Func < bool > ? beforeRecord )
244
248
where T : struct
245
249
{
0 commit comments