Skip to content

Instantly share code, notes, and snippets.

@romcaname
Last active July 4, 2022 12:28
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save romcaname/dfc98b4f5577fb0deb4fb2b5ea3c6017 to your computer and use it in GitHub Desktop.
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
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
Copy link

Hey @romcaname ,

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

Thanks!

@PaulBunkerWhich
Copy link

Hey @romcaname

I am also wondering how to implement this.

Thanks, -Paul

@electr0sheep
Copy link

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
Copy link
Author

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

Thank you for your detailed explanation!

@p0onage
Copy link

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
Copy link
Author

@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.

@beccagsc
Copy link

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"
			}
		]
	}

@martinharder
Copy link

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