|
| 1 | +function InvokeRestMethod($headers, $contentType, $uri , $method= "Get", $body) |
| 2 | +{ |
| 3 | + $ServicePoint = [System.Net.ServicePointManager]::FindServicePoint($uri) |
| 4 | + $result = Invoke-RestMethod -ContentType "application/json" -UserAgent $global:userAgent -TimeoutSec $global:RestTimeout -Uri $uri -Method $method -Headers $headers -Body $body |
| 5 | + $ServicePoint.CloseConnectionGroup("") |
| 6 | + return $result |
| 7 | +} |
| 8 | + |
| 9 | +function ComposeTestDropJson($name, $duration, $homepage, $vu, $geoLocation) |
| 10 | +{ |
| 11 | + $tdjson = @" |
| 12 | + { |
| 13 | + "dropType": "InplaceDrop", |
| 14 | + "loadTestDefinition":{ |
| 15 | + "loadTestName":"$name", |
| 16 | + "runDuration":$duration, |
| 17 | + "urls":["$homepage"], |
| 18 | + "browserMixs":[ |
| 19 | + {"browserName":"Internet Explorer 11.0","browserPercentage":60.0}, |
| 20 | + {"browserName":"Chrome 2","browserPercentage":40.0} |
| 21 | + ], |
| 22 | + "loadPatternName":"Constant", |
| 23 | + "maxVusers":$vu, |
| 24 | + "loadGenerationGeoLocations":[ |
| 25 | + {"Location":"$geoLocation","Percentage":100} |
| 26 | + ] |
| 27 | + } |
| 28 | + } |
| 29 | +"@ |
| 30 | + |
| 31 | + return $tdjson |
| 32 | +} |
| 33 | + |
| 34 | +function CreateTestDrop($headers, $dropJson, $CltAccountUrl) |
| 35 | +{ |
| 36 | + $uri = [String]::Format("{0}/_apis/clt/testdrops?api-version=1.0", $CltAccountUrl) |
| 37 | + $drop = InvokeRestMethod -contentType "application/json" -uri $uri -method Post -headers $headers -body $dropJson |
| 38 | + |
| 39 | + return $drop |
| 40 | +} |
| 41 | + |
| 42 | +function GetTestDrop($headers, $drop, $CltAccountUrl) |
| 43 | +{ |
| 44 | + $uri = [String]::Format("{0}/_apis/clt/testdrops/{1}?api-version=1.0", $CltAccountUrl, $drop.id) |
| 45 | + $testdrop = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers |
| 46 | + |
| 47 | + return $testdrop |
| 48 | +} |
| 49 | + |
| 50 | +function UploadTestDrop($testdrop) |
| 51 | +{ |
| 52 | + $uri = New-Object System.Uri($testdrop.accessData.dropContainerUrl) |
| 53 | + $sas = New-Object Microsoft.WindowsAzure.Storage.Auth.StorageCredentials($testdrop.accessData.sasKey) |
| 54 | + $container = New-Object Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer($uri, $sas) |
| 55 | + |
| 56 | + return $container |
| 57 | +} |
| 58 | + |
| 59 | +#function GetTestRuns($headers, $CltAccountUrl) |
| 60 | +#{ |
| 61 | +# $uri = [String]::Format("{0}/_apis/clt/testruns?api-version=1.0", $CltAccountUrl) |
| 62 | +# $runs = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers |
| 63 | + |
| 64 | +# return $runs |
| 65 | +#} |
| 66 | + |
| 67 | +function GetTestRunUri($testRunId, $headers, $CltAccountUrl) |
| 68 | +{ |
| 69 | + $uri = [String]::Format("{0}/_apis/clt/testruns/{1}?api-version=1.0", $CltAccountUrl,$testRunId) |
| 70 | + $run = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers |
| 71 | + |
| 72 | + return $run.WebResultUrl |
| 73 | +} |
| 74 | + |
| 75 | +function RunInProgress($run) |
| 76 | +{ |
| 77 | + return $run.state -eq "queued" -or $run.state -eq "inProgress" |
| 78 | +} |
| 79 | + |
| 80 | +function MonitorTestRun($headers, $run, $CltAccountUrl) |
| 81 | +{ |
| 82 | + $uri = [String]::Format("{0}/_apis/clt/testruns/{1}?api-version=1.0", $CltAccountUrl, $run.id) |
| 83 | + $prevState = $run.state |
| 84 | + $prevSubState = $run.subState |
| 85 | + Write-Output ("Load test '{0}' is in state '{1}|{2}'." -f $run.name, $run.state, $run.subState) |
| 86 | + |
| 87 | + do |
| 88 | + { |
| 89 | + Start-Sleep -s 15 |
| 90 | + $run = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers |
| 91 | + if ($prevState -ne $run.state -or $prevSubState -ne $run.subState) |
| 92 | + { |
| 93 | + $prevState = $run.state |
| 94 | + $prevSubState = $run.subState |
| 95 | + Write-Output ("Load test '{0}' is in state '{1}|{2}'." -f $run.name, $run.state, $run.subState) |
| 96 | + } |
| 97 | + } |
| 98 | + while (RunInProgress $run) |
| 99 | + |
| 100 | + $run = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers |
| 101 | + Write-Output "------------------------------------" |
| 102 | + $uri = [String]::Format("{0}/_apis/clt/testruns/{1}/messages?api-version=1.0", $CltAccountUrl, $run.id) |
| 103 | + $messages = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers |
| 104 | + |
| 105 | + if ($messages) |
| 106 | + { |
| 107 | + $timeSorted = $messages.value | Sort-Object loggedDate |
| 108 | + foreach ($message in $timeSorted) |
| 109 | + { |
| 110 | + switch ($message.messageType) |
| 111 | + { |
| 112 | + "info" { Write-Host -NoNewline ("[Message]{0}" -f $message.message) } |
| 113 | + "output" { Write-Host -NoNewline ("[Output]{0}" -f $message.message) } |
| 114 | + "warning" { Write-Warning $message.message } |
| 115 | + "error" { Write-Error $message.message } |
| 116 | + "critical" { Write-Error $message.message } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + Write-Output "------------------------------------" |
| 122 | +} |
| 123 | + |
| 124 | +function ComposeTestRunJson($name, $tdid, $machineType) |
| 125 | +{ |
| 126 | + $trjson = @" |
| 127 | + { |
| 128 | + "name":"$name", |
| 129 | + "description":"Quick perf test from automation task", |
| 130 | + "testSettings":{"cleanupCommand":"", "hostProcessPlatform":"x64", "setupCommand":""}, |
| 131 | + "superSedeRunSettings":{"loadGeneratorMachinesType":"$machineType"}, |
| 132 | + "testDrop":{"id":"$tdid"}, |
| 133 | + "runSourceIdentifier":"build/$env:SYSTEM_DEFINITIONID/$env:BUILD_BUILDID" |
| 134 | + } |
| 135 | +"@ |
| 136 | + |
| 137 | + return $trjson |
| 138 | +} |
| 139 | + |
| 140 | +function QueueTestRun($headers, $runJson, $CltAccountUrl) |
| 141 | +{ |
| 142 | + $uri = [String]::Format("{0}/_apis/clt/testruns?api-version=1.0", $CltAccountUrl) |
| 143 | + $run = InvokeRestMethod -contentType "application/json" -uri $uri -method Post -headers $headers -body $runJson |
| 144 | + |
| 145 | + $start = @" |
| 146 | + { |
| 147 | + "state": "queued" |
| 148 | + } |
| 149 | +"@ |
| 150 | + |
| 151 | + $uri = [String]::Format("{0}/_apis/clt/testruns/{1}?api-version=1.0", $CltAccountUrl, $run.id) |
| 152 | + InvokeRestMethod -contentType "application/json" -uri $uri -method Patch -headers $headers -body $start |
| 153 | + $run = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers |
| 154 | + |
| 155 | + return $run |
| 156 | +} |
| 157 | + |
| 158 | +function ComposeAccountUrl($connectedServiceUrl, $headers) |
| 159 | +{ |
| 160 | + #Load all dependent files for execution |
| 161 | + . $PSScriptRoot/VssConnectionHelper.ps1 |
| 162 | + $connectedServiceUrl = $connectedServiceUrl.TrimEnd('/') |
| 163 | + Write-Host "Getting Clt Endpoint:" |
| 164 | + $elsUrl = Get-CltEndpoint $connectedServiceUrl $headers |
| 165 | + |
| 166 | + return $elsUrl |
| 167 | +} |
| 168 | + |
| 169 | +function ValidateInputs($websiteUrl, $tfsCollectionUrl, $connectedServiceName) |
| 170 | +{ |
| 171 | + if (![System.Uri]::IsWellFormedUriString($websiteUrl, [System.UriKind]::Absolute)) |
| 172 | + { |
| 173 | + throw "Website Url is not well formed." |
| 174 | + } |
| 175 | + |
| 176 | + if([string]::IsNullOrWhiteSpace($connectedServiceName) -and $tfsCollectionUrl -notlike "*VISUALSTUDIO.COM*" -and $tfsCollectionUrl -notlike "*TFSALLIN.NET*") |
| 177 | + { |
| 178 | + throw "VS Team Services Connection is mandatory for non hosted TFS builds " |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +function UploadSummaryMdReport($summaryMdPath) |
| 183 | +{ |
| 184 | + Write-Verbose "Summary Markdown Path = $summaryMdPath" |
| 185 | + |
| 186 | + if (($env:SYSTEM_HOSTTYPE -eq "build") -and (Test-Path($summaryMdPath))) |
| 187 | + { |
| 188 | + Write-Host "##vso[task.addattachment type=Distributedtask.Core.Summary;name=Load test results;]$summaryMdPath" |
| 189 | + } |
| 190 | +} |
| 191 | + |
0 commit comments