Performance testing with Postman [SAVE UKRAINE. STOP RUSSIA]
A lot of QA engineers have Postman collections for testing API endpoints. Usually, these collections contain tests that check:
- response codes
- response body structure
- response body contents
But sometimes we need to do performance tests, e.g. check that the response time is within some baseline values. But the response time for a single request is not representative due to various reasons:
- issues with a network
- issues with a test environment
- bad luck
That’s why we used to acquire specific performance testing tools like JMeter. But what if we want to use a single tool for testing our API from a functional and performance perspective?
Let’s try to solve this problem by Postman.
Inputs
- Endpoint to test: dummy.restapiexample.com/api/v1/employees
- Number of requests: 100
- The time of delay between requests: 100 ms
- The baseline value of 90 percentile response time: 1000 ms
- Goal: create a Postman test that checks the 90 percentile response time for a specified endpoint.
What is the 90 percentile response time?
To explain this concept, I want to give an example: let’s say we have to check the response time for the request:
GET dummy.restapiexample.com/api/v1/employees
We will send this request 100 times with a 100-ms delay. We will collect the response time values for each request. Then we’ll sort all the response time values in ascending order. Afterward, we’ll take the first 90% transactions out of this set. As a result, the response time that has the maximum value among them is considered as the 90 percentile response time.
Let’s start
Create a collection with a request you want to test.
Then open the Tests tab and write the following test script:
//1000 ms is a maximum allowed value according to requirements
maximumResponseTime = 1000;//100 is a number of sent requests according to requirements
iterations = 100;//100 ms is a delay between requests according to requirements
delay = 100;//responseTimes is an array for collecting response time values
responseTimes = [];i=0;
function sendRequest() {
pm.sendRequest({
url: "dummy.restapiexample.com/api/v1/employees",
method: 'GET'
}, function (err, res) {
pm.test("Response time is " + res.responseTime, function (){
pm.expect(err).to.equal(null);
pm.expect(res).to.have.property('code', 200);
responseTimes.push(res.responseTime);
});
if (i < iterations - 1) {
i++;
setTimeout(sendRequest, delay);
}
else {
percentile90ResponseTime = quantile(responseTimes, 90);
pm.test("90 percentile response time " + percentile90ResponseTime + " is lower than " + maximumResponseTime + ", the number of iterations is " + iterations, function () {
pm.expect(percentile90ResponseTime).to.be.below(maximumResponseTime);
});
}
});
}
sendRequest();function sortNumber(a,b) {
return a - b;
}function quantile(array, percentile) {
array.sort(sortNumber);
index = percentile/100. * (array.length-1);
if (Math.floor(index) == index) {
result = array[index];
} else {
j = Math.floor(index)
fraction = index - j;
result = array[j] + (array[j+1] - array[j]) * fraction;
}
return result;
}
In the 1st part of this script, we declare some baseline values: number of iterations, the maximum acceptable response time value and the time of delay between requests. Then we call functions that send our request for a specified number of iterations with a required delay. All the response time values are stored in responseTimes array.
The test that checks the 90 percentile response time calls a helper function called quantile(responseTimes, 90). It’s declared at the end of the script. This function uses another helper function sortNumber that is required for sorting our array in ascending order. For more information use this reference.
Results
When you hit Send the request, you can check the Console that will show the sequence of requests being sent.
The Test Results tab will show the response time records for all requests and the final test result: e.g. 90 percentile response time 808.1 is lower than 1000, the number of iterations is 100.
That’s it. You can use this script in your Postman tests and check it out. Good luck :)