-
Notifications
You must be signed in to change notification settings - Fork 893
Expand file tree
/
Copy pathbenchmark.ps1
More file actions
executable file
·291 lines (221 loc) · 8.35 KB
/
Copy pathbenchmark.ps1
File metadata and controls
executable file
·291 lines (221 loc) · 8.35 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#! /usr/bin/env pwsh
#Requires -PSEdition Core
#Requires -Version 7
<#
.SYNOPSIS
Runs BenchmarkDotNet benchmarks for a target git ref and an optional baseline ref.
.DESCRIPTION
Checks out each requested ref, runs the Benchmarks test project with the supplied
BenchmarkDotNet filters and options, and writes artifacts into ref-specific
subdirectories under BenchmarkDotNet.Artifacts in the root of the current repository.
This script requires a clean working tree because it switches refs while running.
When -Target is omitted, the current branch is used. In a detached HEAD state,
-Target must be explicitly provided.
.PARAMETER Benchmarks
One or more BenchmarkDotNet filter expressions to pass through using --filter.
.PARAMETER Target
The target branch, tag, or commit to benchmark. Defaults to the current branch.
.PARAMETER Baseline
The baseline branch, tag, or commit to benchmark for comparison. Defaults to "main".
.PARAMETER Job
The BenchmarkDotNet job to use (e.g. "Short"). Defaults to "Default".
.PARAMETER Runtimes
One or more target frameworks to benchmark. Defaults to "net10.0".
.PARAMETER EnableMemoryDiagnoser
Enables the BenchmarkDotNet memory diagnoser.
.PARAMETER EnableEventPipeProfiler
Enables the BenchmarkDotNet EventPipe profiler.
.PARAMETER SkipBaseline
Runs only the target benchmark and skips the baseline ref.
.EXAMPLE
./benchmark.ps1 @("*SamplerBenchmarks*") -SkipBaseline
Runs the matching benchmarks for the current branch only.
.EXAMPLE
./benchmark.ps1 @("*ExporterBenchmarks*") -Target my-feature -Job Short -Runtimes @("net10.0", "net462")
Runs the matching exporter benchmarks for the my-feature branch and main using the
"Short" job for .NET 10 and .NET Framework 4.6.2.
#>
param(
[Parameter(Mandatory = $true, Position = 0)][string[]] $Benchmarks,
[Parameter(Mandatory = $false)][string] $Target,
[Parameter(Mandatory = $false)][string] $Baseline = "main",
[Parameter(Mandatory = $false)][string] $Job = "Default",
[Parameter(Mandatory = $false)][string[]] $Runtimes = @("net10.0"),
[Parameter(Mandatory = $false)][switch] $EnableMemoryDiagnoser,
[Parameter(Mandatory = $false)][switch] $EnableEventPipeProfiler,
[Parameter(Mandatory = $false)][switch] $SkipBaseline
)
$ErrorActionPreference = "Stop"
$InformationPreference = "Continue"
$ProgressPreference = "SilentlyContinue"
function Invoke-Git {
param(
[Parameter(Mandatory = $true)][string[]] $Arguments
)
& git @Arguments
if ($LASTEXITCODE -ne 0) {
throw "git $($Arguments -join ' ') failed with exit code $LASTEXITCODE."
}
}
function Get-GitOutput {
param(
[Parameter(Mandatory = $true)][string[]] $Arguments
)
$output = & git @Arguments
if ($LASTEXITCODE -ne 0) {
throw "git $($Arguments -join ' ') failed with exit code $LASTEXITCODE."
}
return ($output | Out-String).Trim()
}
function Resolve-GitCommit {
param(
[Parameter(Mandatory = $true)][string] $RefName
)
return Get-GitOutput -Arguments @("rev-parse", "--verify", "--end-of-options", "$RefName`^{commit}")
}
function ConvertTo-SafePathSegment {
param(
[Parameter(Mandatory = $true)][string] $Value
)
$invalidChars = [System.Collections.Generic.HashSet[char]]::new([System.IO.Path]::GetInvalidFileNameChars())
$invalidChars.Add([System.IO.Path]::DirectorySeparatorChar) | Out-Null
$invalidChars.Add([System.IO.Path]::AltDirectorySeparatorChar) | Out-Null
$buffer = $Value.ToCharArray()
for ($i = 0; $i -lt $buffer.Length; $i++) {
if ($invalidChars.Contains($buffer[$i])) {
$buffer[$i] = '_'
}
}
$safeValue = [string]::new($buffer)
$safeValue = $safeValue.TrimEnd('.')
if ([string]::IsNullOrEmpty($safeValue)) {
return "_"
}
return $safeValue
}
$Configuration = "Release"
$Framework = "net10.0"
if (-not ($Runtimes | Where-Object { $_ -notmatch "^net4\d+$" })) {
$Framework = "net462"
}
if ($Benchmarks.Count -eq 0) {
throw "At least one benchmark filter must be specified."
}
if ($Runtimes.Count -eq 0) {
throw "At least one .NET runtime must be specified."
}
$unsupportedRuntimes = $Runtimes | Where-Object { $_ -match "^net4\d+$" }
if (-not $IsWindows -and $unsupportedRuntimes.Count -gt 0) {
throw ".NET Framework runtimes ($($unsupportedRuntimes -join ", ")) are only supported on Windows."
}
$workingTreeStatus = Get-GitOutput -Arguments @("status", "--porcelain")
if (-not [string]::IsNullOrWhiteSpace($workingTreeStatus)) {
throw "The git working tree must be clean before running benchmarks."
}
$startingBranch = Get-GitOutput -Arguments @("branch", "--show-current")
$startingCommit = Resolve-GitCommit -RefName "HEAD"
$targetName = $Target
$baselineName = $Baseline
if ($env:GITHUB_ACTIONS -eq "true") {
if ([string]::IsNullOrEmpty($Target)) {
$Target = $env:GITHUB_SHA
if (-not [string]::IsNullOrEmpty($env:GITHUB_HEAD_REF)) {
$targetName = $env:GITHUB_HEAD_REF
}
elseif (-not [string]::IsNullOrEmpty($env:GITHUB_REF_NAME)) {
$targetName = $env:GITHUB_REF_NAME
}
}
if (-not $PSBoundParameters.ContainsKey("Baseline")) {
if (-not [string]::IsNullOrEmpty($env:GITHUB_BASE_REF)) {
$Baseline = "origin/$($env:GITHUB_BASE_REF)"
$baselineName = $env:GITHUB_BASE_REF
}
elseif ($env:GITHUB_REF_TYPE -eq "branch" -and $env:GITHUB_REF_NAME -like "main*") {
$Baseline = "$($env:GITHUB_SHA)~1"
$baselineName = "$($env:GITHUB_REF_NAME)~1"
}
}
}
if ([string]::IsNullOrEmpty($Target)) {
if ([string]::IsNullOrEmpty($startingBranch)) {
throw "Target must be specified when the repository is in a detached HEAD state."
}
$Target = $startingBranch
}
if ([string]::IsNullOrEmpty($targetName)) {
$targetName = $Target
}
if ([string]::IsNullOrEmpty($baselineName)) {
$baselineName = $Baseline
}
if (-not $SkipBaseline -and $Target -eq $Baseline) {
throw "Target branch '$Target' cannot be the same as baseline branch '$Baseline'."
}
$solutionPath = $PSScriptRoot
$project = Join-Path $solutionPath "test" "Benchmarks" "Benchmarks.csproj"
$artifacts = Join-Path $solutionPath "BenchmarkDotNet.Artifacts"
$targetBenchmark = [PSCustomObject]@{
Name = $targetName
Commit = Resolve-GitCommit -RefName $Target
}
if ($SkipBaseline) {
$benchmarkRefs = @($targetBenchmark)
}
else {
$benchmarkRefs = @(
$targetBenchmark,
[PSCustomObject]@{
Name = $baselineName
Commit = Resolve-GitCommit -RefName $Baseline
}
)
}
try {
foreach ($benchmarkRef in $benchmarkRefs) {
$branch = $benchmarkRef.Name
Write-Information "Checking out ref '$branch'..."
Invoke-Git -Arguments @("switch", "--detach", "--", $benchmarkRef.Commit)
Write-Information "Running benchmarks for ref '$branch'..."
$additionalArgs = @()
$additionalArgs += "--artifacts"
$additionalArgs += (Join-Path $artifacts (ConvertTo-SafePathSegment -Value $branch))
$additionalArgs += "--runtimes"
$additionalArgs += $Runtimes
$additionalArgs += "--filter"
$additionalArgs += $Benchmarks
if (-not [string]::IsNullOrEmpty($Job)) {
$additionalArgs += "--job"
$additionalArgs += $Job
}
if ($EnableMemoryDiagnoser) {
$additionalArgs += "--memory"
}
if ($EnableEventPipeProfiler) {
$additionalArgs += "--profiler"
$additionalArgs += "EP"
}
$dotnetArgs = @(
"run"
"--configuration", $Configuration
"--framework", $Framework
"--project", $project
"--"
) + $additionalArgs
$p = Start-Process -FilePath "dotnet" -ArgumentList $dotnetArgs -NoNewWindow -Wait -PassThru
if ($p.ExitCode -ne 0) {
throw "Benchmarks failed with exit code $($p.ExitCode)."
}
}
}
finally {
if (-not [string]::IsNullOrEmpty($startingBranch)) {
Write-Information "Restoring original ref '$startingBranch'..."
Invoke-Git -Arguments @("switch", "--", $startingBranch)
}
else {
Write-Information "Restoring original ref '$startingCommit'..."
Invoke-Git -Arguments @("switch", "--detach", "--", $startingCommit)
}
}
Write-Information "Finished running benchmarks."