使用curl命令分析http请求耗时情况

使用curl命令发送http POST请求

curl -v -X POST -d @data.json --header "Content-Type: application/json" $url

curl -w选项

Defines what to display on stdout after a completed and successful operation. 
The format is a string that may contain plain text mixed with any number of variables. The string can be specified as "string", to get read from a particular file you specify it "@filename"
and to tell curl to read the format from stdin you write "@-".

 示例

[root@localhost ~]# cat format.txt 
\n\n
         http_code:  %{http_code}\n
        local_addr:  %{local_ip}:%{local_port}\n
       remote_addr:  %{remote_ip}:%{remote_port}\n
       size_upload:  %{size_upload}\n
      speed_upload:  %{speed_upload}\n
     size_download:  %{size_download}\n
    speed_download:  %{speed_download}\n
                   ----------\n
   time_namelookup:  %{time_namelookup}\n
      time_connect:  %{time_connect}\n
   time_appconnect:  %{time_appconnect}\n
     time_redirect:  %{time_redirect}\n
  time_pretransfer:  %{time_pretransfer}\n
time_starttransfer:  %{time_starttransfer}\n
                   ----------\n
        time_total:  %{time_total}\n
[root@localhost ~]# 
[root@localhost ~]# curl -v -w @format.txt -X POST -d @data.json --header "Content-Type: application/json" $url
* About to connect() to v.qq.com port 80 (#0)
*   Trying 8.8.8.8...
* Connected to v.qq.com (8.8.8.8) port 80 (#0)
> POST /api/v2/database/upload?name=ron&age=18 HTTP/1.1
> User-Agent: curl/7.29.0
> Host: v.qq.com
> Accept: */*
> Content-Type: application/json
> Content-Length: 717
> 
* upload completely sent off: 717 out of 717 bytes
< HTTP/1.1 200 OK
< Server: nginx
< Date: Tue, 19 Jan 2021 11:58:31 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 145
< Connection: keep-alive
< 
* Connection #0 to host v.qq.com left intact
{"code":200,"data":{"download_url":"","download_protocol":"","host":"","intra_host":"","name":"","version":"","size":0,"image_id":""},"msg":"ok"}

         http_code:  200
        local_addr:  10.0.0.9:32155
       remote_addr:  8.8.8.8:80
       size_upload:  717         // The total amount of bytes that were uploaded.
      speed_upload:  67.000      // The average upload speed that curl measured for the complete upload. Bytes per second.
     size_download:  145         // The total amount of bytes that were downloaded.
    speed_download:  13.000      // The average download speed that curl measured for the complete download. Bytes per second.
                   ----------
   time_namelookup:  10.523      // 从开始到完成域名解析的耗时
      time_connect:  10.548      // 从开始到完成TCP三次握手的耗时
   time_appconnect:  0.000       // 从开始到完成ssl/ssh等上层协议建立的耗时
     time_redirect:  0.000       // 
  time_pretransfer:  10.549      // 从请求开始到响应开始传输的时间
time_starttransfer:  10.642      // 从请求开始到第一个字节将要传输的时间,即Time To First Byte (TTFB) 
                   ----------
        time_total:  10.643
[root@localhost ~]# 

各变量对应的时间点详见下图

 

更多参考:A Question of Timing 

posted @ 2021-01-19 20:29  RainingInMacondo  阅读(1141)  评论(0编辑  收藏  举报