curl 命令提供了 -w 参数,解释如下

-w, --write-out
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 \n, a carriage return with \r and a tab space with \t. 它能够按照指定的格式打印某些信息,里面可以使用某些特定的变量,而且支持 \n 、 \t 和 \r 转义字符。提供的变量很多,比如 status_code 、 local_port 、 size_download 等等,这篇文章我们只关注和请求时间有关的变量(以 time_ 开头的变量)

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

[root@node ~]# cat curl-format.txt
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 #变量解释如下
time_namelookup :DNS 域名解析的时候,就是把 https://zhihu.com 转换成 ip 地址的过程
time_connect :TCP 连接建立的时间,就是三次握手的时间
time_appconnect :SSL/SSH 等上层协议建立连接的时间,比如 connect/handshake 的时间
time_redirect :从开始到最后一个请求事务的时间
time_pretransfer :从请求开始到响应开始传输的时间
time_starttransfer :从请求开始到第一个字节将要传输的时间
time_total :这次请求花费的全部时间

看一下命令的输出

[root@node ~]# curl -w "@curl-format.txt" -o /dev/null -s -L "http://www.52py.com.cn"
time_namelookup: 0.124
time_connect: 0.132
time_appconnect: 0.257
time_redirect: 0.282
time_pretransfer: 0.257
time_starttransfer: 15.315
----------
time_total: 15.605 可以看到这次请求各个步骤的时间都打印出来了,每个数字的单位都是秒(seconds),这样可以分析哪一步比较耗时,方便定位问题。这个命令各个参数的意义:
  • -w :从文件中读取要打印信息的格式
  • -o /dev/null :把响应的内容丢弃,不关心,只关心请求的耗时情况
  • -s :不要打印进度条
从上面的输出,我们可以算出各个步骤的时间
  • DNS 查询:124ms
  • TCP 连接时间:pretransfter(257) - namelookup(124) = 133ms
  • SSL 协议处理时间: appconnect(257) - connect(132) = 125ms
  • 服务器处理时间:starttransfter(15.315) - pretransfer(257) = 15.058s
  • 内容传输时间:total(15.605) - starttransfer(15.315) = 290ms

curl分析请求的各个部分耗时情况的更多相关文章

  1. 如何使用cURL获得请求/响应具体耗时?

    如何使用cURL一次测量请求和响应时间? cURL支持格式化输出请求的详细信息(请参阅cURL手册页的-w.–write out<format>获取更多信息). 如题,我们将只关注如何知晓 ...

  2. 转:PHP中的使用curl发送请求(GET请求和POST请求)

    原文地址:http://www.jb51.net/article/104974.htm 使用CURL发送请求的基本流程 使用CURL的PHP扩展完成一个HTTP请求的发送一般有以下几个步骤: 1.初始 ...

  3. 使用PHP中的curl发送请求

    使用CURL发送请求的基本流程 使用CURL的PHP扩展完成一个HTTP请求的发送一般有以下几个步骤: 初始化连接句柄: 设置CURL选项: 执行并获取结果: 释放VURL连接句柄. 下面的程序片段是 ...

  4. curl查看请求你响应时间

    [root@localhost ~]# curl -o /dev/null -s -w time_namelookup:%{time_namelookup}"\n"time_con ...

  5. 基于binlog来分析mysql的行记录修改情况(python脚本分析)

          最近写完mysql flashback,突然发现还有有这种使用场景:有些情况下,可能会统计在某个时间段内,MySQL修改了多少数据量?发生了多少事务?主要是哪些表格发生变动?变动的数量是怎 ...

  6. 简单探究Android平台下' if ' 语句条件判断耗时情况

    2017年6月13日 前言 前几日在改Bug时看到好多调试时用的日志语句都被一个日志开关控制着它的执行权.形如: if(Constants.LOG_TAG){ Log.d(TAG, "Ini ...

  7. 基于binlog来分析mysql的行记录修改情况

    https://www.cnblogs.com/xinysu/archive/2017/05/26/6908722.html import pymysqlfrom pymysql.cursors im ...

  8. Tomcat源码分析——请求原理分析(中)

    前言 在<TOMCAT源码分析——请求原理分析(上)>一文中已经介绍了关于Tomcat7.0处理请求前作的初始化和准备工作,请读者在阅读本文前确保掌握<TOMCAT源码分析——请求原 ...

  9. Tomcat源码分析——请求原理分析(上)

    前言 谈起Tomcat的诞生,最早可以追溯到1995年.近20年来,Tomcat始终是使用最广泛的Web服务器,由于其使用Java语言开发,所以广为Java程序员所熟悉.很多人早期的J2EE项目,由程 ...

随机推荐

  1. 异常 Cannot resolve class or package

    spring boot yml配置异常Cannot resolve class or package 是因为mvaen设置 pom.xml的文件配置如上,scope 范围指定为runtime,runt ...

  2. 浅析 http 接口

     一.HTTP接口 Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页数据的时候,会发出一次Http请求.Http会通过TCP建立起一个到服务器的连接通道,当本次请求需要的数据完毕 ...

  3. HTTP Error 500.30 - ANCM In-Process Start Failure

    环境 windown 10 IIS 10 net core 2.2 vs2019 背景 在vs2019使用net core 2.2发布到IIS 10上(见在 ASP.NET Core 中使用多个环境) ...

  4. 【leetcode】1020. Partition Array Into Three Parts With Equal Sum

    题目如下: Given an array A of integers, return true if and only if we can partition the array into three ...

  5. centos6安装完成之后必要的配置

    centos6安装完成之后必要的配置 一配置yum源 [root@centos61 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirro ...

  6. python之字符串切分

    在工作中,经常遇到字符串切分,尤其是操作linux命令,返回一段文本,如下面这种格式 Filesystem Size Used Avail Use% Mounted on /dev/vda1 40G ...

  7. 误删SQL Server日志文件后怎样附加数据库

    SQL Server日志文件因为误操作被删除,当附加数据库的时候提示:附加数据库失败. 解决办法如下: 1.新建一个同名数据库. 2.停止数据库服务,覆盖新建的数据库主文件(小技巧:最好放在同一个磁盘 ...

  8. 用DELPHI中实现RAR文件解压到指定一目录

    一个RAR压缩文件,用DELPHI编的程序打开它并解压到某一目录,怎么实现的?自己搞定了例子:winrar.exe e -y C:\WINDOWS\Desktop\ghost.rar d:\ 但新的问 ...

  9. CodeForces - 990G (点分治+链表计数)

    题目:https://vjudge.net/contest/307753#problem/J 题意:一棵树,每个点都有个权值,现在问你,树上gcd每个不同的数有多少个 思路:点分治,首先范围只有 1e ...

  10. php理解非对称SSL加密解密、验证及签名

    加密方式分为对称加密和非对称加密,对称加密只使用一个秘钥,加密和解密都使用该秘钥:非对称加密则使用一对秘钥,使用公钥加密,私钥解密. 需要通过原生的openssl_public_encrypt加密,o ...