How to measure latency with curl

Sylvain Witmeyer
2 min readNov 23, 2020

When you deploy or migrate new infrastructure it can be useful to test the application's performance. Curl is often used to easily debug web requests. A few days ago, I had to temporarily insert an AWS NLB in front of our on-prem instances and I wanted to measure the overhead it adds.

Timing in command ine

You have probably already seen this waterfall in the chrome debug console.

But do you know you can get the same information on the command line with curl ?

Timing on demand

You can customize the output of curl. As stated in the man page

-w/ — write-out <format>
Defines what to display on stdout after a completed and successful operation. The format is a string that may contain plain text mixed with any number of variables.

Here is the output with all the variables I need.

"dnslookup: %{time_namelookup} | connect: %{time_connect} | appconnect: %{time_appconnect} | pretransfer: %{time_pretransfer} | redirect: %{time_redirect} | starttransfer: %{time_starttransfer} | total: %{time_total} | size: %{size_download}\n"

Checking kubernetes.io returns:

~ curl -w "\ndnslookup: %{time_namelookup} | connect: %{time_connect} | appconnect: %{time_appconnect} | pretransfer: %{time_pretransfer} | redirect: %{time_redirect} | starttransfer: %{time_starttransfer} | total: %{time_total} | size: %{size_download}\n" -so /dev/null https://kubernetes.iodnslookup: 0.113570 | connect: 0.163061 | appconnect: 0.274441 | pretransfer: 0.274648 | redirect: 0.000000 | starttransfer: 0.326740 | total: 0.377432 | size: 22426

We can also better format this output by using a template file. Create a curl_format.txt with this content:

\n   
namelookup: %{time_namelookup}\n
connect: %{time_connect}\n
appconnect: %{time_appconnect}\n
pretransfer: %{time_pretransfer}\n
redirect: %{time_redirect}\n
starttransfer: %{time_starttransfer}\n
— — — — — \n
total: %{time_total}\n
size: %{size_download}\n

Now the output is a bit cleaner


~ curl -w "@curl_format.txt" -so /dev/null https://kubernetes.io
namelookup: 0.160746
connect: 0.235043
appconnect: 0.393475
pretransfer: 0.393570
redirect: 0.000000
starttransfer: 0.476859
— — — — —
total: 0.559858
size: 22426

You can define an alias which will be available for the lifetime of your terminal session.

alias curl='curl -w "n\dnslookup: %{time_namelookup} | connect: %{time_connect} | appconnect: %{time_appconnect} | pretransfer: %{time_pretransfer} | redirect: %{time_redirect} | starttransfer: %{time_starttransfer} | total: %{time_total} | size: %{size_download}\n"'

Make it the default with .curlrc

Did you know that you can customize curl the same way you customize bash or vim ? Create a ~/.curlrc file and paste this line:

-w "dnslookup: %{time_namelookup} | connect: %{time_connect} | appconnect: %{time_appconnect} | pretransfer: %{time_pretransfer} | redirect: %{time_redirect} | starttransfer: %{time_starttransfer} | total: %{time_total} | size: %{size_download}\n"

See it in action

--

--