前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Curl命令分析接口耗时

Curl命令分析接口耗时

作者头像
Luga Lee
发布2021-12-09 20:12:55
5.9K0
发布2021-12-09 20:12:55
举报
文章被收录于专栏:架构驿站架构驿站

Curl是一个非常实用的,用来与服务器之间传输数据的工具,支持的协议包括 (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP),Curl设计为无用户交互下完成工作。   Curl提供了一大堆非常有用的功能,包括代理访问、用户认证、Ftp上传下载、HTTP POST、SSL连接、Cookie支持、断点续传 。。。

本文主要分享下Curl -w参数相关功能,通过此命令行定位分析API接口的请求耗时情况,以便快速高效解决链路存在的问题。

Curl 命令提供了 -w 参数,此参数在 man page 解释如下:

代码语言:javascript
复制
-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 \n, a carriage return with \r and a tab space with \t.

即:此命令行能够按照指定的格式打印某些特定信息,里面可以使用某些特定的变量,而且支持 \n、\t和 \r 转义字符。并且其提供的变量也很多,比如 status_code、local_port、size_download 等等,在实际项目中我们只需关注和请求时间有关的变量(以 time_ 开头的变量),即可满足解决问题所需。

此参数如何使用呢?很简单,只需以下步骤即可完成:

1、创建curl-format.txt文本,定义参数变量

代码语言:javascript
复制
[administrator@JavaLangOutOfMemory ~ ]% vi 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:本次请求花费的总共时间

2、借助Curl命令行 curl -w "@curl-format.txt" -o /dev/null -s -L执行

代码语言:javascript
复制
[administrator@JavaLangOutOfMemory ~ ]% curl -w "@curl-format.txt" -o /dev/null -s -L "https://developer.ibm.com/zh/" 
time_namelookup:  0.002834
       time_connect:  0.044624
    time_appconnect:  0.166298
      time_redirect:  0.100450
   time_pretransfer:  0.166663
 time_starttransfer:  1.462707
                    ----------
         time_total:  1.574913

可以看到本次请求各个步骤的时间戳都打印出来了,每个数字的单位都是秒(seconds),这样可以分析哪一步、那一个环节比较耗时,方便定位问题。

这个命令各个参数的意义:

-w:从文件中读取要打印信息的格式

-o /dev/null:把响应的内容丢弃,因为我们这里并不关心它,只关心请求的耗时情况

-s:不要打印进度条

从这个结果输出可以得知:此站点带有中间有重定向和 SSL 协议。可以看到 time_appconnect 和 time_redirect 都不是 0 了,其中 SSL 协议处理时间为( 0.166298-0.044624 )s。而且 pretransfer 和 starttransfer 的时间都缩短了,这是重定向之后请求的时间。我们可以算出各个步骤的时间:

代码语言:javascript
复制
 DNS 查询:0.002834s
 TCP 连接时间:pretransfter(0.044624) - namelookup(0.002834) s
 服务器处理时间:starttransfter(1.462707) - pretransfer(0.166663) s
 内容传输时间:total(1.574913) - starttransfer(1.462707) s

综上所述,工作中若遇到某个接口请求的响应特别慢,我们可借助此种方法能够分析到底请求的哪一步耗时比较长,好进一步找到问题的根本原因。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-01-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 架构驿站 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档