Presenting time details with cURL

September 10, 2022 No comments curl time bash

1. Introduction

cURL is a friendly tool used by many programmers and DevOps around the world. Sometimes there is a need to measure how long our request takes, and how long we wait for the server to respond. In this article, we show how to present timing details with curl.

2. -w,--write-out <format> parameter

cURL offers formatted output for the request details. We will focus on details related to time.

  • time_appconnect The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed.
  • time_connect The time, in seconds, it took from the start until the TCP connection to the remote host (or proxy) was completed.
  • time_namelookup The time, in seconds, it took from the start until the name resolving was completed.
  • time_pretransfer The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.
  • time_redirect The time, in seconds, it took for all redirection steps including name lookup, connect, pretransfer, and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections.
  • time_starttransfer The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.
  • time_total The total time, in seconds, that the full operation lasted.

To display time information to the standard output first we need to:

1) Create a new file, time-details.txt, and paste in:

touch time-details.txt
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
time_total: %{time_total}\n

2) Next, make a request using this file in the -w parameter with @:

curl -w "@time-details.txt" -o /dev/null -s ""

on Windows use:

curl -w "@time-details.txt" -o NUL -s ""

What this command will do:

  • -w "@time-details.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 display a progress meter,
  • "" - is the URL we are requesting.

The output after we run that command:

time_namelookup: 0,000564
time_connect: 0,010830
time_appconnect: 0,000000
time_pretransfer: 0,010922
time_redirect: 0,000000
time_starttransfer: 0,345097
time_total: 0,345202

3. cURL with time details wrapper

We could use a bash script that will work as a curl wrapper returning timing information.

1) First, create a curlt file

touch curlt

2) Paste the following content to the file

# Example usage:
#   $ curlt
#   $ time curlt -v

set -e

 "time_namelookup": %{time_namelookup},
 "time_connect": %{time_connect},
 "time_appconnect": %{time_appconnect},
 "time_pretransfer": %{time_pretransfer},
 "time_redirect": %{time_redirect},
 "time_starttransfer": %{time_starttransfer},
 "time_total": %{time_total}

exec curl -w "$curl_format" -o /dev/null -s "$@"

3) Make file executable

sudo chmod 777 curlt

4) Make a request


The output:

 "time_namelookup": 0,007869,
 "time_connect": 0,016145,
 "time_appconnect": 0,000000,
 "time_pretransfer": 0,016221,
 "time_redirect": 0,000000,
 "time_starttransfer": 0,442338,
 "time_total": 0,442457

4. Conclusion

In this article, we presented how to do a curl request with timing information.

{{ message }}

{{ 'Comments are closed.' | trans }}