用linux curl命令来测试http/https各部分(dns,tcp,ssl等)的耗时

       来看下如下linux命令:

curl -X POST -w '\n\n    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}\n' \
  https://www.baidu.com:443/ \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -d '{
    "k1": "xxx",
    "k2": "yyy",
    "token": "123456789"
}'

       结果:

    time_namelookup:  0.004
       time_connect:  0.153
    time_appconnect:  0.702
   time_pretransfer:  0.702
      time_redirect:  0.000
 time_starttransfer:  0.863
                      ----------
         time_total:  0.865

 

        注意,上面的时间有累积性,单位是秒。

        比如:

        dns解析的耗时是0.004s

        tcp连接建立的耗时是: 0.153s - 0.004s

        https ssl交互的耗时是:0.702s - 0.153s

        https连接建立好到准备发送数据的耗时是: 0.702s - 0.702s

        此处不涉及重定向,故为0.000

        服务端收到https请求并处理完毕的耗时时间是:0.863s - 0.702s    (包含了服务端的所有处理逻辑)

        服务端数据返回到客户端的耗时是:0.865s - 0.863s

 

 

         再来做个简单实验,服务端go代码为:

package main
 
import (
    "io"
    "log"
    "time"
    "net/http"
)
 
func handlerHello(w http.ResponseWriter, r *http.Request) {
	time.Sleep(3 * time.Second)
    io.WriteString(w, "hello girls")
}
 
func main() {
    http.HandleFunc("/hello", handlerHello)     // 注册   
    err := http.ListenAndServe("localhost:8080", nil)
    if err != nil {
        log.Println(err)
    }
}

        执行如下命令:

curl -X POST -w '\n\n    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}\n' \
  http://localhost:8080/hello \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -d '{
    "k1": "xxx",
    "k2": "yyy",
    "token": "123456789"
}'

        结果是:

taoge:~$ curl -X POST -w '\n\n    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}\n' \
>   http://localhost:8080/hello \
>   -H 'Cache-Control: no-cache' \
>   -H 'Content-Type: application/json' \
>   -d '{
>     "k1": "xxx",
>     "k2": "yyy",
>     "token": "123456789"
> }'
hello girls

    time_namelookup:  0.004930
       time_connect:  0.005109
    time_appconnect:  0.000000
   time_pretransfer:  0.005150
      time_redirect:  0.000000
 time_starttransfer:  3.007478
                      ----------
         time_total:  3.007560
taoge:~$

         注意那个3s, 好好理解一下。

         不多说。

 

 

你可能感兴趣的:(s2:,Linux杂项,s2:,实用工具)