In the previous post about automated load testing, we didn’t have the room to include a proper sample. So that’s what this post is going to be about. The complete code sample can be found here https://github.com/loadimpact/loadimpactapi-samples
A few comments if you want to try it out:
At the beginning, we net to set token and test id. If you haven’t already done so, you can generate an API token on your accounts page https://loadimpact.com/account/. Please note that the API token is not the same as the server metrics token. The next thing is to find the test configuration id that you want to run.
Assuming that you already have an account and at least one test configuration created, you just go to your test configuration list and click on the test you’re interested in running. The URL will say https://loadimpact.com/test/config/edit/NNNNN where NNNNN is your test id. At the top of the test script, you find the two variables that you need to update. $token and $test_config_id.
$token = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
$test_config_id = 1234567;
$verbose = TRUE;
Then, the interesting part of the script is quite straightforward:
$resp = loadimpactapi("test-configs/$test_config_id/start", 'POST');
if(isset($resp->id)) {
$test_id = $resp->id; // The Id of the running test.
$running = TRUE;
$status = loadimpactapi("tests/$test_id", 'GET');
while($running) {
if($verbose) echo "Test $test_id is {$status->status_text} \n";
if($status->status > 2) {
$running = FALSE;
break;
}
sleep(15);
$status = loadimpactapi("tests/$test_id", 'GET');
}
// At this point, a status code != 3 would indicate a failure
if($status->status == 3) {
$jsonresult = loadimpactapi("tests/$test_id/results", 'GET');
$timestamps = resulttoarray($jsonresult);
echo responsetimeatmaxclients($timestamps) ."\n";
}
} else {
echo "Test $test_config_id failed to start \n";
}
Start the test, wait for it to finish and do something with the results. It’s really only the last part, ‘do something’ that’s deserves commenting on.
The LoadImpact API will return it’s data as two time series by default (you can ask for other time series as well). Basically, each of these two are a series of observations made during the test at given time intervals. The first series is the number of active clients seen at any given time. The other series represents the average response time from the test target. The two time series are synchronized via the timestamps (UNIX epoch). The code on github includes a function that massages these two time series into a single array of timestamps. At each timestamp, we can see number of active clients as well as the response time. So in the sample, I first run resulttoarray($jsonresult); so that I get an array that is easier to work with. Then I call responsetimeatmaxclients($timestamps) to find the response time found at the highest load during the test.
At the and, the return value is simply echoed to StdOut. Running the script, I’d get something like:
erik@erik-laptop$ php test.php
Test 1234567 is Created
Test 1234567 is Initializing
Test 1234567 is Running
Test 1234567 is Running
Test 1234567 is Running
Test 1234567 is Running
Test 1234567 is Running
Test 1234567 is Finished
3747.78
Since I’ve left $verbose=TRUE, I’ll get some status messages in the output. In a real scenario where the output is likely to be handled by a script, set $verbose=FALSE so that you just get the actual measurement back to StdOut.
Questions? Ideas? Opinions? Leave a comment below, we love to hear from you.
Pingback: Write Scalable Code – use Jenkins to Automate your Load Testing | Load Impact Blog