I have a web service that receives data in JSON format, processes the data, and then returns the result to the requester.
I want to measure the request, response, and total time using cURL
.
My example request looks like:
curl -X POST -d @file server:port
and I currently measure this using the time
command in Linux:
time curl -X POST -d @file server:port
The time command only measures total time, though – which isn't quite what I am looking for.
Is there any way to measure request and response times using cURL
?
Best Answer
From this brilliant blog post... https://blog.josephscott.org/2011/10/14/timing-details-with-curl/
cURL supports formatted output for the details of the request (see the cURL manpage for details, under
-w, –write-out <format>
). For our purposes we’ll focus just on the timing details that are provided. Times below are in seconds.Create a new file,
curl-format.txt
, and paste in:Make a request:
Or on Windows, it's...
What this does:
-w "@curl-format.txt"
tells cURL to use our format file-o /dev/null
redirects the output of the request to /dev/null-s
tells cURL not to show a progress meter"http://wordpress.com/"
is the URL we are requesting. Use quotes particularly if your URL has "&" query string parametersAnd here is what you get back:
I have not yet seen an option to output the results in microseconds, but if you're aware of one, post in the comments below.
Make a Linux/Mac shortcut (alias)
Then you can simply call...
Thanks to commenter Pete Doyle!
Make a Linux/Mac stand-alone script
This script does not require a separate
.txt
file to contain the formatting.Create a new file,
curltime
, somewhere in your executable path, and paste in:Then call it the same way as the alias:
Make a Windows shortcut (aka BAT file)
Create a new text file called
curltime.bat
in the same folder ascurl.exe
andcurl-format.txt
, and paste in the following line:Then from the command line you can simply call:
(Make sure the folder is listed in your Windows
PATH
variable to be able to use the command from any folder.)