How to measure request and response times at once using curl.
search cancel

How to measure request and response times at once using curl.

book

Article ID: 298157

calendar_today

Updated On:

Products

VMware Tanzu Application Service for VMs

Issue/Introduction


This document explains how to track request and response times for an API call using a curl command.

Sometimes, when you troubleshoot slow API calls, timeouts, connection resets, and similar scenarios you need to verify in which section of the flow the requests are taking longer, getting timeouts, etc. This article shows how to get a time summary when testing with curl.


Environment

Product Version: 4.0

Resolution


Instructions


Step 1) SSH into a Linux vm you want to use for testing the requests. In this example, we will use vim editor. Find out the executable path for 'vim' with the following command:
 
which vim

Step 2) Move to the executable directory, and make a file with vim, for example, we are going to create a file called "curltime".
 
cd /usr/bin/
vim curltime

Step 3) Inside the file, paste and save the following script:
 
#!/bin/bash
curl -w @- -o /dev/null -s "$@" <<'EOF'
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
EOF

Step 4) Give the file write/read/execute permissions with chmod 777 <file_name>. Example below:
 
chmod 777 curltime 


Step 5) All set. Test the command, instead of using 'curl' use 'curltime', if you called the file differently, then use the name you gave it in order to invoke the script:
 

curltime google.com


See the example step by step:


Screenshot 2023-08-10 at 13.18.10.png


Note: You can also include more filters in order to increase verbosity and enrich the output upon need, examples below,


curltime https://google.com:443 --trace-time -v -o /dev/null

 

router/e6f2a67d-bc87-49ba-a458-e524f2d646b6:/# curltime https://google.com:443 --trace-time -v -o /dev/null
19:36:54.461685 *   Trying 142.251.46.206:443...
19:36:54.491708 * Connected to google.com (142.251.46.206) port 443 (#0)
19:36:54.497072 * ALPN, offering h2
19:36:54.497176 * ALPN, offering http/1.1
19:36:54.584906 *  CAfile: /etc/ssl/certs/ca-certificates.crt
19:36:54.584982 *  CApath: /etc/ssl/certs

-------------------

19:36:55.038710 * TLSv1.2 (IN), TLS header, Supplemental data (23):
19:36:55.038919 { [5 bytes data]
19:36:55.039595 * Connection #0 to host google.com left intact
    time_namelookup:  0.001086
       time_connect:  0.031088
    time_appconnect:  0.181960
   time_pretransfer:  0.182862
      time_redirect:  0.000000
 time_starttransfer:  0.576149
                    ----------
         time_total:  0.579023