Tuesday, October 4, 2022

[SOLVED] What does time_total include that is shown in the curl --write-out format

Issue

I am using this curl command:

curl -w @curl-format.txt \
    --request POST "https://api.mydomain.com/v2/customer/cust_report" \
    --header "Authorization: Bearer MY_API_KEY" \
    --header "Content-Type: application/json" \
    --data-raw '{
        "ipAddress": "MY_IP_ADDRESS",
        "userId": "MY_USER_ID"
    }'

curl-format.txt:

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

With the above, I get the following output:

                     ----------
     time_namelookup:  0.252513s
        time_connect:  0.270122s
     time_appconnect:  0.312970s
    time_pretransfer:  0.313064s
       time_redirect:  0.000000s
  time_starttransfer:  0.313085s
                     ----------
          time_total:  1.285209s

The time_total description shows on the curl manpage (https://curl.se/docs/manpage.html):

time_total The total time, in seconds, that the full operation lasted.

So, I was not expecting the sum of all 6 values in my output to be more than the value of time_total, but it is 1.461754s.

How does my total becomes larger than the value of time_total?


Solution

You can't just sum them up due two reasons

  1. As man page describes time_namelookup, time_pretransfer and time_starttransfer: "The time, in seconds, it took from the start until"
  2. time_total includes all time including data transfer stage, that starts from time_starttransfer

For better understanding I would recommend to look at diagram at https://blog.cloudflare.com/a-question-of-timing/



Answered By - mike
Answer Checked By - Willingham (WPSolving Volunteer)