Skip to content

Commit e2c515f

Browse files
authored
Output perfView events (#64)
1 parent 4a277e4 commit e2c515f

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

Profiler/Profiler.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
RootModule = 'Profiler.psm1'
99

1010
# Version number of this module.
11-
ModuleVersion = '4.0.1'
11+
ModuleVersion = '4.1.0'
1212

1313
# Supported PSEditions
1414
# CompatiblePSEditions = @()

csharp/Profiler.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.30431.218
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33530.505
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Profiler", "Profiler\Profiler.csproj", "{84FC4143-E62C-469F-82F2-6575045EC0B6}"
77
EndProject

csharp/Profiler/ProfilerTracer.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Diagnostics.Tracing;
45
using System.Management.Automation;
56
using System.Management.Automation.Language;
67

78
namespace Profiler
89
{
10+
[EventSource(Name = "Profiler")]
11+
public class ProfilerEventSource : EventSource
12+
{
13+
private ProfilerEventSource() { }
14+
15+
public void WriteStart(int index, string functionName, string text)
16+
{
17+
WriteEvent(1, index, AsNonNull(functionName), Max100Chars(text));
18+
}
19+
20+
public void WriteStop(int index, string functionName, string text)
21+
{
22+
WriteEvent(2, index, AsNonNull(functionName), Max100Chars(text));
23+
}
24+
25+
public static ProfilerEventSource Log { get; } = new ProfilerEventSource();
26+
27+
private static string AsNonNull(string functionName)
28+
{
29+
return functionName ?? string.Empty;
30+
}
31+
32+
private static string Max100Chars(string text)
33+
{
34+
text = AsNonNull(text);
35+
if (text.Length > 100)
36+
{
37+
return text.Substring(0, 100);
38+
}
39+
40+
return text;
41+
}
42+
}
43+
944
public class ProfilerTracer : ITracer
1045
{
1146
// timespan ticks are 10k per millisecond, but the stopwatch can have different resolution
@@ -41,9 +76,9 @@ public void Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level, stri
4176
if (_index > 0)
4277
{
4378
SetSelfDurationAndAddToHits(ref _previousHit, timestamp);
79+
ProfilerEventSource.Log.WriteStop(_previousHit.Index, _previousHit.Function, _previousHit.Text);
4480
}
4581

46-
4782
#if !POWERSHELL3
4883
if (!ScriptBlocks.ContainsKey(scriptBlock.Id))
4984
{
@@ -59,7 +94,6 @@ public void Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level, stri
5994
}
6095
}
6196
#endif
62-
6397
// overwrite the previous event because we already scraped it
6498
_previousHit = new Hit();
6599
_previousHit.StartTime = TimeSpan.FromTicks(timestamp);
@@ -85,6 +119,7 @@ public void Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level, stri
85119
_previousHit.Level = level;
86120

87121
_index++;
122+
ProfilerEventSource.Log.WriteStart(_previousHit.Index, _previousHit.Function, _previousHit.Text);
88123
}
89124

90125
private void SetSelfDurationAndAddToHits(ref Hit eventRecord, long timestamp)

0 commit comments

Comments
 (0)