-
Notifications
You must be signed in to change notification settings - Fork 7
/
run-it.ps1
executable file
·238 lines (205 loc) · 6.44 KB
/
run-it.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env pwsh
param(
[string] $appname,
[string] $url,
[switch] $trim,
[switch] $trimr2r,
[switch] $aggro,
[switch] $r2r,
# for the purposes of this prototype, singleFile means the
# experimental statically-linked host bundle (with custom msbuild
# logic), not the 3.0 feature that extracts files to disk (under
# the PublishSingleFile switch)
[switch] $singleFile,
[switch] $customBuilder,
[switch] $noMvc,
[switch] $time,
[switch] $trace)
dotnet clean
if (Test-Path "obj")
{
Write-Debug "Deleting obj"
Remove-Item "obj" -r -for
}
# PS-non-core doesn't define these variables - so assume windows
$rid = "win10-x64"
if ($IsMacOS)
{
$rid = "osx-x64"
}
elseif ($IsLinux)
{
$rid = "linux-x64"
}
if ($trace)
{
if (-not $IsLinux)
{
Write-Error "tracing is only supported on linux"
exit
}
$trace_name = "$appname.$(Get-Date -UFormat "%m-%d-%H-%M")"
if (Test-Path "$trace_name.zip")
{
Write-Error "$trace_name.zip already exists"
exit
}
}
$project = Join-Path "$PSScriptRoot" "src" | Join-Path -ChildPath "$appname" | Join-Path -ChildPath "$appname.csproj"
if ($IsLinux -or $IsMacOS)
{
$app_intermediates_dir = Join-Path "$PSScriptRoot" "intermediates" | Join-Path -ChildPath "$appname"
$publish_dir = Join-Path "$app_intermediates_dir" "bin" | Join-Path -ChildPath "Release" | Join-Path -ChildPath "netcoreapp3.0" | Join-Path -ChildPath "$rid" | Join-Path -ChildPath "publish"
}
else
{
$publish_dir = Join-Path "." "bin" | Join-Path -ChildPath "Release" | Join-Path -ChildPath "netcoreapp3.0" | Join-Path -ChildPath "$rid" | Join-Path -ChildPath "publish"
}
if (Test-Path $publish_dir)
{
Write-Debug "Deleting $publish_dir"
Remove-Item $publish_dir -r -for
}
$defines = ""
if ($time)
{
$defines += "TIME;"
}
if ($customBuilder)
{
$defines += "CUSTOM_BUILDER;"
}
if ($noMvc)
{
$defines += "NO_MVC";
}
# Use a stopwatch for this measurement so that we get console output
# (unlike Measure-Command). We don't need to be too precise for the publish time.
$stopWatch = [Diagnostics.StopWatch]::StartNew()
# Do not try to simplify the $defines part of this. Please.
& dotnet publish -c Release -r $rid /bl `
/p:PublishTrimmed=$trim `
/p:LinkAggressively=$aggro `
/p:LinkAwayReadyToRun=$trimr2r `
/p:PublishReadyToRun=$r2r `
/p:UseStaticHost=$singleFile `
"/p:DefineConstants=\`"$defines\`"" `
"$project"
$stopWatch.Stop();
Write-Host ("Size is {0:N2} MB" -f ((Get-ChildItem "$publish_dir" -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB))
Write-Host ("Publish took {0:N2} s" -f ($stopWatch.Elapsed.TotalSeconds))
$app_path = Join-Path "$publish_dir" "$appname"
if ($IsLinux -or $IsMacOS)
{
$app_path = "$app_path"
}
else
{
$app_path = "$app_path" + ".exe"
}
if (-not (Test-Path "$app_path"))
{
Write-Error "app not found at $app_path"
exit
}
$console = $url -eq $null -or $url -eq ""
$iterations = 10
if ($trace)
{
# 1. get perfcollect
$perfcollect = Join-Path "$PSScriptRoot" "perfcollect"
if (-not (Test-Path "$perfcollect"))
{
& wget "http://aka.ms/perfcollect" -O "$perfcollect"
& chmod +x "$perfcollect"
}
# 2. copy crossgen to the publish directory (used to resolve R2R symbols)
if ($singleFile)
{
# there's no deps.json alongside the single executable - look in the intermediates instead
$deps_json = Join-Path "$app_intermediates_dir" "multifile-publish" "$appname.deps.json"
}
else
{
$deps_json = Join-Path "$publish_dir" "$appname.deps.json"
}
if (-not (Test-Path $deps_json))
{
Write-Error "deps.json not found at $deps_json"
exit
}
$runtimeversion = Select-String -Path "$deps_json" -Pattern "runtimepack.Microsoft.NETCore.App.Runtime.$rid" `
| Select-Object -First 1 | %{ $_ -split ':' } | Select-Object -Last 1 | %{ $_ -split '"' } | Select-Object -Index 1
if (-not ($runtimeversion -and ($runtimeversion.StartsWith("3.0.0"))))
{
Write-Error "unable to get runtime version"
exit
}
$crossgen = Join-Path "$env:HOME" ".nuget" "packages" "microsoft.netcore.app.runtime.linux-x64" "$runtimeversion" "tools" "crossgen"
if (-not (Test-Path "$crossgen"))
{
Write-Error "crossgen does not exist at `"$crossgen`""
exit
}
Copy-Item "$crossgen" "$publish_dir"
# 3. install native symbols for the runtime libraries
& dotnet tool install -g dotnet-symbol
$env:DOTNET_ROOT = (Split-Path -Parent (Get-Command dotnet).Source)
$dotnet_symbol = Join-Path "$env:HOME" ".dotnet" "tools" "dotnet-symbol"
if (-not (Test-Path "$dotnet_symbol"))
{
Write-Error "dotnet-symbol not found at $dotnet_symbol"
exit
}
& "$dotnet_symbol" --symbols "$publish_dir/lib*.so"
# 4. run perfcollect and hold onto the perfcollect PID to wait on it later
$pid_file = & mktemp -u
& mkfifo "$pid_file"
Start-Process sudo -ArgumentList @("sh", "-c", "`"echo `$`$ > `"$pid_file`"; exec $perfcollect collect $trace_name`"")
$perfcollect_pid = & cat "$pid_file"
Write-Host "perfcollect PID: $perfcollect_pid"
# 5. set COMPlus variables for tracing
$env:COMPlus_PerfMapEnabled = 1
$env:COMPlus_EnableEventLog = 1
}
if ($time)
{
$sum = 0
for ($i = 0; $i -lt ($iterations + 1); $i++)
{
Write-Debug "Starting $app"
$result = Measure-Command {
$proc = Start-Process -FilePath $app_path -ArgumentList @("--time") -PassThru -NoNewWindow
$proc.WaitForExit()
}
# Ignore first result
if ($i -gt 0) {
$sum += $result.TotalMilliseconds
Write-Host $result.TotalMilliseconds
}
}
$avg = $sum / $iterations
Write-Host "Average startup time (ms): $avg"
if (-not $console) {
# Measure working set after startup and one request
Write-Debug "Starting $app"
$proc = Start-Process -FilePath $app_path -PassThru -NoNewWindow -RedirectStandardOutput '.\NUL'
Start-Sleep -Seconds 1
Write-Debug "Making a request to $url"
Invoke-WebRequest $url | Out-Null
$proc.Refresh()
$mb = [int]$proc.WorkingSet / 1MB
Write-Host "Working set: $mb"
Write-Debug "Stopping"
$proc.Kill();
}
}
else
{
& $app_path
}
if ($trace)
{
& sudo kill ((Get-Process -Name 'perf').Id)
Wait-Process -Id "$perfcollect_pid"
}