-
Notifications
You must be signed in to change notification settings - Fork 592
/
Copy pathRunTests.ps1
237 lines (203 loc) · 9.78 KB
/
RunTests.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
param (
[String] $ProjectFilter = "*.pproj",
[Alias('ms')]
[Int] $MaxSteps = 200000,
[Alias('i')]
[Int] $NumIters = 10,
[Alias('p')]
[Int] $Parallel = 1,
[Alias('s')]
[Int64] $Seed = -1,
[ValidateSet('random', 'pos', 'feedback', 'feedbackpos')]
[Alias('sch')]
[String] $Scheduling = "pos",
[Alias('v')]
[Switch] $Verbose,
[Alias('m')]
[String[]] $TestMethods = @(),
[Alias('t')]
[String] $TestFilter = ".*",
[Alias('c')]
[Switch] $ContinueOnFailure,
[Alias('k')]
[Switch] $SkipBuildProject,
[Alias('w')]
[Int] $StartTaskDelayMilliSecs = 100,
[Alias('o')]
[Int] $TimeoutSecs = 0
);
try {
$projectPath = Get-ChildItem -Filter $ProjectFilter | Select-Object -First 1
$projectFolder = Split-Path -Parent $projectPath.FullName
[xml]$projectObj = Get-Content -Path $projectPath.FullName
$projectName = $projectObj.Project.ProjectName
} catch {
Write-Error "Cannot load project file: $ProjectFilter"
exit 1
}
if ($SkipBuildProject) {
Write-Host -ForegroundColor DarkYellow "Skip building project"
$LASTEXITCODE = 0
} else {
try {
Push-Location $projectFolder
Write-Host -ForegroundColor DarkYellow "Building project: $projectName"
dotnet tool run p compile --pproj $projectPath.FullName
} finally {
Pop-Location
}
}
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
if ($TestMethods.Count -eq 0) {
$TestMethods = dotnet tool run p check --list-tests | Select-String -SimpleMatch tc
Write-Host -ForegroundColor Blue "Test methods: {$TestMethods}"
}
$exitCode = 0
$testResults = @();
$failedTasks = @();
$startTime = Get-Date
$outputRoot = Join-Path "PCheckerOutput" $startTime.ToString("yyyy-MM-dd_HH-mm-ss")
foreach ($testMethod in $TestMethods) {
if (!($testMethod -match $TestFilter)) {
Write-Host -ForegroundColor Blue "Skipped test: $testMethod"
continue;
}
$testParams = "--fail-on-maxsteps --max-steps $MaxSteps --schedules $NumIters --sch-$Scheduling --testcase $testMethod"
if ($Seed -ge 0) { $testParams += " --seed $Seed" }
if ($Verbose) { $testParams += " --verbose" }
Write-Host -ForegroundColor DarkYellow "Running test: $testMethod"
# start the test tasks
$testStart = Get-Date
$testTasks = @{}
$testOutputs = @{}
for ($taskId = 0; $taskId -lt $Parallel; $taskId++) {
Start-Sleep -Milliseconds $StartTaskDelayMilliSecs
$outputPath = Join-Path $outputRoot "$testMethod" "t$taskId"
New-Item -ItemType Directory -Force $outputPath | Out-Null
$testOutput = New-Item (Join-Path $outputPath "test.log")
Write-Host "Test task output: $($testOutput.FullName)"
$testTask = Start-Process -NoNewWindow -Passthru -RedirectStandardOutput $testOutput -FilePath "dotnet" -ArgumentList "tool run p check $testParams --outdir $outputPath"
$testTasks[$taskId] = $testTask
$testOutputs[$taskId] = $testOutput
}
while ($true) {
$runningTasks = @()
for ($taskId = 0; $taskId -lt $Parallel; $taskId++) {
if (!$testTasks[$taskId].HasExited) { $runningTasks += $taskId }
}
if ($runningTasks.Count -eq 0) {
break;
}
Write-Host "[$(Get-Date)] Found $($runningTasks.Count) running test processes: $($runningTasks)"
Start-Sleep -Seconds 10
if ($TimeoutSecs -gt 0) {
$elapsedSecs = (New-TimeSpan -Start $testStart -End (Get-Date)).TotalSeconds
if ($elapsedSecs -ge $TimeoutSecs) {
Write-Host -ForegroundColor DarkYellow "[$(Get-Date)] Elapsed time ($($elapsedSecs.ToString("#.#"))s) exceeds timeout ($($timeoutSecs)s), stopping tests..."
for ($taskId = 0; $taskId -lt $Parallel; $taskId++) {
Stop-Process -Force $testTasks[$taskId]
$testTasks[$taskId].WaitForExit()
}
}
}
}
$elapsedSecs = (New-TimeSpan -Start $testStart -End (Get-Date)).TotalSeconds
Write-Host "[$(Get-Date)] Test processes stopped after running for $($elapsedSecs.ToString("#.#"))s"
# print outputs of test processes
for ($taskId = 0; $taskId -lt $Parallel; $taskId++) {
if ($Verbose) {
Copy-Item -Verbose -Force $testOutputs[$taskId] ($testMethod + ".$taskId.txt")
}
$numSchedPoints = Get-Content $testOutputs[$taskId] | Select-String -Pattern "(\d+) \(min\), (\d+) \(avg\), (\d+) \(max\)" |
Foreach-Object {
$min, $avg, $max = $_.Matches[0].Groups[1..3].Value
[PSCustomObject] @{
min = $min
avg = $avg
max = $max
}
} | Select-Object -Last 1
$taskName = "$testMethod[$taskId]"
$foundBug = Get-Content $testOutputs[$taskId] | Select-String -Pattern "Checker found a bug" |
Foreach-Object { ($_.Matches[0].Groups[0].Value) } | Select-Object -First 1
$testStatus = if ($null -eq $foundBug) { "pass" } else { "fail" }
$testSeed = Get-Content $testOutputs[$taskId] | Select-String -Pattern "Checker is using '[A-Za-z]+' strategy \(seed:(\d+)\)" |
Foreach-Object { [Int64]($_.Matches[0].Groups[1].Value) } | Select-Object -First 1
$testSchedules = Get-Content $testOutputs[$taskId] | Select-String -Pattern "Explored ([\d.]+) schedules" |
Foreach-Object { [Int64]($_.Matches[0].Groups[1].Value) } | Select-Object -First 1
$testSeconds = Get-Content $testOutputs[$taskId] | Select-String -Pattern "Elapsed ([\d.]+) sec" |
Foreach-Object { [Double]($_.Matches[0].Groups[1].Value) } | Select-Object -First 1
$testResults += [PSCustomObject] @{
test = $taskName
status = $testStatus
seed = $testSeed
schedules = $testSchedules
seconds = $testSeconds.ToString("0.0")
min = if ($null -ne $numSchedPoints) { $numSchedPoints.min } else { -1 }
avg = if ($null -ne $numSchedPoints) { $numSchedPoints.avg } else { -1 }
max = if ($null -ne $numSchedPoints) { $numSchedPoints.max } else { -1 }
};
if ($null -ne $foundBug) {
Write-Host -ForegroundColor DarkYellow "Test process output #${taskId}/${Parallel}:"
Get-Content $testOutputs[$taskId]
$checkerOutputPath = Get-Content $testOutputs[$taskId] | Select-String -Pattern "Writing (.*)" |
Foreach-Object { ($_.Matches[0].Groups[1].Value) } | Select-String -SimpleMatch -Pattern ".txt" | Select-Object -First 1
# filter key messages from output
$checkerOutputFile = Get-Item $checkerOutputPath
$debugLogFile = (Join-Path $checkerOutputFile.DirectoryName $checkerOutputFile.BaseName) + ".$testMethod" + ".csv"
$logPattern = @(
"sent event 'eWriteWork with payload", "sent event 'eWriteWorkDone with payload",
"sent event 'eCommitWork with payload", "sent event 'eCommitWorkDone with payload",
"sent event 'eWriteReq with payload", "sent event 'eWriteResp with payload",
"sent event 'eUpdateMsg with payload", "sent event 'eCommitMsg with payload",
"sent event 'eGetTargetSyncInfoResult with payload",
"sent event 'eSyncStartResp with payload",
"sent event 'eSyncDoneResp with payload",
"sent event 'eHaltReq with payload",
"sent event 'eShutDown with payload",
"sent event 'eRestart with payload",
# "dequeued event 'eUpdateTargetStateMsg with payload",
"dequeued event 'eNewRoutingInfo with payload",
"set its targets offline",
"replication chain updated",
"start write process",
"updatesOfChunkReplica",
# "aliveStorageServices",
"<ErrorLog>"
)
Select-String -Path $checkerOutputFile -SimpleMatch -CaseSensitive -Pattern $logPattern |
Select-Object -Property 'Line' -First 100000 |
Out-File -Width 10000 -Encoding utf8 $debugLogFile
Write-Host -ForegroundColor DarkYellow "Debug log: $debugLogFile"
# save reprod script to file
$outputPath = Join-Path $outputRoot "$testMethod" "t$taskId"
$reprodCmdstr = "dotnet tool run p check $testParams -s $testSeed --outdir $outputPath"
$reprodScriptFile = (Join-Path $checkerOutputFile.DirectoryName $checkerOutputFile.BaseName) + ".$testMethod" + ".ps1"
Set-Content -Path $reprodScriptFile -Value $reprodCmdstr
Write-Host -ForegroundColor DarkYellow "Reprod script: $reprodScriptFile"
Write-Host -ForegroundColor DarkYellow "Reprod command: $reprodCmdstr"
# set exit code to indicate the failure
$exitCode = -1
$failedTasks += $taskName
}
}
if (($exitCode -ne 0) -and (-not $ContinueOnFailure)) {
Write-Host -ForegroundColor DarkYellow "[$(Get-Date)] Test $testMethod failed, stopping..."
break;
}
}
$elapsedTime = New-TimeSpan -Start $startTime -End (Get-Date)
Write-Host -ForegroundColor DarkYellow "-----------------------"
Write-Host -ForegroundColor DarkYellow "Summary of test results"
Write-Host -ForegroundColor DarkYellow "-----------------------"
Write-Host -ForegroundColor DarkYellow "[$(Get-Date)] Elapsed time: $($elapsedTime.TotalSeconds.ToString(`"#.#`"))s"
Format-Table -AutoSize -InputObject $testResults
$testResults | Export-Csv -NoTypeInformation -Path (Join-Path $outputRoot "test_results.csv")
if ($exitCode -eq 0) {
Write-Host -ForegroundColor DarkYellow "[$(Get-Date)] All tests passed"
} else {
Write-Host -ForegroundColor DarkYellow "[$(Get-Date)] Failed test tasks: $failedTasks"
}
exit $exitCode