-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-RegionsXml.ps1
218 lines (187 loc) · 6.69 KB
/
New-RegionsXml.ps1
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<#
.SYNOPSIS
Create a WPA-compatible "Regions of Interest" file given a set of Generic Events and the corresponding event Provider info.
.DESCRIPTION
Create a WPA-compatible "Regions of Interest" file given a set of Generic Events and the corresponding event Provider info.
.PARAMETER RootName
The name of the top-most scenario / region. This should align to the name of your app or your top-level user flow.
.PARAMETER Measures
A collection of one or more objects that represents a measure that should be made a region.
A measure needs a **Name**, a **Start** event, and a **Stop** event.
.PARAMETER Path
The path where the Regions of Interest files should be saved.
Defaults to the current directory.
.PARAMETER EventProviders
A collection of one or more objects that represents an event provider.
A Regsions of Interest file will be created for each event provider.
An event provider needs a **Provider** GUID, an event **ID**, an event **Version**, and a **FieldName**.
These values can all be gathered from inspecting the Generic Events table in WPA.
By default the EventProviders are the performance.mark() providers for Edge and Chrome.
For more information about using performance.mark() and ETW, see https://matt.kotsenas.com/posts/using-wpa-to-analyze-performance-marks
.EXAMPLE
PS> $measures = @(@{Name = "Widget Load"; Start = "WidgetLoad-Start"; Stop = "WidgetLoad-End"}, @{Name = "Flyout Animation"; Start = "animation.flyout.begin"; Stop = "animation.flyout.end"})
PS> $measures | .\New-RegionsXml.ps1 -RootName "My App Scenarios"
.EXAMPLE
PS> $measures = @(@{Name = "Widget Load"; Start = "WidgetLoad-Start"; Stop = "WidgetLoad-End"}, @{Name = "Flyout Animation"; Start = "animation.flyout.begin"; Stop = "animation.flyout.end"})
PS> $providers = @(@{Name = "MyApp"; Provider = [Guid]"488d209a-d0fe-433d-8156-d212766fd68e"; Id = 123; Version = 0; FieldName = "MyEventFieldName"})
PS> .\New-RegionsXml.ps1 -RootName "My App Scenarios" -Measures $measures -Path .\path\to\files
#>
param
(
[Parameter(Mandatory = $true)]
[string]
$RootName,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Hashtable[]]
$Measures,
[Parameter(Mandatory = $false)]
[string]
$Path = $pwd,
[Parameter(Mandatory = $false)]
[Hashtable[]]
$EventProviders = @(
@{
Name = "Edge";
Provider = [Guid]"9e3b3947-ca5d-4614-91a2-7b624e0e7244";
Id = 211;
Version = 0;
FieldName = "Name"
},
@{
Name = "Chrome";
Provider = [Guid]"d2d578d9-2936-45b6-a09f-30e32715f42d";
Id = 1;
Version = 0;
FieldName = "Name"
}
)
)
Begin
{
<#
.SYNOPSIS
WPA requires all GUIDs to be wrapped in curly-braces, so centralize creating and formatting GUIDs.
#>
function Format-Guid
{
param
(
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[Guid]
$Guid = [Guid]::NewGuid()
)
Set-StrictMode -Version 2
$ErrorActionPreference = "Stop"
return "{" + $Guid + "}"
}
<#
.SYNOPSIS
Pretty-print XML.
#>
function Write-Xml
{
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Xml.XmlDocument]
$Document,
[Parameter(Mandatory = $true)]
[string]
$Path
)
Set-StrictMode -Version 2
$ErrorActionPreference = "Stop"
$settings = New-Object Xml.XmlWriterSettings
$settings.OmitXmlDeclaration = $false
$settings.Indent = $true
$settings.NewLineOnAttributes = $false
$writer = $null
try
{
$writer = [Xml.XmlWriter]::Create($Path, $settings)
$Document.Save($writer)
}
finally
{
if ($writer -ne $null)
{
$writer.Dispose()
}
}
}
<#
.SYNOPSIS
Instead of using the verbose CreateElement() and SetAttribute() APIs, allow creating XML snippets in PowerShell, then importing them into a document.
#>
function Append-Element
{
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Xml.XmlDocument]
$Document,
[Parameter(Mandatory = $true)]
[string]
$XPath,
[Parameter(Mandatory = $true)]
[Xml.XmlElement]
$Child
)
Set-StrictMode -Version 2
$ErrorActionPreference = "Stop"
$Document.DocumentElement.SelectSingleNode($XPath).AppendChild($Document.ImportNode($Child, $true))
}
Set-StrictMode -Version 2
$ErrorActionPreference = "Stop"
# Generate a root Regions document
$template = [xml]@"
<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<InstrumentationManifest>
<Instrumentation>
<Regions>
<RegionRoot Guid=`"$(Format-Guid)`" Name=`"$RootName`" />
</Regions>
</Instrumentation>
</InstrumentationManifest>
"@
$clonedMeasures = @()
}
Process
{
# Generate the "Event" GUID
foreach ($measure in $Measures)
{
$clonedMeasure = $measure.Clone()
$clonedMeasure.Guid = (Format-Guid)
$clonedMeasures += $clonedMeasure
}
}
End
{
foreach ($provider in $EventProviders)
{
# Create a Regions file per provider because we want regions with the same name to have the same GUID, but WPA does not
# allow two regions with the same GUID to be in the same file
$doc = $template.Clone()
foreach ($measure in $clonedMeasures)
{
$region = [xml]@"
<Region Guid=`"$($measure.Guid)`" Name=`"$($measure.Name)`">
<Match>
<Event TID=`"true`" PID=`"true`" />
</Match>
<Start>
<Event Provider=`"$(Format-Guid -Guid $provider.Provider)`" Id=`"$($provider.Id)`" Version=`"$($provider.Version)`" />
<PayloadIdentifier FieldName=`"$($provider.FieldName)`" FieldValue=`"$($measure.Start)`" />
</Start>
<Stop>
<Event Provider=`"$(Format-Guid -Guid $provider.Provider)`" Id=`"$($provider.Id)`" Version=`"$($provider.Version)`" />
<PayloadIdentifier FieldName=`"$($provider.FieldName)`" FieldValue=`"$($measure.Stop)`" />
</Stop>
</Region>
"@
$doc | Append-Element -XPath "//RegionRoot" -Child $region.Region
}
$doc | Write-Xml -Path (Join-Path -Path $Path -ChildPath "$RootName.$($provider.Name).xml")
}
}