Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a postman script to calculate the response time average and standard deviation of a request
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)
}
@gabrielcraveiro

This comment has been minimized.

Copy link

@gabrielcraveiro gabrielcraveiro commented Jan 4, 2018

Hey @romcaname ,

I'm new to postman tests, how would I go about using this with pm.test?

Thanks!

@PaulBunkerWhich

This comment has been minimized.

Copy link

@PaulBunkerWhich PaulBunkerWhich commented Jan 9, 2018

Hey @romcaname

I am also wondering how to implement this.

Thanks, -Paul

@electr0sheep

This comment has been minimized.

Copy link

@electr0sheep electr0sheep commented Jan 16, 2019

Hey guys, Postman has a Pre-request Script and Tests tab with each request.

Unfortunately, "Tests" is, imho, very poorly named. It should have been named Post-request Script.

That being said, if you copy the above javascript into the "Tests" tab of the request you're benchmarking, you can find the data in your global variables.

I personally used this in conjunction with the ability postman gives you to run x number of iterations to get some stats on a certain request. Essentially, I put the request in its own collection, and then ran that collection 100 times. Just be sure to have the "keep variables" or "persist variables" option checked.

@romcaname

This comment has been minimized.

Copy link
Owner Author

@romcaname romcaname commented Oct 17, 2019

@electr0sheep has it right. This is how I've made use of the script.

Thank you for your detailed explanation!

@p0onage

This comment has been minimized.

Copy link

@p0onage p0onage commented Jul 9, 2020

I don't think this is needed anymore when you export the results of your test runner it gives you the average response time and the milliseconds of each request

@romcaname

This comment has been minimized.

Copy link
Owner Author

@romcaname romcaname commented Jul 17, 2020

@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 the time field. I'm using postman v7.28.0.

{
	"id": "1a46165d-e0b7-4379-951f-6da25ce99662",
	"name": "Create Token",
	"time": 73,
	"responseCode": {
		"code": 401,
		"name": "Unauthorized"
	},
	"tests": {},
	"testPassFailCounts": {},
	"times": [
		189,
		73,
		73
	],
	"allTests": [
		{},
		{},
		{}
	]
},

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.