Generic sample of how to use the Load Impact API in Powershell script
Switch branches/tags
Nothing to show
Clone or download
micsjo Update README.md
Added runtime sample
Latest commit d558cc7 Feb 7, 2017
Permalink
Failed to load latest commit information.
media Add files via upload Feb 6, 2017
LICENSE Initial commit Feb 6, 2017
LoadImpact_API.ps1 Add files via upload Feb 6, 2017
README.md Update README.md Feb 7, 2017

README.md

Automated Performance Testing with Powershell

Powershell

Load Impact can be used from any command line or scripting environment capable of doing http REST calls.

Using our robust and extensible APIs you can integrate Load Impact’s world-leading performance testing platform into your automated build and test process.

Load Impact covers your back with everything you need for Web, API and application performance testing. And test scripting is simple.

To get started, try this sample of how to run performance testing from your Windows Powershell environment.

Continuous Delivery. Continuous performance feedback. Simple.

This sample assumes you are familiar with how to execute Powershell scripts.

Is also assumes you have a Load Impact account. If not, go get one – it’s free.

Set up your environment

Executing Load Impact tests from script allows you to integrate into any platform, tool or environment of your choice that can execute Powershell scripts.

So setting up your environment is really up to which you would like to use.

You do need Powershell >= v4 since Invoke-Webrequest isn’t introduced until that version.

The current version at the time of writing is v5.1 so as long as you are recently updated you should be fine.

The details are all in the PowerShell script for the execution so we will take a look at what it does in some detail.

You can get the code from github in the loadimpact/genericwindows repo where it is shared.

Integrate with the Load Impact API

Before we dive into the details – let’s get some essentials from your Load Impact account. We need the API key so you can access the API and a test to run.

The API key you get in your Load Impact account when you are logged in

Go to “Monitoring” on the left and click “Use REST API”.

Then copy it from the yellow text box.

Just note that the API token is longer than the size of the box so make sure you get all of it!

Now you need to know which test to run. You can list your test configurations using the API or the CLI if you want to but the simplest way is to open it directly in your account and copy the id from the URL. Underlined in red.

So now you have a test id for the test you want to run in your build pipeline and your API key.

All of the code is shared at Github for your download in the loadimpact/genericwindows repo!

3a Edit the script to set the test Id and the API key

The code has four parts, the initial and then three stages “Kickoff performance test”, “Performance test running” and “Show results”. The output from the execution is visible in the console when you execute your Powershell script including the Load Impact performance test.

The initial part of the PowerShell code is where you set the test id and the API key.

<# Load Impact test id #>
$testId = YOUR_TEST_ID_HERE
<# API_KEY from your Load Impact account #>
$API_KEY = "YOUR_API_KEY_HERE" + ":"

$auth = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($API_KEY))

So replace “YOUR_TEST_ID_HERE” with your test id, just the number – not a string.

And replace “YOUR_API_KEY_HERE” with your API key. Keep inside the quotes (it is a string) and remember to keep the ‘:’ added at the end. It is basic AUTH, the username is the API key with a blank password and that is why we base 64 encode the authorization for later use.

3b Kick off a performance test

$uri = "https://api.loadimpact.com/v2/test-configs/" + $testId + "/start"

Write-Host "Load Impact performance test"
Write-Host "Kickoff performance test"

<# try-catch because PS considers all 400+ codes to be errors and will exit if returned #>
$resp = $null
try {
  $resp = Invoke-WebRequest -uri $uri -Method Post -Headers @{'Authorization'=$auth}
} catch {}

<# Status 201 expected, 200 is just a current running test id #>

if ($resp.StatusCode -ne 201) {
  Write-Host "Could not start test $testId : $resp.StatusCode `n$resp.Content" 
  return
}

$js = ConvertFrom-Json -InputObject $resp.Content

$tid = $js.id

<# Until 5 minutes timout or status is running   #> 

$t = 0
$uri = "https://api.loadimpact.com/v2/tests/" + $tid + "/"
do {
  Start-Sleep -Seconds 10
  $resp = Invoke-WebRequest -uri $uri -Method Get -Headers @{'Authorization'=$auth}
  $j = ConvertFrom-Json -InputObject $resp.Content
  $status_text = $j.status_text
  $t = $t + 10

  if ($t -gt 300) {
    Write-Host "Timeout - test start > 5 min" 
    return
  }
} until ($status_text -eq "Running")

Write-Host "Performance test running"

