Add New EventPipe Tests (#16130)
authorBrian Robbins <brianrob@microsoft.com>
Fri, 2 Feb 2018 02:48:34 +0000 (18:48 -0800)
committerGitHub <noreply@github.com>
Fri, 2 Feb 2018 02:48:34 +0000 (18:48 -0800)
tests/src/tracing/common/Assert.cs
tests/src/tracing/eventactivityidcontrol/EventActivityIdControl.cs
tests/src/tracing/tracevalidation/inducedgc/InducedGC.cs [new file with mode: 0644]
tests/src/tracing/tracevalidation/inducedgc/inducedgc.csproj [new file with mode: 0644]
tests/src/tracing/tracevalidation/jittingstarted/JittingStarted.cs [new file with mode: 0644]
tests/src/tracing/tracevalidation/jittingstarted/JittingStarted.csproj [new file with mode: 0644]

index c538ecc..fa47281 100755 (executable)
@@ -4,36 +4,36 @@ namespace Tracing.Tests.Common
 {
     public static class Assert
     {
-        public static void Equal<T>(T left, T right) where T : IEquatable<T>
+        public static void Equal<T>(string name, T left, T right) where T : IEquatable<T>
         {
             if (left == null && right != null)
             {
                 throw new Exception(
-                    string.Format("Values are not equal!  Left=NULL Right='{0}'", right));
+                    string.Format("Values for '{0}' are not equal!  Left=NULL Right='{1}'", name, right));
             }
             else if (left != null && right == null)
             {
                 throw new Exception(
-                    string.Format("Values are not equal!  Left='{0}' Right=NULL", left));
+                    string.Format("Values for '{0}' are not equal!  Left='{1}' Right=NULL", name, left));
             }
             else if (!left.Equals(right))
             {
                 throw new Exception(
-                    string.Format("Values are not equal! Left='{0}' Right='{1}'", left, right));
+                    string.Format("Values for '{0}' are not equal! Left='{1}' Right='{2}'", name, left, right));
             }
         }
 
-        public static void NotEqual<T>(T left, T right) where T : IEquatable<T>
+        public static void NotEqual<T>(string name, T left, T right) where T : IEquatable<T>
         {
             if (left == null && right == null)
             {
                 throw new Exception(
-                    "Values are equal! Left=NULL Right=NULL");
+                    string.Format("Values for '{0}' are equal! Left=NULL Right=NULL", name));
             }
             else if (left != null && left.Equals(right))
             {
                 throw new Exception(
-                    string.Format("Values are equal! Left='{0}' Right='{1}'", left, right));
+                    string.Format("Values for '{0}' are equal! Left='{1}' Right='{2}'", name, left, right));
             }
         }
     }
index 1a7e288..c650234 100755 (executable)
@@ -70,22 +70,22 @@ namespace Tracing.Tests
                 int retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_GET_ID,
                     ref activityId);
-                Assert.Equal<int>(retCode, 0);
-                Assert.Equal<Guid>(activityId, Guid.Empty);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
+                Assert.Equal<Guid>(nameof(activityId), activityId, Guid.Empty);
 
                 // Set the activity ID to a random GUID and then confirm that it was properly set.
                 activityId = Guid.NewGuid();
                 retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_SET_ID,
                     ref activityId);
-                Assert.Equal<int>(retCode, 0);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
 
                 Guid currActivityId = Guid.Empty;
                 retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_GET_ID,
                     ref currActivityId);
-                Assert.Equal<int>(retCode, 0);
-                Assert.Equal<Guid>(currActivityId, activityId);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
+                Assert.Equal<Guid>(nameof(currActivityId), currActivityId, activityId);
 
                 // Set and get the activity ID in one call.
                 activityId = Guid.NewGuid();
@@ -93,63 +93,63 @@ namespace Tracing.Tests
                 retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_GET_SET_ID,
                     ref activityId);
