ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Curl命令分析接口耗时

2022-07-05 17:00:20  阅读:228  来源: 互联网

标签:请求 format 接口 耗时 time Curl curl pretransfer


https://cloud.tencent.com/developer/article/1916475
Curl命令分析接口耗时
url是一个非常实用的,用来与服务器之间传输数据的工具,支持的协议包括 (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 解释如下:

-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_ 开头的变量),即可满足解决问题所需。

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

1、创建curl-format.txt文本,定义参数变量
[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执行
[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 的时间都缩短了,这是重定向之后请求的时间。我们可以算出各个步骤的时间:

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

标签:请求,format,接口,耗时,time,Curl,curl,pretransfer
来源: https://www.cnblogs.com/xiedy001/p/16447112.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有