We kick off the performance test by gluing together the URI for the API to start the test and then write the status of the test to the console.

In PowerShell Invoke-Webrequest will fail if the response code is not < 400 which is not really what we want so we try-catch all possible response codes and then specifically check for the expected 201.

If not there we will print a message on the console about a build problem including an error text and exit the script.

If it is good, we parse the json response and extract the running test id.

Then we let it take a maximum of five minutes for the test to actually kickoff. Since it can take Load Impact a couple of minutes to acquire and allocate all the resources (mainly load agents) needed we take some time to let the test reach the status of “Running”. The bigger the test, the more resources needed and the longer it can take. But remember, it’s a couple of minutes.

We get that status of the test by calling the API and parsing the json response to check for the status in the response.

The last thing we do is to output to the console the test is running.

3c The test is running

<# wait until completed #>

$maxVULoadTime = 0.0
$percentage = 0.0
$uri = "https://api.loadimpact.com/v2/tests/" + $tid + "/results?ids=__li_progress_percent_total"
$uril = "https://api.loadimpact.com/v2/tests/" + $tid + "/results?ids=__li_user_load_time"
do {
  Start-Sleep -Seconds 30

  <# Get percent completed #>
  $resp = Invoke-WebRequest -uri $uri -Method Get -Headers @{'Authorization'=$auth}
  $j = ConvertFrom-Json -InputObject $resp.Content

  <# Since -Last 1 will get TWO on occassion we sort and get the first which will always get 1 #>
  $percentage = ($j.__li_progress_percent_total | Sort value -Descending | Select-Object -First 1).value

  Write-Host "Percentage completed $percentage"

  <# Get VU Load Time #>
  $resp = Invoke-WebRequest -uri $uril -Method Get -Headers @{'Authorization'=$auth}
  $j = ConvertFrom-Json -InputObject $resp.Content

  <# Sort and get the highest value #>
  $maxVULoadTime = ($j.__li_user_load_time | Sort value -Descending | Select-Object -First 1).value

  if ($maxVULoadTime -gt 1000) {
    Write-Host " VU Load Time exceeded limit of 1 sec: $maxVULoadTime"
    return
  }

} until ([double]$percentage -eq 100.0)

So now your Load Impact performance test is running!

This time we wait until the test has completed, reached the percentage completed value of 100% with a slightly longer sleep between refreshing status calls.

We do this by calling the API for results and only requesting the percentage completed. The API returns all of the relevant data so we do some json parsing and just get the last percentage value from the result set.

All the results are available from the API so you can either use them or calculate new aggregate results to use as test thresholds for your pipeline test results.

We included an example of making a threshold from the VU Load Time (please read the explanation of this value before using it).

We get the value by calling the same API as before but for the VU Load Time result, parse the json and get the max value by some PowerShell magic sorting and selecting.

If the value exceeds 1 second we exit the build step and fail the build by writing to the console.

3d Show the results

<# show results #>
Write-Host "Show results"
Write-Host "Max VU Load Time: $maxVULoadTime
Write-Host "Full results at https://app.loadimpact.com/test-runs/$tid

Finally, we show the results and output the max VU Load Time. It can of course be any result but as a sample.

You can use this result to decide on further actions in your execution context as well but that is outside the scope of this sample. And of course we tack on a direct link to the full results and analysis in Load Impact.

Finally, executing the build.

C:\>powershell -f LoadImpact_API.ps1
Load Impact performance test
Kickoff performance test
Performance test running
Percentage completed 10.0
Percentage completed 20.0
Percentage completed 30.333333333333336
Percentage completed 40.33333333333333
Percentage completed 50.33333333333333
Percentage completed 60.333333333333336
Percentage completed 70.33333333333334
Percentage completed 80.33333333333333
Percentage completed 90.66666666666666
Percentage completed 100.0
Show results
Max VU Load Time: 284.62
Full results at https://app.loadimpact.com/test-runs/3798633

Once started it will look something like the above.

There’s also a direct link to the full results and analysis in Load Impact where you can always find all the results of all your tests.

You can add/update/delete tests, user scenarios and data stores using the API and CLI, even stick all of it in your SCM of choice and let all of it be part of your build pipeline.

To dive deeper into using Load Impact from your CI/CD process (or for that matter any external usage) see our comprehensive API reference and the accompanying CLI.

Contact us today and we’ll throw in a free, one-hour consultation to help you get up and running quickly.