forked from microsoft/Power-Fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteBenchmark.ps1
316 lines (262 loc) · 11.9 KB
/
WriteBenchmark.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
param(
[Parameter (Mandatory = $true)] [String]$ConnectionString
)
## $env:BUILD_SOURCESDIRECTORY = "C:\Data\2\Power-Fx"
## $env:BUILD_DEFINITIONNAME = "Test"
## $env:BUILD_BUILDID = "69992023"
## $env:BUILD_BUILDNUMBER = "2023.X1"
## $env:BUILDCONFIGURATION = "TestRelease"
function ConvertToMs([string]$str)
{
try
{
if (($str.Trim() -eq '-') -or [string]::IsNullOrEmpty($str.Trim()))
{
$val = [double]0
}
else
{
$parts = $str.Split(@(' '), [System.StringSplitOptions]::RemoveEmptyEntries)
$val = [double]$parts[0]
## Convert to milliseconds
if ($parts[1] -eq "s") { $val *= 1000 }
elseif ($parts[1] -eq "ms") { } ## Do nothing
elseif ($parts[1] -eq "μs") { $val /= 1000 }
elseif ($parts[1] -eq "ns") { $val /= 1000000 }
else { throw ("Unknown unit: " + $parts[1]) }
}
$val
}
catch
{
Write-Error $_
}
}
function ConvertToBytes([string]$str)
{
try
{
$parts = $str.Split(@(' '), [System.StringSplitOptions]::RemoveEmptyEntries)
$val = [double]$parts[0]
## Convert to milliseconds
if ($parts[1] -eq "B") { } ## Do nothing
elseif ($parts[1] -eq "kB") { $val *= 1024 }
elseif ($parts[1] -eq "MB") { $val *= 1048576 }
elseif ($parts[1] -eq "GB") { $val *= 1073741824 }
else { throw ("Unknown unit: " + $parts[1]) }
$val
}
catch
{
Write-Error $_
}
}
## Display PowerShell version
$PSVersionTable
## Retrieve Power Fx latest Git hash and Git remote branch
cd ($env:BUILD_SOURCESDIRECTORY)
$pfxHash = (git log -n 1 --pretty=%H).ToString()
$pfxBranch = (git log -n 1 --pretty=%D).Split(", /")
$pfxBranch = $pfxBranch[$pfxBranch.Length-1]
Write-Host "------ Git context ------"
Write-Host "PFX Hash : $pfxHash"
Write-Host "PFX Branch: $pfxBranch"
Write-Host
cd src
if (-not (Test-Path "BenchmarkDotNet.Artifacts"))
{
Write-Host -ForegroundColor Yellow "No BenchmarkDotNet.Artifacts folder!"
Write-Host -ForegroundColor DarkYellow "Run benchmark first."
dir
exit
}
Write-Host "------ Test context ------"
$cpuFile = "BenchmarkDotNet.Artifacts\cpu.csv"
$memoryFile = "BenchmarkDotNet.Artifacts\memory.csv"
$referenceFile = "BenchmarkDotNet.Artifacts\results\Microsoft.PowerFx.Performance.Tests.Reference-report-github.md"
## Read CPU file, fixed size columns
$index = 0; $x = 0
foreach ($line in (Get-Content $cpuFile))
{
## Get headers and identify their positon
if ($index -eq 0)
{
$headers = $line.Split(@(' '), [System.StringSplitOptions]::RemoveEmptyEntries)
$hdrs = [System.Collections.ArrayList]::new()
$l = $line
for ($i = 0; $i -lt $headers.Length; $i++)
{
$y = $line.IndexOf($headers[$i])
$x += $y
[void]$hdrs.Add($x)
$line = $line.Substring($headers[$i].Length + $y )
$x += $headers[$i].Length
}
[void]$hdrs.Add($x)
}
if ($index -eq 1)
{
$cpuModel = $line.Substring($hdrs[0], $hdrs[1] - $hdrs[0]).Trim()
$cpuSpeed = [int]::Parse($line.Substring($hdrs[1], $hdrs[2] - $hdrs[1]).Trim())
$cpuName = $line.Substring($hdrs[2], $hdrs[3] - $hdrs[2]).Trim()
$numberCores = [int]::Parse($line.Substring($hdrs[3], $hdrs[4] - $hdrs[3]).Trim())
$numberLogicalProcs = [int]::Parse($line.Substring($hdrs[4], $hdrs[5] - $hdrs[4]).Trim())
}
$index++
}
Write-Host "CPU Model : $cpuModel"
Write-Host "CPU Speed (MHz) : $cpuSpeed"
Write-Host "CPU Name : $cpuName"
Write-Host "Number Cores : $numberCores"
Write-Host "Logical processors: $numberLogicalProcs"
## Read CPU file, fixed size columns
$index = 0; $x = 0; $memory = [double]0;
foreach ($line in (Get-Content $memoryFile))
{
## Get headers and identify their positon
if ($index -eq 0)
{
$headers = $line.Split(@(' '), [System.StringSplitOptions]::RemoveEmptyEntries)
$hdrs = [System.Collections.ArrayList]::new()
$l = $line
for ($i = 0; $i -lt $headers.Length; $i++)
{
$y = $line.IndexOf($headers[$i])
$x += $y
[void]$hdrs.Add($x)
$line = $line.Substring($headers[$i].Length + $y )
$x += $headers[$i].Length
}
[void]$hdrs.Add($x)
}
if ($index -eq 1)
{
$memory = [double]::Parse($line.Substring($hdrs[0], $hdrs[1] - $hdrs[0]).Trim());
}
$index++
}
$memoryGB = $memory / [double]1024 / [double]1024 / [double]1024;
Write-Host "Memory (GB) : $memoryGB"
$index = 0
foreach ($line in (Get-Content $referenceFile))
{
if ($index -in @(2, 4))
{
foreach ($a in ($line.Split(@(','), [System.StringSplitOptions]::RemoveEmptyEntries) | % { $_.Trim() }))
{
$b = $a.Split(@('='), [System.StringSplitOptions]::RemoveEmptyEntries) | % { $_.Trim() }
if ($b[0] -eq 'BenchmarkDotNet') { $bmdnVersion = $b[1] }
if ($b[0] -eq 'OS') { $osVersion = $b[1] }
if ($b[0] -eq 'VM') { $vmType = $b[1] }
if ($b[0] -in @('.Net Core SDK', '.NET SDK')) { $dnRTVersion = $b[1] }
}
}
if ($index -eq 6)
{
$a = $line.Split(@(':'), [System.StringSplitOptions]::RemoveEmptyEntries) | % { $_.Trim() }
$dnVersion = $a[1]
}
$index++
}
Write-Host "BenchmarkDotNet : $bmdnVersion"
Write-Host "OS Version : $osVersion"
Write-Host "VM Type : $vmType"
Write-Host ".Net RT Version : $dnRTVersion"
Write-Host ".Net Version : $dnVersion"
Write-Host
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = $ConnectionString
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$insertQuery = "insert into Runs ([TimeStamp], [Hash], [Pipeline], [BuildId], [BuildNumber], [BuildConfiguration], [Branch]) "
$insertQuery += "values (getutcdate(), '$pfxHash', '$env:BUILD_DEFINITIONNAME', '$env:BUILD_BUILDID', '$env:BUILD_BUILDNUMBER', '$env:BUILDCONFIGURATION', '$pfxBranch');"
$insertQuery += "select scope_identity() as 'Id'"
$Command.CommandText = $insertquery
$runId = $Command.ExecuteScalar()
Write-Host "RunId: $runId"
$insertQuery = "insert into Contexts ([RunId], [CPUModel], [CPUSpeedMHz], [CPUName], [NumberCores], [LogicalProcessors], [MemoryGB], [BenchMarkDotNetVersion], [OS], [VM], [DotNetRuntime], [DotNetVersion]) "
$insertQuery += "values ('$runId', '$cpuModel', '$cpuSpeed', '$cpuName', '$numberCores', '$numberLogicalProcs', '$memoryGB', '$bmdnVersion', '$osVersion', '$vmType', '$dnRTVersion', '$dnVersion');"
$insertQuery += "select scope_identity() as 'Id'"
$Command.CommandText = $insertquery
$contextId = $Command.ExecuteScalar()
Write-Host "ContextId: $contextId"
Write-Host
## Always start the list of results with the Reference CSV file
$list = (Get-Item -Filter *.csv -Path '.\BenchmarkDotNet.Artifacts\results\*' | % { $_.FullName })
foreach ($file in [System.Linq.Enumerable]::OrderBy($list, [Func[object, string]] { param($s) if ($s -match 'Reference-report\.csv') { "" } else { $s } }))
{
$t = [System.IO.Path]::GetFileNameWithoutExtension($file).Split(@('.'))[-1]
if ($t.Length -gt 7) { $testCategory = $t.Substring(0, $t.Length - 7) } else { $testCategory = $t }
Write-Host "------ [TEST] $testCategory ------"
$table = [System.Data.DataTable]::new()
[void]$table.Columns.Add("TestName", [string]); $table.Columns["TestName"].AllowDBNull = $false
[void]$table.Columns.Add("N", [int]); $table.Columns["N"].AllowDBNull = $true ## Optional column, depends on the test
[void]$table.Columns.Add("Mean", [double]); $table.Columns["Mean"].AllowDBNull = $false
[void]$table.Columns.Add("StdDev", [double]); $table.Columns["StdDev"].AllowDBNull = $false
[void]$table.Columns.Add("Min", [double]); $table.Columns["Min"].AllowDBNull = $false
[void]$table.Columns.Add("Q1", [double]); $table.Columns["Q1"].AllowDBNull = $false
[void]$table.Columns.Add("Median", [double]); $table.Columns["Median"].AllowDBNull = $false
[void]$table.Columns.Add("Q3", [double]); $table.Columns["Q3"].AllowDBNull = $false
[void]$table.Columns.Add("Max", [double]); $table.Columns["Max"].AllowDBNull = $false
[void]$table.Columns.Add("Gen0", [double]); $table.Columns["Gen0"].AllowDBNull = $false
[void]$table.Columns.Add("Gen1", [double]); $table.Columns["Gen1"].AllowDBNull = $false
[void]$table.Columns.Add("Allocated", [double]); $table.Columns["Allocated"].AllowDBNull = $false
[void]$table.Columns.Add("AllocatedNativeMemory", [double]); $table.Columns["AllocatedNativeMemory"].AllowDBNull = $false
[void]$table.Columns.Add("NativeMemoryLeak", [double]); $table.Columns["NativeMemoryLeak"].AllowDBNull = $false
foreach ($row in (Import-Csv -Encoding UTF8 $file | Select-Object Method, Runtime, N, Mean, StdDev, Min, Q1, Median, Q3, Max, Gen0, Gen1, Allocated, 'Allocated Native Memory', 'Native Memory Leak'))
{
$mean = ConvertToMs($row.Mean)
$stddev = ConvertToMs($row.StdDev)
$min = ConvertToMs($row.Min)
$q1 = ConvertToMs($row.Q1)
$median = ConvertToMs($row.Median)
$q3 = ConvertToMs($row.Q3)
$max = ConvertToMs($row.Max)
$gen0 = $row.Gen0 ## Gen X means number of GC collections per 1000 operations for that generation
$gen1 = $row.Gen1
$alloc = ConvertToBytes($row.Allocated)
$native = ConvertToBytes($row.'Allocated Native Memory')
$leak = ConvertToBytes($row.'Native Memory Leak')
if (($gen0 -eq $null) -or ($gen0.Trim() -eq '-') -or [string]::IsNullOrEmpty($gen0.Trim())) { $gen0 = [double]0 }
if (($gen1 -eq $null) -or ($gen1.Trim() -eq '-') -or [string]::IsNullOrEmpty($gen1.Trim())) { $gen1 = [double]0 }
[void]$table.Rows.Add($row.Method, $row.N, $mean, $stddev, $min, $q1, $median, $q3, $max, $gen0, $gen1, $alloc, $native, $leak)
}
$table | Sort-Object TestName, N | ft TestName, N, Mean, StdDev, Min, Q1, Median, Q3, Max, Gen0, Gen1, Allocated, AllocatedNativeMemory, NativeMemoryLeak
$ctxids = [System.Collections.ArrayList]::new()
foreach ($row in $table)
{
$testName = $row.TestName
$n = $row.N
$mean = $row.Mean
$stdDev = $row.StdDev
$min = $row.Min
$q1 = $row.Q1
$median = $row.Median
$q3 = $row.Q3
$max = $row.Max
$gen0 = $row.Gen0
$gen1 = $row.Gen1
$alloc = $row.Allocated
$native = $row.AllocatedNativeMemory
$leak = $row.NativeMemoryLeak
$insertQuery = "insert into Tests ([RunId], [ContextId], [TestName], [N], [MeanMs], [StdDevMs], [MinMs], [Q1Ms], [MedianMs], [Q3Ms], [MaxMs], [Gen0], [Gen1], [Allocated], [AllocatedNativeMemory], [NativeMemoryLeak]) "
$insertQuery += "values ('$runId', '$contextId', '$testName', "
if ($n.GetType() -eq [DBNull])
{
$insertQuery += "null"
}
else
{
$insertQuery += "'$n'"
}
$insertQuery += ", '$mean', '$stdDev', '$min', '$q1', '$median', '$q3', '$max', '$gen0', '$gen1', '$alloc', '$native', '$leak'); "
$insertQuery += "select scope_identity() as 'Id'"
$Command.CommandText = $insertquery
$id = $Command.ExecuteScalar()
[void]$ctxids.Add($id)
}
Write-Host "TestIds: " ([String]::Join(', ', $ctxids.ToArray()))
Write-Host
}
Write-Host "--- End of script ---"