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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because of some deprecated code parts here is the code for the main part of the script for current Postman version (7.36.1):
When you want to use collection variables, use
pm.collectionVariables
instead ofpm.globals
. For environment variables usepm.environment
.