From: Sung Yoon Whang Date: Sat, 23 Nov 2019 02:26:47 +0000 (-0800) Subject: Make dotnet-counters aware of DisplayUnits (#642) X-Git-Tag: submit/tizen/20200402.013218~14^2^2~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=21cf588c9b180f44c1938e9ebcbe29c4396af9f1;p=platform%2Fcore%2Fdotnet%2Fdiagnostics.git Make dotnet-counters aware of DisplayUnits (#642) --- diff --git a/src/Tools/dotnet-counters/CounterPayload.cs b/src/Tools/dotnet-counters/CounterPayload.cs index 6f013e3a0..4f81d5031 100644 --- a/src/Tools/dotnet-counters/CounterPayload.cs +++ b/src/Tools/dotnet-counters/CounterPayload.cs @@ -22,11 +22,17 @@ namespace Microsoft.Diagnostics.Tools.Counters public string m_Name; public double m_Value; public string m_DisplayName; + public string m_DisplayUnits; + public CounterPayload(IDictionary payloadFields) { m_Name = payloadFields["Name"].ToString(); m_Value = (double)payloadFields["Mean"]; m_DisplayName = payloadFields["DisplayName"].ToString(); + m_DisplayUnits = payloadFields["DisplayUnits"].ToString(); + + // In case these properties are not provided, set them to appropriate values. + m_DisplayName = m_DisplayName.Length == 0 ? m_Name : m_DisplayName; } public string GetName() @@ -41,7 +47,11 @@ namespace Microsoft.Diagnostics.Tools.Counters public string GetDisplay() { - return m_DisplayName; + if (m_DisplayUnits.Length > 0) + { + return $"{m_DisplayName} ({m_DisplayUnits})"; + } + return $"{m_DisplayName}"; } public string GetCounterType() @@ -57,6 +67,7 @@ namespace Microsoft.Diagnostics.Tools.Counters public double m_Value; public string m_DisplayName; public string m_DisplayRateTimeScale; + public string m_DisplayUnits; public IncrementingCounterPayload(IDictionary payloadFields, int interval) { @@ -64,6 +75,7 @@ namespace Microsoft.Diagnostics.Tools.Counters m_Value = (double)payloadFields["Increment"]; m_DisplayName = payloadFields["DisplayName"].ToString(); m_DisplayRateTimeScale = payloadFields["DisplayRateTimeScale"].ToString(); + m_DisplayUnits = payloadFields["DisplayUnits"].ToString(); timescaleInSec = m_DisplayRateTimeScale.Length == 0 ? 1 : (int)TimeSpan.Parse(m_DisplayRateTimeScale).TotalSeconds; m_Value *= timescaleInSec; @@ -84,6 +96,9 @@ namespace Microsoft.Diagnostics.Tools.Counters public string GetDisplay() { + if (m_DisplayUnits.Length > 0) + return $"{m_DisplayName} / {m_DisplayRateTimeScale} ({m_DisplayUnits})"; + return $"{m_DisplayName} / {m_DisplayRateTimeScale}"; } diff --git a/src/tests/dotnet-counters/CSVExporterTests.cs b/src/tests/dotnet-counters/CSVExporterTests.cs index cf713a612..cf0529ff9 100644 --- a/src/tests/dotnet-counters/CSVExporterTests.cs +++ b/src/tests/dotnet-counters/CSVExporterTests.cs @@ -142,5 +142,45 @@ namespace DotnetCounters.UnitTests } } + [Fact] + public void DisplayUnitsTest() + { + string fileName = "displayUnitsTest.csv"; + CSVExporter exporter = new CSVExporter(fileName); + exporter.Initialize(); + for (int i = 0; i < 100; i++) + { + exporter.CounterPayloadReceived("myProvider", TestHelpers.GenerateCounterPayload(true, "allocRateGen", i, 60, "Allocation Rate Gen: " + i.ToString(), "MB"), false); + } + exporter.Stop(); + + Assert.True(File.Exists(fileName)); + + try + { + List lines = File.ReadLines(fileName).ToList(); + Assert.Equal(101, lines.Count); // should be 101 including the headers + + string[] headerTokens = lines[0].Split(','); + Assert.Equal("Provider", headerTokens[1]); + Assert.Equal("Counter Name", headerTokens[2]); + Assert.Equal("Counter Type", headerTokens[3]); + Assert.Equal("Mean/Increment", headerTokens[4]); + + for (int i = 1; i < lines.Count; i++) + { + string[] tokens = lines[i].Split(','); + + Assert.Equal("myProvider", tokens[1]); + Assert.Equal($"Allocation Rate Gen: {i-1} / 60 sec (MB)", tokens[2]); + Assert.Equal("Rate", tokens[3]); + Assert.Equal(((i - 1) * 60).ToString(), tokens[4]); + } + } + finally + { + File.Delete(fileName); + } + } } } diff --git a/src/tests/dotnet-counters/JSONExporterTests.cs b/src/tests/dotnet-counters/JSONExporterTests.cs index 27e27c3e4..29ad426fb 100644 --- a/src/tests/dotnet-counters/JSONExporterTests.cs +++ b/src/tests/dotnet-counters/JSONExporterTests.cs @@ -105,6 +105,37 @@ namespace DotnetCounters.UnitTests } } } + + [Fact] + public void DisplayUnitsTest() + { + string fileName = "displayUnitsTest.json"; + JSONExporter exporter = new JSONExporter(fileName, "myProcess.exe"); + exporter.Initialize(); + + for (int i = 0 ; i < 20; i++) + { + exporter.CounterPayloadReceived("myProvider", TestHelpers.GenerateCounterPayload(false, "heapSize", (double)i, 0, "Heap Size", "MB"), false); + } + exporter.Stop(); + + Assert.True(File.Exists(fileName)); + using (StreamReader r = new StreamReader(fileName)) + { + string json = r.ReadToEnd(); + JSONCounterTrace counterTrace = JsonConvert.DeserializeObject(json); + Assert.Equal("myProcess.exe", counterTrace.targetProcess); + Assert.Equal(20, counterTrace.events.Length); + var i = 0; + foreach(JSONCounterPayload payload in counterTrace.events) + { + Assert.Equal("myProvider", payload.provider); + Assert.Equal("Heap Size (MB)", payload.name); + Assert.Equal(i, payload.value); + i += 1; + } + } + } } class JSONCounterPayload diff --git a/src/tests/dotnet-counters/TestHelpers.cs b/src/tests/dotnet-counters/TestHelpers.cs index c1d1a7cc9..067db7609 100644 --- a/src/tests/dotnet-counters/TestHelpers.cs +++ b/src/tests/dotnet-counters/TestHelpers.cs @@ -17,7 +17,8 @@ namespace DotnetCounters.UnitTests string counterName, double counterValue, int displayRateTimeScaleSeconds = 0, - string displayName = "") + string displayName = "", + string displayUnits = "") { if (isIncrementingCounter) { @@ -27,6 +28,7 @@ namespace DotnetCounters.UnitTests { "Increment", counterValue }, { "DisplayName", displayName }, { "DisplayRateTimeScale", displayRateTimeScaleSeconds == 0 ? "" : TimeSpan.FromSeconds(displayRateTimeScaleSeconds).ToString() }, + { "DisplayUnits", displayUnits }, }; ICounterPayload payload = new IncrementingCounterPayload(payloadFields, 1); return payload; @@ -38,6 +40,7 @@ namespace DotnetCounters.UnitTests { "Name", counterName }, { "Mean", counterValue }, { "DisplayName", displayName }, + { "DisplayUnits", displayUnits }, }; ICounterPayload payload = new CounterPayload(payloadFields); return payload;