-                Assert.Equal<int>(retCode, 0);
-                Assert.Equal<Guid>(currActivityId, activityId);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
+                Assert.Equal<Guid>(nameof(currActivityId), currActivityId, activityId);
 
                 // Validate that the value we specified in the previous call is what comes back from a call to Get.
                 retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_GET_ID,
                     ref activityId);
-                Assert.Equal<int>(retCode, 0);
-                Assert.Equal<Guid>(savedActivityId, activityId);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
+                Assert.Equal<Guid>(nameof(savedActivityId), savedActivityId, activityId);
 
                 // Create a new ID but don't change the current value.
                 Guid newActivityId = Guid.Empty;
                 retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_CREATE_ID,
                     ref newActivityId);
-                Assert.Equal<int>(retCode, 0);
-                Assert.NotEqual<Guid>(newActivityId, Guid.Empty);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
+                Assert.NotEqual<Guid>(nameof(newActivityId), newActivityId, Guid.Empty);
 
                 retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_GET_ID,
                     ref activityId);
-                Assert.Equal<int>(retCode, 0);
-                Assert.Equal<Guid>(savedActivityId, activityId);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
+                Assert.Equal<Guid>(nameof(savedActivityId), savedActivityId, activityId);
 
                 // Create a new ID and set it in one action.
                 retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_CREATE_SET_ID,
                     ref activityId);
-                Assert.Equal<int>(retCode, 0);
-                Assert.Equal<Guid>(savedActivityId, activityId);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
+                Assert.Equal<Guid>(nameof(savedActivityId), savedActivityId, activityId);
 
                 savedActivityId = activityId;
                 retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_GET_ID,
                     ref activityId);
-                Assert.Equal<int>(retCode, 0);
-                Assert.NotEqual<Guid>(savedActivityId, activityId);
-                Assert.NotEqual<Guid>(activityId, Guid.Empty);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
+                Assert.NotEqual<Guid>(nameof(savedActivityId), savedActivityId, activityId);
+                Assert.NotEqual<Guid>(nameof(activityId), activityId, Guid.Empty);
 
                 retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_GET_ID,
                     ref newActivityId);
-                Assert.Equal<int>(retCode, 0);
-                Assert.Equal<Guid>(activityId, newActivityId);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
+                Assert.Equal<Guid>(nameof(activityId), activityId, newActivityId);
 
                 // Set the activity ID back to zero.
                 activityId = Guid.Empty;
                 retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_SET_ID,
                     ref activityId);
-                Assert.Equal<int>(retCode, 0);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
 
                 retCode = EventActivityIdControl(
                     ActivityControlCode.EVENT_ACTIVITY_CTRL_GET_ID,
                     ref activityId);
-                Assert.Equal<int>(retCode, 0);
-                Assert.Equal<Guid>(activityId, Guid.Empty);
+                Assert.Equal<int>(nameof(retCode), retCode, 0);
+                Assert.Equal<Guid>(nameof(activityId), activityId, Guid.Empty);
 
                 // Try pass an invalid control code.
                 activityId = Guid.NewGuid();
@@ -157,8 +157,8 @@ namespace Tracing.Tests
                 retCode = EventActivityIdControl(
                     (ActivityControlCode)10,
                     ref activityId);
