-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathTestResultAnalyzer.ps1
324 lines (304 loc) · 17.7 KB
/
TestResultAnalyzer.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
317
318
319
320
321
322
323
324
$statusOK = " :heavy_check_mark:"
$statusWarning = " :warning:"
$statusError = " :x:"
$statusSkipped = " :question:"
# Build MarkDown of TestResults file
# This function will not fail if the file does not exist or if any test errors are found
# TestResults is in JUnit format
# Returns both a summary part and a failures part
function GetTestResultSummaryMD {
Param(
[string] $testResultsFile
)
$summarySb = [System.Text.StringBuilder]::new()
$failuresSb = [System.Text.StringBuilder]::new()
if (Test-Path -Path $testResultsFile -PathType Leaf) {
$testResults = [xml](Get-Content -path $testResultsFile -Encoding UTF8)
$totalTests = 0
$totalTime = 0.0
$totalFailed = 0
$totalSkipped = 0
if ($testResults.testsuites) {
$appNames = @($testResults.testsuites.testsuite | ForEach-Object { $_.Properties.property | Where-Object { $_.Name -eq "appName" } | ForEach-Object { $_.Value } } | Select-Object -Unique)
if (-not $appNames) {
$appNames = @($testResults.testsuites.testsuite | ForEach-Object { $_.Properties.property | Where-Object { $_.Name -eq "extensionId" } | ForEach-Object { $_.Value } } | Select-Object -Unique)
}
foreach($testsuite in $testResults.testsuites.testsuite) {
$totalTests += $testsuite.Tests
$totalTime += [decimal]::Parse($testsuite.time, [System.Globalization.CultureInfo]::InvariantCulture)
$totalFailed += $testsuite.failures
$totalSkipped += $testsuite.skipped
}
Write-Host "$($appNames.Count) TestApps, $totalTests tests, $totalFailed failed, $totalSkipped skipped, $totalTime seconds"
$summarySb.Append('|Test app|Tests|Passed|Failed|Skipped|Time|\n|:---|---:|---:|---:|---:|---:|\n') | Out-Null
foreach($appName in $appNames) {
$appTests = 0
$appTime = 0.0
$appFailed = 0
$appSkipped = 0
$suites = $testResults.testsuites.testsuite | where-Object { $_.Properties.property | Where-Object { ($_.Name -eq 'appName' -or $_.Name -eq 'extensionId') -and $_.Value -eq $appName } }
foreach($suite in $suites) {
$appTests += [int]$suite.tests
$appFailed += [int]$suite.failures
$appSkipped += [int]$suite.skipped
$appTime += [decimal]::Parse($suite.time, [System.Globalization.CultureInfo]::InvariantCulture)
}
$appPassed = $appTests-$appFailed-$appSkipped
Write-Host "- $appName, $appTests tests, $appPassed passed, $appFailed failed, $appSkipped skipped, $appTime seconds"
$summarySb.Append("|$appName|$appTests|") | Out-Null
if ($appPassed -gt 0) {
$summarySb.Append("$($appPassed)$statusOK") | Out-Null
}
$summarySb.Append("|") | Out-Null
if ($appFailed -gt 0) {
$summarySb.Append("$($appFailed)$statusError") | Out-Null
}
$summarySb.Append("|") | Out-Null
if ($appSkipped -gt 0) {
$summarySb.Append("$($appSkipped)$statusSkipped") | Out-Null
}
$summarySb.Append("|$($appTime)s|\n") | Out-Null
if ($appFailed -gt 0) {
$failuresSb.Append("<details><summary><i>$appName, $appTests tests, $appPassed passed, $appFailed failed, $appSkipped skipped, $appTime seconds</i></summary>\n") | Out-Null
foreach($suite in $suites) {
Write-Host " - $($suite.name), $($suite.tests) tests, $($suite.failures) failed, $($suite.skipped) skipped, $($suite.time) seconds"
if ($suite.failures -gt 0 -and $failuresSb.Length -lt 32000) {
$failuresSb.Append("<details><summary><i>$($suite.name), $($suite.tests) tests, $($suite.failures) failed, $($suite.skipped) skipped, $($suite.time) seconds</i></summary>") | Out-Null
foreach($testcase in $suite.testcase) {
if ($testcase.ChildNodes.Count -gt 0) {
Write-Host " - $($testcase.name), Failure, $($testcase.time) seconds"
$failuresSb.Append("<details><summary><i>$($testcase.name), Failure</i></summary>") | Out-Null
foreach($failure in $testcase.ChildNodes) {
Write-Host " - Error: $($failure.message)"
Write-Host " Stacktrace:"
Write-Host " $($failure."#text".Trim().Replace("`n","`n "))"
$failuresSb.Append("<i> Error: $($failure.message)</i><br/>") | Out-Null
$failuresSb.Append("<i> Stack trace</i><br/>") | Out-Null
$failuresSb.Append("<i> $($failure."#text".Trim().Replace("`n","<br/> "))</i><br/>") | Out-Null
}
$failuresSb.Append("</details>") | Out-Null
}
}
$failuresSb.Append("</details>") | Out-Null
}
}
$failuresSb.Append("</details>") | Out-Null
}
}
}
if ($totalFailed -gt 0) {
$failuresSummaryMD = "<i>$totalFailed failing tests, download test results to see details</i>"
$failuresSb.Insert(0,"<details><summary>$failuresSummaryMD</summary>") | Out-Null
$failuresSb.Append("</details>") | Out-Null
}
else {
$failuresSummaryMD = "<i>No test failures</i>"
$failuresSb.Append($failuresSummaryMD) | Out-Null
}
}
else {
$summarySb.Append("<i>No test results found</i>") | Out-Null
$failuresSummaryMD = ''
}
$summarySb.ToString()
$failuresSb.ToString()
$failuresSummaryMD
}
function ReadBcptFile {
Param(
[string] $bcptTestResultsFile
)
if ((-not $bcptTestResultsFile) -or (-not (Test-Path -Path $bcptTestResultsFile -PathType Leaf))) {
return $null
}
# Read BCPT file
$bcptResult = Get-Content -Path $bcptTestResultsFile -Encoding UTF8 | ConvertFrom-Json
$suites = [ordered]@{}
# Sort by bcptCode, codeunitID, operation
foreach($measure in $bcptResult) {
$bcptCode = $measure.bcptCode
$codeunitID = $measure.codeunitID
$codeunitName = $measure.codeunitName
$operation = $measure.operation
# Create Suite if it doesn't exist
if(-not $suites.Contains($bcptCode)) {
$suites."$bcptCode" = [ordered]@{}
}
# Create Codeunit under Suite if it doesn't exist
if (-not $suites."$bcptCode".Contains("$codeunitID")) {
$suites."$bcptCode"."$codeunitID" = @{
"codeunitName" = $codeunitName
"operations" = [ordered]@{}
}
}
# Create Operation under Codeunit if it doesn't exist
if (-not $suites."$bcptCode"."$codeunitID"."operations".Contains($operation)) {
$suites."$bcptCode"."$codeunitID"."operations"."$operation" = @{
"measurements" = @()
}
}
# Add measurement to measurements under operation
$suites."$bcptCode"."$codeunitID"."operations"."$operation".measurements += @(@{
"durationMin" = $measure.durationMin
"numberOfSQLStmts" = $measure.numberOfSQLStmts
})
}
$suites
}
function GetBcptSummaryMD {
Param(
[string] $bcptTestResultsFile,
[string] $baseLinePath = '',
[string] $thresholdsPath = '',
[int] $skipMeasurements = 0,
[hashtable] $bcptThresholds = $null
)
$bcpt = ReadBcptFile -bcptTestResultsFile $bcptTestResultsFile
if (-not $bcpt) {
return ''
}
$baseLine = ReadBcptFile -bcptTestResultsFile $baseLinePath
if ($baseLine) {
if ($null -eq $bcptThresholds) {
throw "Thresholds must be provided when comparing to a baseline"
}
# Override thresholds if thresholds file exists
if ($thresholdsPath -and (Test-Path -path $thresholdsPath)) {
Write-Host "Reading thresholds from $thresholdsPath"
$thresholds = Get-Content -Path $thresholdsPath -Encoding UTF8 | ConvertFrom-Json
foreach($threshold in 'durationWarning', 'durationError', 'numberOfSqlStmtsWarning', 'numberOfSqlStmtsError') {
if ($thresholds.PSObject.Properties.Name -eq $threshold) {
$bcptThresholds."$threshold" = $thresholds."$threshold"
}
}
}
Write-Host "Using thresholds:"
Write-Host "- DurationWarning: $($bcptThresholds.durationWarning)"
Write-Host "- DurationError: $($bcptThresholds.durationError)"
Write-Host "- NumberOfSqlStmtsWarning: $($bcptThresholds.numberOfSqlStmtsWarning)"
Write-Host "- NumberOfSqlStmtsError: $($bcptThresholds.numberOfSqlStmtsError)"
}
$summarySb = [System.Text.StringBuilder]::new()
if ($baseLine) {
$summarySb.Append("|BCPT Suite|Codeunit ID|Codeunit Name|Operation|Status|Duration (ms)|Duration base (ms)|Duration diff (ms)|Duration diff|SQL Stmts|SQL Stmts base|SQL Stmts diff|SQL Stmts diff|\n") | Out-Null
$summarySb.Append("|:---------|:----------|:------------|:--------|:----:|------------:|-----------------:|-----------------:|------------:|--------:|-------------:|-------------:|-------------:|\n") | Out-Null
}
else {
$summarySb.Append("|BCPT Suite|Codeunit ID|Codeunit Name|Operation|Duration (ms)|SQL Stmts|\n") | Out-Null
$summarySb.Append("|:---------|:----------|:------------|:--------|------------:|--------:|\n") | Out-Null
}
$lastSuiteName = ''
$lastCodeunitID = ''
$lastCodeunitName = ''
$lastOperationName = ''
# calculate statistics on measurements, skipping the $skipMeasurements longest measurements
foreach($suiteName in $bcpt.Keys) {
$suite = $bcpt."$suiteName"
foreach($codeUnitID in $suite.Keys) {
$codeunit = $suite."$codeunitID"
$codeUnitName = $codeunit.codeunitName
foreach($operationName in $codeunit."operations".Keys) {
$operation = $codeunit."operations"."$operationName"
# Get measurements to use for statistics
$measurements = @($operation."measurements" | Sort-Object -Descending { $_.durationMin } | Select-Object -Skip $skipMeasurements)
# Calculate statistics and store them in the operation
$durationMin = ($measurements | ForEach-Object { $_.durationMin } | Measure-Object -Minimum).Minimum
$numberOfSQLStmts = ($measurements | ForEach-Object { $_.numberOfSQLStmts } | Measure-Object -Minimum).Minimum
$baseLineFound = $true
try {
$baseLineMeasurements = @($baseLine."$suiteName"."$codeUnitID"."operations"."$operationName"."measurements" | Sort-Object -Descending { $_.durationMin } | Select-Object -Skip $skipMeasurements)
if ($baseLineMeasurements.Count -eq 0) {
throw "No base line measurements"
}
$baseDurationMin = ($baseLineMeasurements | ForEach-Object { $_.durationMin } | Measure-Object -Minimum).Minimum
$diffDurationMin = $durationMin-$baseDurationMin
$baseNumberOfSQLStmts = ($baseLineMeasurements | ForEach-Object { $_.numberOfSQLStmts } | Measure-Object -Minimum).Minimum
$diffNumberOfSQLStmts = $numberOfSQLStmts-$baseNumberOfSQLStmts
}
catch {
$baseLineFound = $false
$baseDurationMin = $durationMin
$diffDurationMin = 0
$baseNumberOfSQLStmts = $numberOfSQLStmts
$diffNumberOfSQLStmts = 0
}
$pctDurationMin = ($durationMin-$baseDurationMin)*100/$baseDurationMin
$durationMinStr = "$($durationMin.ToString("#"))|"
$baseDurationMinStr = "$($baseDurationMin.ToString("#"))|"
$diffDurationMinStr = "$($diffDurationMin.ToString("+#;-#;0"))|$($pctDurationMin.ToString('+#;-#;0'))%|"
$pctNumberOfSQLStmts = ($numberOfSQLStmts-$baseNumberOfSQLStmts)*100/$baseNumberOfSQLStmts
$numberOfSQLStmtsStr = "$($numberOfSQLStmts.ToString("#"))|"
$baseNumberOfSQLStmtsStr = "$($baseNumberOfSQLStmts.ToString("#"))|"
$diffNumberOfSQLStmtsStr = "$($diffNumberOfSQLStmts.ToString("+#;-#;0"))|$($pctNumberOfSQLStmts.ToString('+#;-#;0'))%|"
$thisOperationName = ''; if ($operationName -ne $lastOperationName) { $thisOperationName = $operationName }
$thisCodeunitName = ''; if ($codeunitName -ne $lastCodeunitName) { $thisCodeunitName = $codeunitName; $thisOperationName = $operationName }
$thisCodeunitID = ''; if ($codeunitID -ne $lastCodeunitID) { $thisCodeunitID = $codeunitID; $thisOperationName = $operationName }
$thisSuiteName = ''; if ($suiteName -ne $lastSuiteName) { $thisSuiteName = $suiteName; $thisOperationName = $operationName }
if (!$baseLine) {
# No baseline provided
$statusStr = ''
$baseDurationMinStr = ''
$diffDurationMinStr = ''
$baseNumberOfSQLStmtsStr = ''
$diffNumberOfSQLStmtsStr = ''
}
else {
if (!$baseLineFound) {
# Baseline provided, but not found for this operation
$statusStr = $statusSkipped
$baseDurationMinStr = 'N/A|'
$diffDurationMinStr = '||'
$baseNumberOfSQLStmtsStr = 'N/A|'
$diffNumberOfSQLStmtsStr = '||'
}
else {
$statusStr = $statusOK
if ($pctDurationMin -ge $bcptThresholds.durationError) {
$statusStr = $statusError
if ($thisCodeunitName) {
# Only give errors and warnings on top level operation
OutputError -message "$operationName in $($suiteName):$codeUnitID degrades $($pctDurationMin.ToString('N0'))%, which exceeds the error threshold of $($bcptThresholds.durationError)% for duration"
}
}
if ($pctNumberOfSQLStmts -ge $bcptThresholds.numberOfSqlStmtsError) {
$statusStr = $statusError
if ($thisCodeunitName) {
# Only give errors and warnings on top level operation
OutputError -message "$operationName in $($suiteName):$codeUnitID degrades $($pctNumberOfSQLStmts.ToString('N0'))%, which exceeds the error threshold of $($bcptThresholds.numberOfSqlStmtsError)% for number of SQL statements"
}
}
if ($statusStr -eq $statusOK) {
if ($pctDurationMin -ge $bcptThresholds.durationWarning) {
$statusStr = $statusWarning
if ($thisCodeunitName) {
# Only give errors and warnings on top level operation
OutputWarning -message "$operationName in $($suiteName):$codeUnitID degrades $($pctDurationMin.ToString('N0'))%, which exceeds the warning threshold of $($bcptThresholds.durationWarning)% for duration"
}
}
if ($pctNumberOfSQLStmts -ge $bcptThresholds.numberOfSqlStmtsWarning) {
$statusStr = $statusWarning
if ($thisCodeunitName) {
# Only give errors and warnings on top level operation
OutputWarning -message "$operationName in $($suiteName):$codeUnitID degrades $($pctNumberOfSQLStmts.ToString('N0'))%, which exceeds the warning threshold of $($bcptThresholds.numberOfSqlStmtsWarning)% for number of SQL statements"
}
}
}
}
$statusStr += '|'
}
$summarySb.Append("|$thisSuiteName|$thisCodeunitID|$thisCodeunitName|$thisOperationName|$statusStr$durationMinStr$baseDurationMinStr$diffDurationMinStr$numberOfSQLStmtsStr$baseNumberOfSQLStmtsStr$diffNumberOfSQLStmtsStr\n") | Out-Null
$lastSuiteName = $suiteName
$lastCodeunitID = $codeUnitID
$lastCodeunitName = $codeUnitName
$lastOperationName = $operationName
}
}
}
if ($baseLine) {
$summarySb.Append("\n<i>Used baseline provided in $([System.IO.Path]::GetFileName($baseLinePath)).</i>") | Out-Null
}
else {
$summarySb.Append("\n<i>No baseline provided. Copy a set of BCPT results to $([System.IO.Path]::GetFileName($baseLinePath)) in the project folder in order to establish a baseline.</i>") | Out-Null
}
$summarySb.ToString()
}