Measuring Request and Response Time with cURL

//

Yusuf Sezer

Measuring or calculating request and response time is essential when you want to know the network performance. For this, you can use a tool like cURL, which offers various parameters.

Whats is cURL?

Client URL or mostly known as cURL is a computer software project providing a library (libcurl) and command-line tool (curl) for transferring data using various network protocols (HTTP, HTTPS, FTP, etc.).

How To use cURL?

Whether you use Linux, macOS or even Windows, you can use the cURL that comes with the operating system. No matter which operating system you use, you can run curl tools in Terminal or Command Line.

curl https://www.yusufsezer.com

Depending on the operating system setting, cURL shows the output, if not, we need to add parameters to the command line and then we will see the output.

The following parameter tell curl to include the header output.

curl -i https://www.yusufsezer.com

The following parameter tells curl to provide the output in detail (v stands for verbose).

curl -vvvv https://www.yusufsezer.com

Similarly, we can use various cURL parameters. For more information, you can visit the curl man page.

How To Measure Request and Response Time with cURL?

cURL provides various parameters to get various request and response information such as request time and response time.

cURL has a -w (–write-out) parameter to get information from the request and response result.

The parameter has special format like %{which_information} to get information.

The following command returns the http status code.

curl -w %{http_code} https://www.yusufsezer.com

The following commands write the response time.

curl -w %{time_total} https://www.yusufsezer.com

The following list contains various output parameters that are often used to retrieve request and response information.

  • http_code: Returns http and ftp status-response code.
  • content_type: Returns content type if available.
  • time_namelookup: Gives the name resolution time.
  • time_connect: Gives TCP connection time.
  • url: Returns the fetched URL information.
  • time_redirect: Gives the redirect time, if any.
  • time_starttransfer: curl measure time to first byte

We can use multiple format parameters at the same time as follows.

curl -w "%{http_code} %{time_total}" https://www.yusufsezer.com

We can write something before and after the parameters to make the output more meaningful.

curl -w "HTTP code: %{http_code} Total Time: %{time_total} seconds" https://www.yusufsezer.com

It is good practice to save the formatting to file and use it as follows.

URL: %{url} \n
DNS lookup time: %{time_namelookup}  seconds\n
Connect time: %{time_connect}  seconds\n
Start transfer time: %{time_starttransfer}  seconds\n
HTTP code: %{http_code} \n
Total time: %{time_total} seconds\n

Then run it as follows.

curl -w "@format.txt" https://www.yusufsezer.com

You can edit the file according to the information you want.

Leave a Comment

Yusuf Sezer icon

About

another human being.

Connect

Subscribe

Join our email list to receive the latest updates.