Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add an initial test
  • Loading branch information
stevejgordon committed Jul 17, 2024
commit 1ca6ff91ae983e5ec50d7f82a8029403361a8ed9
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace System.Diagnostics.Metrics.Tests
{
public class RuntimeMetricsTests
{
[Fact]
public async Task GcCollectionsCount()
{
using InstrumentRecorder<long> instrumentRecorder = new("dotnet.gc.collections.count");
using CancellationTokenSource cts = new(1000);

for (var gen = 0; gen <= GC.MaxGeneration; gen++)
{
GC.Collect(gen, GCCollectionMode.Forced);
}

instrumentRecorder.RecordObservableInstruments();

(bool success, IReadOnlyList<Measurement<long>> measurements) = await WaitForMeasurements(instrumentRecorder, 3, cts.Token);

Assert.True(success, "Expected to receive at least 3 measurements.");

int count = 0;
for (int i = GC.MaxGeneration; i >= 0; i--)
{
Measurement<long> measurement = measurements[count++];
Assert.True(measurement.Value > 0, $"Gen {i} count should be greater than zero.");

var tags = measurement.Tags.ToArray();

Assert.Equal(1, tags.Length);
VerifyTag(tags, "gc.heap.generation", $"gen{i}");
}
}

private static async Task<(bool, IReadOnlyList<Measurement<T>>)> WaitForMeasurements<T>(InstrumentRecorder<T> instrumentRecorder,
int expected, CancellationToken cancellationToken) where T : struct
{
IReadOnlyList<Measurement<T>> measurements;
while ((measurements = instrumentRecorder.GetMeasurements()).Count < 3)
{
if (cancellationToken.IsCancellationRequested)
{
return (false, measurements);
}

await Task.Delay(50, cancellationToken);
}

return (true, measurements);
}

private static void VerifyTag<T>(KeyValuePair<string, object?>[] tags, string name, T value)
{
if (value is null)
{
Assert.DoesNotContain(tags, t => t.Key == name);
}
else
{
Assert.Equal(value, (T)tags.Single(t => t.Key == name).Value);
}
}

protected sealed class InstrumentRecorder<T> : IDisposable where T : struct
{
private readonly MeterListener _meterListener = new();
private readonly ConcurrentQueue<Measurement<T>> _values = new();

public InstrumentRecorder(string instrumentName)
{
_meterListener.InstrumentPublished = (instrument, listener) =>
{
if (instrument.Meter.Name == "System.Runtime" && instrument.Name == instrumentName)
{
listener.EnableMeasurementEvents(instrument);
}
};
_meterListener.SetMeasurementEventCallback<T>(OnMeasurementRecorded);
_meterListener.Start();
}

private void OnMeasurementRecorded(Instrument instrument, T measurement, ReadOnlySpan<KeyValuePair<string, object?>> tags, object? state) =>
_values.Enqueue(new Measurement<T>(measurement, tags));

public IReadOnlyList<Measurement<T>> GetMeasurements() => _values.ToArray();

public void RecordObservableInstruments() => _meterListener.RecordObservableInstruments();

public void Dispose() => _meterListener.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Compile Include="MetricsAdviceTests.cs" />
<Compile Include="MetricsTests.cs" />
<Compile Include="PropagatorTests.cs" />
<Compile Include="RuntimeMetricsTests.cs" />
<Compile Include="TagListTests.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
Expand Down