summaryrefslogtreecommitdiff
path: root/tests/src/tracing/eventpipetrace/EventPipeTrace.cs
blob: af0c619ef9b01399b45a030817a5bc90f37026e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
{
    class EventPipeTrace
    {
        private static int allocIterations = 10000;
        private static int gcIterations = 10;

        static void AssertEqual<T>(T left, T right) where T : IEquatable<T>
        {
            if (left.Equals(right) == false)
            {
                throw new Exception(string.Format("Values were not equal! {0} and {1}", left, right));
            }
        }

        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: Generating CLR events");
                // Allocate for allocIterations iterations.
                for(int i=0; i<allocIterations; i++)
                {
                    GC.KeepAlive(new object());
                }
                // GC gcIternation times
                for(int i=0; i<gcIterations; i++)
                {
                    GC.Collect();
                }
                Console.WriteLine("\tEnd: Generating CLR Events\n");

                Console.WriteLine("\tStart: Disable tracing.");
                TraceControl.Disable();
                Console.WriteLine("\tEnd: Disable tracing.\n");

                Console.WriteLine("\tStart: Processing events from file.");
                int allocTickCount = 0;
                int gcTriggerCount = 0;
                using (var trace = TraceEventDispatcher.GetDispatcherFromFileName(outputFilename))
                {
                    trace.Clr.GCAllocationTick += delegate(GCAllocationTickTraceData data)
                    {
                        allocTickCount += 1;

                        // Some basic integrity checks
                        // AssertEqual(data.TypeName, "System.Object"); https://github.com/Microsoft/perfview/issues/470
                        AssertEqual(data.AllocationKind.ToString(), GCAllocationKind.Small.ToString());
                        AssertEqual(data.ProviderName, "Microsoft-Windows-DotNETRuntime");
                        AssertEqual(data.EventName, "GC/AllocationTick");
                    };
                    trace.Clr.GCTriggered += delegate(GCTriggeredTraceData data)
                    {
                        gcTriggerCount += 1;

                        // Some basic integrity checks
                        AssertEqual(data.Reason.ToString(), GCReason.Induced.ToString());
                        AssertEqual(data.ProviderName, "Microsoft-Windows-DotNETRuntime");
                        AssertEqual(data.EventName, "GC/Triggered");
                    };

                    trace.Process();
                }
                Console.WriteLine("\tEnd: Processing events from file.\n");

                Console.WriteLine("\tProcessed {0} GCAllocationTick events", allocTickCount);
                Console.WriteLine("\tProcessed {0} GCTriggered events", gcTriggerCount);

                pass &= allocTickCount > 0;
                pass &= gcTriggerCount == gcIterations;
            }
            finally {
                if (keepOutput)
                {
                    Console.WriteLine("\n\tOutput file: {0}", outputFilename);
                }
                else
                {
                    System.IO.File.Delete(outputFilename);
                }
            }

            return pass ? 100 : 0;
        }
    }
}