-                Assert.Equal<int>(retCode, 1);
-                Assert.Equal<Guid>(activityId, savedActivityId);
+                Assert.Equal<int>(nameof(retCode), retCode, 1);
+                Assert.Equal<Guid>(nameof(activityId), activityId, savedActivityId);
             }
             catch(Exception ex)
             {
diff --git a/tests/src/tracing/tracevalidation/inducedgc/InducedGC.cs b/tests/src/tracing/tracevalidation/inducedgc/InducedGC.cs
new file mode 100644 (file)
index 0000000..c7c535f
--- /dev/null
@@ -0,0 +1,92 @@
+using System;
+using System.IO;
+using Tracing.Tests.Common;
+using Microsoft.Diagnostics.Tracing;
+using Microsoft.Diagnostics.Tracing.Parsers;
+using Microsoft.Diagnostics.Tracing.Parsers.Clr;
+
+namespace Tracing.Tests
+{
+    public static class TraceValidationInducedGC
+    {
+        private static int InducedGCIterations = 10;
+
+        public static int Main(string[] args)
+        {
+            bool pass = true;
+            bool keepOutput = false;
+
+            // Use the first arg as an output filename if there is one.
+            string outputFilename = null;
+            if (args.Length >= 1)
+            {
+                outputFilename = args[0];
+                keepOutput = true;
+            }
+            else
+            {
+                outputFilename = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".netperf";
+            }
+
+            try
+            {
+                Console.WriteLine("\tStart: Enable tracing.");
+                TraceControl.EnableDefault(outputFilename);
+                Console.WriteLine("\tEnd: Enable tracing.\n");
+
+                Console.WriteLine("\tStart: Generate some events.");
+                for(int i=0; i<InducedGCIterations; i++)
+                {
+                    GC.Collect();
+                }
+                Console.WriteLine("\tEnd: Generate some events.\n");
+
+                Console.WriteLine("\tStart: Disable tracing.");
+                TraceControl.Disable();
+                Console.WriteLine("\tEnd: Disable tracing.\n");
+
+                Console.WriteLine("\tStart: Process the trace file.");
+                int matchingEventCount = 0;
+                int nonMatchingEventCount = 0;
+                using (var trace = TraceEventDispatcher.GetDispatcherFromFileName(outputFilename))
+                {
+                    string gcReasonInduced = GCReason.Induced.ToString();
+                    string providerName = "Microsoft-Windows-DotNETRuntime";
+                    string gcTriggeredEventName = "GC/Triggered";
+
+                    trace.Clr.GCTriggered += delegate(GCTriggeredTraceData data)
+                    {
+                        if(gcReasonInduced.Equals(data.Reason.ToString()) &&
+                           providerName.Equals(data.ProviderName) &&
+                           gcTriggeredEventName.Equals(data.EventName))
+                        {
+                            matchingEventCount++;
+                        }
+                        else
+                        {
+                            nonMatchingEventCount++;
+                        }
+                    };
+
+                    trace.Process();
+                }
+                Console.WriteLine("\tEnd: Processing events from file.\n");
+
+                Assert.Equal(nameof(matchingEventCount), InducedGCIterations, matchingEventCount);
+                Assert.Equal(nameof(nonMatchingEventCount), nonMatchingEventCount, 0);
+            }
+            finally {
+                if (keepOutput)
+                {
+                    Console.WriteLine("\n\tOutput file: {0}", outputFilename);
+                }
+                else
+                {
+                    System.IO.File.Delete(outputFilename);
+                }
+            }
+
+            return 100;
+        }
+    }
+}
diff --git a/tests/src/tracing/tracevalidation/inducedgc/inducedgc.csproj b/tests/src/tracing/tracevalidation/inducedgc/inducedgc.csproj
new file mode 100644 (file)
index 0000000..d4bb1f0
--- /dev/null
@@ -0,0 +1,29 @@
+<?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>
+    <CLRTestPriority>0</CLRTestPriority>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"></PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"></PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="InducedGC.cs" />
+    <ProjectReference Include="../../common/common.csproj" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
\ No newline at end of file
diff --git a/tests/src/tracing/tracevalidation/jittingstarted/JittingStarted.cs b/tests/src/tracing/tracevalidation/jittingstarted/JittingStarted.cs
new file mode 100644 (file)
index 0000000..4a65075
--- /dev/null
@@ -0,0 +1,112 @@
+using System;
+using System.IO;
+using System.Reflection;
+using System.Reflection.Emit;
+using System.Runtime.CompilerServices;
+using Tracing.Tests.Common;
+using Microsoft.Diagnostics.Tracing;
+using Microsoft.Diagnostics.Tracing.Parsers;
+using Microsoft.Diagnostics.Tracing.Parsers.Clr;
+
+namespace Tracing.Tests
+{
+    public static class TraceValidationJittingStarted
+    {
+        // Delegate declaration matching the signature of the dynamic method.
+        private delegate void DynamicallyCompiledMethodInvoker();
+
+        public static int Main(string[] args)
+        {
+            bool pass = true;
+            bool keepOutput = false;
+
+            // Use the first arg as an output filename if there is one.
+            string outputFilename = null;
+            if (args.Length >= 1)
+            {
+                outputFilename = args[0];
+                keepOutput = true;
+            }
+            else
+            {
+                outputFilename = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".netperf";
+            }
+
+            try
+            {
+                Console.WriteLine("\tStart: Enable tracing.");
+                TraceControl.EnableDefault(outputFilename);
+                Console.WriteLine("\tEnd: Enable tracing.\n");
+
+                Console.WriteLine("\tStart: Generate some events.");
+                DynamicallyCompiledMethodInvoker invoker = BuildDynamicMethod();
+                invoker.Invoke();
+                Console.WriteLine("\tEnd: Generate some events.\n");
+
+                Console.WriteLine("\tStart: Disable tracing.");
+                TraceControl.Disable();
+                Console.WriteLine("\tEnd: Disable tracing.\n");
+
+                Console.WriteLine("\tStart: Process the trace file.");
+
+                int matchingEventCount = 0;
+                int nonMatchingEventCount = 0;
+
+                using (var trace = TraceEventDispatcher.GetDispatcherFromFileName(outputFilename))
+                {
+                    string methodNamespace = "dynamicClass";
+                    string methodName = "DynamicallyCompiledMethod";
+                    string methodSignature = "void  ()";
+                    string providerName = "Microsoft-Windows-DotNETRuntime";
+                    string gcTriggeredEventName = "Method/JittingStarted";
+
+                    trace.Clr.MethodJittingStarted += delegate(MethodJittingStartedTraceData data)
+                    {
+                        if(methodNamespace.Equals(data.MethodNamespace) &&
+                           methodName.Equals(data.MethodName) &&
+                           methodSignature.Equals(data.MethodSignature) &&
+                           providerName.Equals(data.ProviderName) &&
+                           gcTriggeredEventName.Equals(data.EventName))
+                        {
+                            matchingEventCount++;
+                        }
+                    };
+
+                    trace.Process();
+                }
+                Console.WriteLine("\tEnd: Processing events from file.\n");
+
+                // CompiledMethod
+                Assert.Equal(nameof(matchingEventCount), matchingEventCount, 1);
+            }
+            finally
+            {
+                if (keepOutput)
+                {
+                    Console.WriteLine("\n\tOutput file: {0}", outputFilename);
+                }
+                else
+                {
+                    System.IO.File.Delete(outputFilename);
+                }
+            }
+
+            return 100;
+        }
+
+        private static DynamicallyCompiledMethodInvoker BuildDynamicMethod()
+        {
+            Type[] methodArgs = { };
+
+            DynamicMethod dynamicMethod = new DynamicMethod(
+                "DynamicallyCompiledMethod",
+                typeof(void),
+                methodArgs);
+
+            ILGenerator il = dynamicMethod.GetILGenerator();
+            il.Emit(OpCodes.Ret);
+
+            return (DynamicallyCompiledMethodInvoker)dynamicMethod.CreateDelegate(typeof(DynamicallyCompiledMethodInvoker));
+        }
+    }
+}
diff --git a/tests/src/tracing/tracevalidation/jittingstarted/JittingStarted.csproj b/tests/src/tracing/tracevalidation/jittingstarted/JittingStarted.csproj
new file mode 100644 (file)
index 0000000..36f9623
--- /dev/null
@@ -0,0 +1,29 @@
+<?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>
+    <CLRTestPriority>0</CLRTestPriority>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"></PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"></PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="JittingStarted.cs" />
+    <ProjectReference Include="../../common/common.csproj" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
\ No newline at end of file