zoukankan      html  css  js  c++  java
  • 使用curl命令查看请求响应时间方法(转载)

    curl命令查看请求响应时间

    # curl -o /dev/null -s -w %{time_namelookup}::%{time_connect}::%{time_starttransfer}::%{time_total}::%{speed_download}"
    " "http://www.36nu.com"
    
    0.014::0.015::0.018::0.019::1516256.00

    -o:把curl 返回的html、js 写到垃圾回收站[ /dev/null] 

    -s:去掉所有状态

    -w:按照后面的格式写出rt

    time_namelookup:DNS 解析域名www.36nu.com的时间 

    time_commect:client和server端建立TCP 连接的时间

    time_starttransfer:从client发出请求;到web的server 响应第一个字节的时间

    time_total:client发出请求;到web的server发送会所有的相应数据的时间

    speed_download:下周速度  单位 byte/s

    上面这条命令及返回结果可以这么理解:

    0.014: DNS 服务器解析www.36nu.com 的时间单位是s   
    
    0.015: client发出请求,到c/s 建立TCP 的时间;里面包括DNS解析的时间  
    
    0.018: client发出请求;到s响应发出第一个字节开始的时间;包括前面的2个时间  
    
    0.019: client发出请求;到s把响应的数据全部发送给client;并关闭connect的时间  
    
    1516256.00 :下载数据的速度  

    建立TCP连接到server返回client第一个字节的时间:0.018s - 0.015s = 0.003s  

    server把响应数据发送给client的时间:0.019s - 0.018 = 0.01s  

    ################################

    curl 命令提供了 -w 参数,这个参数在 manpage 是这样解释的:

     -w, --write-out <format>
                  Make curl display information on stdout after a completed transfer. The format is a string that may contain plain text mixed with any number of variables. The  format
                  can  be  specified  as  a literal "string", or you can have curl read the format from a file with "@filename" and to tell curl to read the format from stdin you write
                  "@-".
    
                  The variables present in the output format will be substituted by the value or text that curl thinks fit, as described below. All variables are specified  as  %{vari‐
                  able_name} and to output a normal % you just write them as %%. You can output a newline by using 
    , a carriage return with 
     and a tab space with 	.
    
    它能够按照指定的格式打印某些信息,里面可以使用某些特定的变量,而且支持 
    、	和 
    转义字符。提供的变量很多,比如 status_code、local_port、size_download 等等,这篇文章我们只关注和请求时间有关的变量(以 time_ 开头的变量)。

    它能够按照指定的格式打印某些信息,里面可以使用某些特定的变量,而且支持  和  转义字符。提供的变量很多,比如 status_codelocal_portsize_download 等等,这篇文章我们只关注和请求时间有关的变量(以 time_ 开头的变量)。

    先往文本文件 curl-format.txt 写入下面的内容:

    ➜  ~ cat curl-format.txt
        time_namelookup:  %{time_namelookup}
    
           time_connect:  %{time_connect}
    
        time_appconnect:  %{time_appconnect}
    
          time_redirect:  %{time_redirect}
    
       time_pretransfer:  %{time_pretransfer}
    
     time_starttransfer:  %{time_starttransfer}
    
                        ----------
    
             time_total:  %{time_total}
    

    那么这些变量都是什么意思呢?我解释一下:

    • time_namelookup:DNS 域名解析的时候,就是把 https://zhihu.com 转换成 ip 地址的过程
    • time_connect:TCP 连接建立的时间,就是三次握手的时间
    • time_appconnect:SSL/SSH 等上层协议建立连接的时间,比如 connect/handshake 的时间
    • time_redirect:从开始到最后一个请求事务的时间
    • time_pretransfer:从请求开始到响应开始传输的时间
    • time_starttransfer:从请求开始到第一个字节将要传输的时间
    • time_total:这次请求花费的全部时间

    我们先看看一个简单的请求,没有重定向,也没有 SSL 协议的时间:

    ➜  ~ curl -w "@curl-format.txt" -o /dev/null -s -L "http://cizixs.com"
        time_namelookup:  0.012
           time_connect:  0.227
        time_appconnect:  0.000
          time_redirect:  0.000
       time_pretransfer:  0.227
     time_starttransfer:  0.443
                        ----------
             time_total:  0.867

    可以看到这次请求各个步骤的时间都打印出来了,每个数字的单位都是秒(seconds),这样可以分析哪一步比较耗时,方便定位问题。这个命令各个参数的意义:

    • -w:从文件中读取要打印信息的格式
    • -o /dev/null:把响应的内容丢弃,因为我们这里并不关心它,只关心请求的耗时情况
    • -s:不要打印进度条

    从这个输出,我们可以算出各个步骤的时间:

    • DNS 查询:12ms
    • TCP 连接时间:pretransfter(227) - namelookup(12) = 215ms
    • 服务器处理时间:starttransfter(443) - pretransfer(227) = 216ms
    • 内容传输时间:total(867) - starttransfer(443) = 424ms

    来个比较复杂的,访问某度首页,带有中间有重定向和 SSL 协议:

    ➜  ~ curl -w "@curl-format.txt" -o /dev/null -s -L "https://baidu.com"
        time_namelookup:  0.012
           time_connect:  0.018
        time_appconnect:  0.328
          time_redirect:  0.356
       time_pretransfer:  0.018
     time_starttransfer:  0.027
                        ----------
             time_total:  0.384

    可以看到 time_appconnect 和 time_redirect 都不是 0 了,其中 SSL 协议处理时间为 328-18=310ms。而且 pretransfer 和 starttransfer 的时间都缩短了,这是重定向之后请求的时间。 

    参考资料

    原文链接:https://blog.csdn.net/zzhongcy/article/details/100116926

    内容仅个人学习使用,不能作为商业用途,一经发现责任自负!

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    各种机器学习方法概念
    深入理解拉格朗日乘子法(Lagrange Multiplier) 和KKT条件
    肤色识别
    创建自己的窗口消息
    模糊C均值
    Fisher线性判别
    用遗传算法加强足球游戏的人工智能
    人工智能-遗传算法解决推箱子问题现实
    LBP特征
    VC 制作系统托盘程序实现将窗口最小化到系统托
  • 原文地址:https://www.cnblogs.com/name-lizonglin/p/12167808.html
Copyright © 2011-2022 走看看