-
Notifications
You must be signed in to change notification settings - Fork 14
/
netperf-lib.psm1
91 lines (85 loc) · 3.35 KB
/
netperf-lib.psm1
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
function ValidateInheritedParams {
Write-Host "Inherited run id: $env:netperf_run_id"
Write-Host "Inherited role: $env:netperf_role"
Write-Host "Inherited remote_powershell_supported: $env:netperf_remote_powershell_supported"
Write-Host "Inherited api url: $env:netperf_api_url"
$headers = @{
'secret' = $env:netperf_syncer_secret
}
Invoke-WebRequest -Uri "$env:netperf_api_url/hello" -Headers $headers
}
function NetperfSendCommand {
param (
[Parameter(Mandatory = $true)]
[string]$Command
)
Write-Host "Sending command: $Command"
# Should send a command to the shared cache and wait for the server process to execute said command before exiting.
$headers = @{
"secret" = "$env:netperf_syncer_secret"
}
$url = "$env:netperf_api_url"
$RunId = "$env:netperf_run_id"
try {
$Response = Invoke-WebRequest -Uri "$url/getkeyvalue?key=$RunId" -Headers $headers -UseBasicParsing
} catch {
Write-Host "Unable to fetch state. Creating a new one now."
$state = [pscustomobject]@{
value=[pscustomobject]@{
"SeqNum" = 0
"Commands" = @($Command)
}}
$StateJson = $state | ConvertTo-Json
$Response = Invoke-WebRequest -Uri "$url/setkeyvalue?key=$RunId" -Headers $headers -Method Post -Body $StateJson -ContentType "application/json" -UseBasicParsing
if ($Response.StatusCode -ne 200) {
throw "Failed to set the key value!"
}
return
}
$CurrState = $Response.Content | ConvertFrom-Json
$CurrState.Commands += $Command
$CurrState = [pscustomobject]@{
value=$CurrState
}
$StateJson = $CurrState | ConvertTo-Json
$Response = Invoke-WebRequest -Uri "$url/setkeyvalue?key=$RunId" -Headers $headers -Method Post -Body $StateJson -ContentType "application/json" -UseBasicParsing
if ($Response.StatusCode -ne 200) {
throw "Failed to set the key value!"
}
}
function NetperfWaitServerFinishExecution {
param (
[Parameter(Mandatory = $false)]
[int]$MaxAttempts = 30,
[Parameter(Mandatory = $false)]
[int]$WaitPerAttempt = 8,
[Parameter(Mandatory = $false)]
[scriptblock]$UnblockRoutine = {}
)
for ($i = 0; $i -lt $maxattempts; $i++) {
$UnblockRoutine.Invoke()
Write-Host "Waiting for server to finish execution... Attempt $i"
Start-Sleep -Seconds $WaitPerAttempt
$headers = @{
"secret" = "$env:netperf_syncer_secret"
}
$url = "$env:netperf_api_url"
$RunId = "$env:netperf_run_id"
try {
$Response = Invoke-WebRequest -Uri "$url/getkeyvalue?key=$RunId" -Headers $headers -UseBasicParsing
if (!($Response.StatusCode -eq 200)) {
throw "Remote Cache State Not Set!"
}
} catch {
Write-Host "Failed to fetch state. Retrying... Error: $_"
continue
}
$CurrState = $Response.Content | ConvertFrom-Json
if ($CurrState.SeqNum -eq $CurrState.Commands.Count) {
return
} else {
Write-Host "Server not done yet. Seq num: $($CurrState.SeqNum), Commands count: $($CurrState.Commands.Count)"
}
}
throw "Server did not finish execution in time! Tried $maxattempts times with $WaitPerAttempt seconds interval."
}