Last active
July 4, 2022 12:28
-
-
Save romcaname/dfc98b4f5577fb0deb4fb2b5ea3c6017 to your computer and use it in GitHub Desktop.
a postman script to calculate the response time average and standard deviation of a request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function standardDeviation(values, avg) { | |
var squareDiffs = values.map(value => Math.pow(value - avg, 2)); | |
return Math.sqrt(average(squareDiffs)); | |
} | |
function average(data) { | |
return data.reduce((sum, value)=>sum + value) / data.length; | |
} | |
if (responseCode.code === 200 || responseCode.code === 201) { | |
response_array = globals['response_times'] ? JSON.parse(globals['response_times']) : [] | |
response_array.push(responseTime) | |
postman.setGlobalVariable("response_times", JSON.stringify(response_array)) | |
response_average = average(response_array); | |
postman.setGlobalVariable('response_average', response_average) | |
response_std = standardDeviation(response_array, response_average) | |
postman.setGlobalVariable('response_std', response_std) | |
} |
The export contains the total time so you can easily just divide that by number of requests. This is using Postman 7.33.1.
"totalTime": 18630,
"collection": {
"requests": [
{
"id": "1d67e71f-dbf3-46de-9b7a-fd566d1ba59d",
"method": "GET"
}
]
}
Because of some deprecated code parts here is the code for the main part of the script for current Postman version (7.36.1):
if (pm.response.code === 200 || pm.response.code === 201) {
response_array = pm.globals.get('response_times') ? JSON.parse(pm.globals('response_times')) : []
response_array.push(pm.response.responseTime)
pm.globals.set("response_times", JSON.stringify(response_array))
response_average = average(response_array);
pm.globals.set('response_average', response_average)
response_std = standardDeviation(response_array, response_average)
pm.globals.set('response_std', response_std)
}
When you want to use collection variables, use pm.collectionVariables
instead of pm.globals
. For environment variables use pm.environment
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@p0onage hmm when I export results I don't see the average response time. I see a list of response times in the
times
field, but I only see the latest run stored in thetime
field. I'm using postman v7.28.0.If the average is available in the exported results, then I think this script could still be useful for those wanting to understand distribution of their response times via the standard deviation.