diff options
author | Sung Yoon Whang <suwhang@microsoft.com> | 2019-07-03 15:50:46 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-03 15:50:46 -0700 |
commit | f36453660859bb88875b655922b4c3e8d28da4c0 (patch) | |
tree | ba63a3ceb67279cb947e065d25e8956824587be0 /tests | |
parent | 234f1b6b238a9faa860a0bd1253d4baabdb35759 (diff) | |
download | coreclr-f36453660859bb88875b655922b4c3e8d28da4c0.tar.gz coreclr-f36453660859bb88875b655922b4c3e8d28da4c0.tar.bz2 coreclr-f36453660859bb88875b655922b4c3e8d28da4c0.zip |
Add test for eventcounter (#25555)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/tracing/eventcounter/eventcounter.cs | 162 | ||||
-rw-r--r-- | tests/src/tracing/eventcounter/eventcounter.csproj | 30 |
2 files changed, 192 insertions, 0 deletions
diff --git a/tests/src/tracing/eventcounter/eventcounter.cs b/tests/src/tracing/eventcounter/eventcounter.cs new file mode 100644 index 0000000000..fffd1c8305 --- /dev/null +++ b/tests/src/tracing/eventcounter/eventcounter.cs @@ -0,0 +1,162 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#if USE_MDT_EVENTSOURCE +using Microsoft.Diagnostics.Tracing; +#else +using System.Diagnostics.Tracing; +#endif +using System; +using System.Collections.Generic; +using System.Threading; + +namespace BasicEventSourceTests +{ + public partial class TestEventCounter + { + [EventSource(Name = "SimpleEventSource")] + private sealed class SimpleEventSource : EventSource + { + private EventCounter _myCounter; + + public SimpleEventSource(string _displayName, string _displayUnits) + { + _myCounter = new EventCounter("test-counter", this) { DisplayName = _displayName, DisplayUnits = _displayUnits }; + } + + public void WriteOne() + { + _myCounter.WriteMetric(1); + } + } + + internal sealed class SimpleEventListener : EventListener + { + private readonly string _targetSourceName; + private readonly EventLevel _level; + private Dictionary<string, string> args; + + public HashSet<int> means; + public string displayName; + public string displayUnits; + public int callbackCount; + + public SimpleEventListener(string targetSourceName, EventLevel level) + { + // Store the arguments + _targetSourceName = targetSourceName; + _level = level; + displayName = ""; + displayUnits = ""; + callbackCount = 0; + means = new HashSet<int>(); + args = new Dictionary<string, string>(); + args.Add("EventCounterIntervalSec", "1"); + } + + protected override void OnEventSourceCreated(EventSource source) + { + if (source.Name.Equals(_targetSourceName)) + { + EnableEvents(source, _level, (EventKeywords)(-1), args); + } + } + + protected override void OnEventWritten(EventWrittenEventArgs eventData) + { + if (eventData.EventName.Equals("EventCounters")) + { + for (int i = 0; i < eventData.Payload.Count; i++) + { + // Decode the payload + IDictionary<string, object> eventPayload = eventData.Payload[i] as IDictionary<string, object>; + foreach (KeyValuePair<string, object> payload in eventPayload) + { + if (payload.Key.Equals("Mean")) + { + int mean = Int32.Parse(payload.Value.ToString()); + means.Add(mean); + } + else if (payload.Key.Equals("DisplayName")) + { + displayName = payload.Value.ToString(); + } + else if (payload.Key.Equals("DisplayUnits")) + { + displayUnits = payload.Value.ToString(); + } + } + } + callbackCount++; + } + } + + public bool validateMean() + { + // we expect to see 1 because we wrote only 1s + if (!means.Contains(1)) + { + return false; + } + + // we also expect to see 0 because there is a period of time we didn't write stuff and got callback + if (!means.Contains(0)) + { + return false; + } + + return true; + } + } + + public static int Main(string[] args) + { + // Create an EventListener. + using (SimpleEventListener myListener = new SimpleEventListener("SimpleEventSource", EventLevel.Verbose)) + { + string displayName = "Mock Counter"; + string displayUnits = "Count"; + + SimpleEventSource eventSource = new SimpleEventSource(displayName, displayUnits); + int iter = 100; + + // increment 100 times + for (int i = 0; i < iter; i++) + { + eventSource.WriteOne(); + } + + Thread.Sleep(3000); + + if (!myListener.validateMean()) + { + Console.WriteLine("Test Failed - Incorrect mean calculation"); + return 1; + } + + if (displayName != myListener.displayName) + { + Console.WriteLine("Test Failed"); + Console.WriteLine($"Expected to see {displayName} as DisplayName property in payload - saw {myListener.displayName}"); + return 1; + } + + if (displayUnits != myListener.displayUnits) + { + Console.WriteLine("Test Failed"); + Console.WriteLine($"Expected to see {displayUnits} as DisplayUnits property in payload - saw {myListener.displayUnits}"); + return 1; + } + + if (myListener.callbackCount == 0) + { + Console.WriteLine("Test Failed: Expected to see 1 or more EventListener callback but got none"); + } + + Console.WriteLine("Test passed"); + return 100; + } + } + } +}
\ No newline at end of file diff --git a/tests/src/tracing/eventcounter/eventcounter.csproj b/tests/src/tracing/eventcounter/eventcounter.csproj new file mode 100644 index 0000000000..eb1c1f9d03 --- /dev/null +++ b/tests/src/tracing/eventcounter/eventcounter.csproj @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{8E3244CB-407F-4142-BAAB-E7A55901A5FA}</ProjectGuid> + <OutputType>Exe</OutputType> + <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir> + <CLRTestKind>BuildAndRun</CLRTestKind> + <DefineConstants>$(DefineConstants);STATIC</DefineConstants> + <AllowUnsafeBlocks>true</AllowUnsafeBlocks> + <CLRTestPriority>0</CLRTestPriority> + <GCStressIncompatible>true</GCStressIncompatible> + <!-- This test has a secondary thread with an infinite loop --> + <UnloadabilityIncompatible>true</UnloadabilityIncompatible> + </PropertyGroup> + <!-- Default configurations to help VS understand the configurations --> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> + </PropertyGroup> + <ItemGroup> + <Compile Include="eventcounter.cs" /> + <ProjectReference Include="../common/common.csproj" /> + </ItemGroup> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> +</Project> |