使用curl命令分析请求的耗时情况

简介:

最近工作中遇到一个问题,某个请求的响应特别慢,因此我就希望有一种方法能够分析到底请求的哪一步耗时比较长,好进一步找到问题的原因。在网络上搜索了一下,发现了一个非常好用的方法, curl 命令就能帮你分析请求的各个部分耗时。

使用curl命令分析请求的耗时情况

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

 
  1. -w, --write-out  
  2. 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 
  3. 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 
  4. "@-"
  5. 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‐ 
  6. 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 写入下面的内容:

 
  1.  ~ cat curl-format.txt  
  2. time_namelookup: %{time_namelookup}\n 
  3. time_connect: %{time_connect}\n  
  4. time_appconnect: %{time_appconnect}\n  
  5. time_redirect: %{time_redirect}\n  
  6. time_pretransfer: %{time_pretransfer}\n  
  7. time_starttransfer: %{time_starttransfer}\n  
  8. ----------\n 
  9. 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 :这次请求花费的全部时间

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

 
  1.  ~ curl -w "@curl-format.txt" -o /dev/null -s -L "http://cizixs.com"  
  2. time_namelookup: 0.012  
  3. time_connect: 0.227  
  4. time_appconnect: 0.000  
  5. time_redirect: 0.000  
  6. time_pretransfer: 0.227  
  7. time_starttransfer: 0.443  
  8. ----------  
  9. 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 协议:

 
  1.  ~ curl -w "@curl-format.txt" -o /dev/null -s -L "https://baidu.com"  
  2. time_namelookup: 0.012  
  3. time_connect: 0.018  
  4. time_appconnect: 0.328  
  5. time_redirect: 0.356  
  6. time_pretransfer: 0.018  
  7. time_starttransfer: 0.027  
  8. ----------  
  9. time_total: 0.384 

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


作者:佚名

来源:51CTO

相关文章
|
3月前
|
Web App开发 iOS开发 Windows
一个典型的 curl 请求分析的解释
一个典型的 curl 请求分析的解释
41 0
|
17天前
|
JSON 测试技术 数据格式
性能工具之 Locust 工具 Get 与 Post 请求
【4月更文挑战第7天】性能工具之 Locust 工具 Get 与 Post 请求
25 1
|
测试技术 Linux
三分钟搞定压力测试之http_load
本文对一个正在云服务器上运行的系统进行一个并发访问的压力测试,以此来模仿真实环境并发访问造成资源带宽不稳定出现的种种问题。
301 0
三分钟搞定压力测试之http_load
|
4天前
|
测试技术 Linux
wrk GET请求压力测试
wrk GET请求压力测试
7 0
|
16天前
|
前端开发 应用服务中间件 nginx
使用nginx-http-concat资源请求合并功能 优化网站响应
使用nginx-http-concat资源请求合并功能 优化网站响应
12 0
|
3月前
|
测试技术 Serverless iOS开发
PTS接口问题之执行请求失败如何解决
PTS(Performance Testing Service)是一项面向网站、应用等提供的压力测试服务,用于模拟不同场景下的用户访问,评估系统的性能表现;在进行PTS压测时,可能会出现一些异常或报错,本合集将PTS压测中频繁出现的问题及其解决办法进行汇编,旨在帮助用户更有效地进行性能测试和问题定位。
|
7月前
Cypress 等待某个 HTTP put 请求得到 200 状态码后,再执行下一步的操作代码
Cypress 等待某个 HTTP put 请求得到 200 状态码后,再执行下一步的操作代码
28 0
|
7月前
|
JSON 数据格式
wrk post lua脚本取excel参数压力测试,判断接口性能
wrk post lua脚本取excel参数压力测试,判断接口性能
140 0
|
9月前
|
域名解析 网络协议 网络安全
Curl命令分析接口耗时
Curl是一个非常实用的,用来与服务器之间传输数据的工具,支持的协议包括 (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP),Curl设计为无用户交互下完成工作。
181 0
|
缓存 前端开发 JavaScript
学习AJAX必知必会(3)~自动重启工具nodemon、缓存问题、请求超时和网络异常、取消重复请求
学习AJAX必知必会(3)~自动重启工具nodemon、缓存问题、请求超时和网络异常、取消重复请求
121 0