From e0b1daf77fbbf952a8eabbc28936ceb1db73f775 Mon Sep 17 00:00:00 2001 From: itn3000 Date: Tue, 9 Nov 2021 03:18:16 +0900 Subject: [PATCH 1/2] add test for exceeding MaxHistograms and not exceeding MaxTimeSeries(#60752) --- .../tests/MetricEventSourceTests.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricEventSourceTests.cs b/src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricEventSourceTests.cs index 1f3a8d13271e3..bb8209d8c8416 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricEventSourceTests.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricEventSourceTests.cs @@ -704,6 +704,45 @@ public void EventSourceWorksWithSequentialListeners() AssertCollectStartStopEventsPresent(events, IntervalSecs, 3); } + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] + [OuterLoop("Slow and has lots of console spew")] + public void EventSourceEnforcesHistogramLimitAndNotMaxTimeSeries() + { + using Meter meter = new Meter("TestMeter17"); + Histogram h = meter.CreateHistogram("histogram1"); + + + EventWrittenEventArgs[] events; + using (MetricsEventListener listener = new MetricsEventListener(_output, MetricsEventListener.TimeSeriesValues, IntervalSecs, 3, 2, "TestMeter17")) + { + listener.WaitForCollectionStop(s_waitForEventTimeout, 1); + + h.Record(5, new KeyValuePair("Color", "red")); + h.Record(6, new KeyValuePair("Color", "blue")); + h.Record(7, new KeyValuePair("Color", "green")); + h.Record(8, new KeyValuePair("Color", "yellow")); + listener.WaitForCollectionStop(s_waitForEventTimeout, 2); + + h.Record(12, new KeyValuePair("Color", "red")); + h.Record(13, new KeyValuePair("Color", "blue")); + h.Record(14, new KeyValuePair("Color", "green")); + h.Record(15, new KeyValuePair("Color", "yellow")); + listener.WaitForCollectionStop(s_waitForEventTimeout, 3); + events = listener.Events.ToArray(); + } + + AssertBeginInstrumentReportingEventsPresent(events, h); + AssertInitialEnumerationCompleteEventPresent(events); + AssertHistogramEventsPresent(events, meter.Name, h.Name, "Color=red", "", "0.5=5;0.95=5;0.99=5", "0.5=12;0.95=12;0.99=12"); + AssertHistogramEventsPresent(events, meter.Name, h.Name, "Color=blue", "", "0.5=6;0.95=6;0.99=6", "0.5=13;0.95=13;0.99=13"); + AssertHistogramLimitPresent(events); + AssertTimeSeriesLimitNotPresent(events); + AssertHistogramEventsNotPresent(events, meter.Name, h.Name, "Color=green"); + AssertHistogramEventsNotPresent(events, meter.Name, h.Name, "Color=yellow"); + AssertCollectStartStopEventsPresent(events, IntervalSecs, 3); + } + + private void AssertBeginInstrumentReportingEventsPresent(EventWrittenEventArgs[] events, params Instrument[] expectedInstruments) { var beginReportEvents = events.Where(e => e.EventName == "BeginInstrumentReporting").Select(e => @@ -766,6 +805,11 @@ private void AssertTimeSeriesLimitPresent(EventWrittenEventArgs[] events) Assert.Equal(1, events.Where(e => e.EventName == "TimeSeriesLimitReached").Count()); } + private void AssertTimeSeriesLimitNotPresent(EventWrittenEventArgs[] events) + { + Assert.Equal(0, events.Where(e => e.EventName == "TimeSeriesLimitReached").Count()); + } + private void AssertHistogramLimitPresent(EventWrittenEventArgs[] events) { Assert.Equal(1, events.Where(e => e.EventName == "HistogramLimitReached").Count()); From ea3fd446a6f9e3c050e961e3e51c2b1c93a07d1b Mon Sep 17 00:00:00 2001 From: itn3000 Date: Thu, 11 Nov 2021 10:58:48 +0900 Subject: [PATCH 2/2] add comment in test --- .../tests/MetricEventSourceTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricEventSourceTests.cs b/src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricEventSourceTests.cs index bb8209d8c8416..a4fc6695ea390 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricEventSourceTests.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricEventSourceTests.cs @@ -713,6 +713,8 @@ public void EventSourceEnforcesHistogramLimitAndNotMaxTimeSeries() EventWrittenEventArgs[] events; + // MaxTimeSeries = 3, MaxHistograms = 2 + // HistogramLimitReached should be raised when Record(tags: "Color=green"), but TimeSeriesLimitReached should not be raised using (MetricsEventListener listener = new MetricsEventListener(_output, MetricsEventListener.TimeSeriesValues, IntervalSecs, 3, 2, "TestMeter17")) { listener.WaitForCollectionStop(s_waitForEventTimeout, 1);