-
Notifications
You must be signed in to change notification settings - Fork 715
/
EventPipeParsing.cs
161 lines (127 loc) · 5.87 KB
/
EventPipeParsing.cs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Etlx;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;
using Xunit.Abstractions;
namespace TraceEventTests
{
public class EventPipeParsing : EventPipeTestBase
{
private class EventRecord
{
public int TotalCount;
public string FirstSeriazliedSample;
}
public EventPipeParsing(ITestOutputHelper output)
: base(output)
{
}
[Theory()]
[MemberData(nameof(TestEventPipeFiles))]
public void Basic(string eventPipeFileName)
{
// Initialize
PrepareTestData();
string eventPipeFilePath = Path.Combine(UnZippedDataDir, eventPipeFileName);
Output.WriteLine(string.Format("Processing the file {0}, Making ETLX and scanning.", Path.GetFullPath(eventPipeFilePath)));
var eventStatistics = new SortedDictionary<string, EventRecord>(StringComparer.Ordinal);
using (var traceLog = new TraceLog(TraceLog.CreateFromEventPipeDataFile(eventPipeFilePath)))
{
var traceSource = traceLog.Events.GetSource();
traceSource.AllEvents += delegate (TraceEvent data)
{
string eventName = data.ProviderName + "/" + data.EventName;
if (eventStatistics.ContainsKey(eventName))
{
eventStatistics[eventName].TotalCount++;
}
else
{
eventStatistics[eventName] = new EventRecord()
{
TotalCount = 1,
FirstSeriazliedSample = new String(data.ToString().Replace("\n", "\\n").Replace("\r", "\\r").Take(1000).ToArray())
};
}
};
// Process
traceSource.Process();
}
// Validate
ValidateEventStatistics(eventStatistics, eventPipeFileName);
}
[Fact]
public void CanParseHeaderOfV1EventPipeFile()
{
PrepareTestData();
const string eventPipeFileName = "eventpipe-dotnetcore2.0-linux-objver1.netperf";
string eventPipeFilePath = Path.Combine(UnZippedDataDir, eventPipeFileName);
using (var eventPipeSource = new EventPipeEventSource(eventPipeFilePath))
{
Assert.Equal(8, eventPipeSource.PointerSize);
Assert.Equal(0, eventPipeSource._processId);
Assert.Equal(1, eventPipeSource.NumberOfProcessors);
Assert.Equal(636414354195130000, eventPipeSource._syncTimeUTC.Ticks);
Assert.Equal(1477613380157300, eventPipeSource._syncTimeQPC);
Assert.Equal(1000000000, eventPipeSource._QPCFreq);
Assert.Equal(10, eventPipeSource.CpuSpeedMHz);
}
}
[Fact]
public void CanParseHeaderOfV3EventPipeFile()
{
PrepareTestData();
const string eventPipeFileName = "eventpipe-dotnetcore2.1-win-x86-objver3.netperf";
string eventPipeFilePath = Path.Combine(UnZippedDataDir, eventPipeFileName);
using (var eventPipeSource = new EventPipeEventSource(eventPipeFilePath))
{
Assert.Equal(4, eventPipeSource.PointerSize);
Assert.Equal(11376, eventPipeSource._processId);
Assert.Equal(4, eventPipeSource.NumberOfProcessors);
Assert.Equal(1000000, eventPipeSource._expectedCPUSamplingRate);
Assert.Equal(636522350205880000, eventPipeSource._syncTimeUTC.Ticks);
Assert.Equal(44518740604, eventPipeSource._syncTimeQPC);
Assert.Equal(2533308, eventPipeSource._QPCFreq);
Assert.Equal(10, eventPipeSource.CpuSpeedMHz);
}
}
[Fact]
public void AllEventsLeavesNoUnhandledEvents()
{
PrepareTestData();
const string eventPipeFileName = "eventpipe-dotnetcore2.1-win-x86-objver3.netperf";
string eventPipeFilePath = Path.Combine(UnZippedDataDir, eventPipeFileName);
using (var traceSource = new TraceLog(TraceLog.CreateFromEventPipeDataFile(eventPipeFilePath)).Events.GetSource())
{
int dynamicAllInvocationCount = 0;
int unhandledEvents = 0;
traceSource.AllEvents += _ => dynamicAllInvocationCount++;
traceSource.UnhandledEvents += _ => unhandledEvents++;
traceSource.Process();
Assert.NotEqual(0, dynamicAllInvocationCount);
Assert.Equal(0, unhandledEvents);
}
}
private void ValidateEventStatistics(SortedDictionary<string, EventRecord> eventStatistics, string eventPipeFileName)
{
StringBuilder sb = new StringBuilder(1024 * 1024);
foreach (var item in eventStatistics)
{
sb.AppendLine($"{item.Key}, {item.Value.TotalCount}, {item.Value.FirstSeriazliedSample}");
}
string actual = sb.ToString();
string baselineFile = Path.Combine(TestDataDir, eventPipeFileName + ".baseline.txt");
string expected = File.ReadAllText(baselineFile);
if (!string.Equals(actual, expected, StringComparison.Ordinal))
{
string eventStatisticsFile = Path.Combine(TestDataDir, eventPipeFileName + ".actual.txt");
File.WriteAllText(eventStatisticsFile, actual, Encoding.UTF8);
Assert.True(false, $"The event statistics doesn't match {Path.GetFullPath(baselineFile)}. It's saved in {Path.GetFullPath(eventStatisticsFile)}.");
}
}
}
}