Skip to content

Instantly share code, notes, and snippets.

@morozgrafix
Last active October 25, 2018 03:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morozgrafix/1ece4477191860ebb611 to your computer and use it in GitHub Desktop.
Save morozgrafix/1ece4477191860ebb611 to your computer and use it in GitHub Desktop.
Time CURL requests

From this brilliant blog post... https://josephscott.org/archives/2011/10/timing-details-with-curl/ found at http://stackoverflow.com/questions/18215389/how-do-i-measure-request-and-response-times-at-once-using-curl

cURL supports formatted output for the details of the request (see the cURL manpage for details, under -w, –write-out ). For our purposes we’ll focus just on the timing details that are provided.

###Prepwork

Create .curl directory in your home folder:

mkdir $HOME/.curl

###Create a new file, curl-format.txt, and paste in:

    time_namelookup:  %{time_namelookup}\n
       time_connect:  %{time_connect}\n
    time_appconnect:  %{time_appconnect}\n
   time_pretransfer:  %{time_pretransfer}\n
      time_redirect:  %{time_redirect}\n
 time_starttransfer:  %{time_starttransfer}\n
                    ----------\n
         time_total:  %{time_total}\n

Save that file in your $HOME/curl

###Make a request:

curl -w "@curl-format.txt" -o /dev/null -s "http://wordpress.com/"

Or on Windows, it's... curl -w "@curl-format.txt" -o NUL -s "http://wordpress.com/"

###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 parameters

###And here is what you get back:

   time_namelookup:  0.001
      time_connect:  0.037
   time_appconnect:  0.000
  time_pretransfer:  0.037
     time_redirect:  0.000
time_starttransfer:  0.092
                   ----------
        time_total:  0.164

###Make alias and optionally add it to your .bash_profile

alias curltime="curl -w "@$HOME/.curl/curl-format.txt" -o /dev/null -s $1"

###Run

curltime http://www.google.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment