教你获取HTTP接口服务的各个时间

8/5/2020 实用技术

本文主要介绍用curl命令、python库获取HTTP接口请求的各个时间。

# 前言

当我们访问一个网站或者网站接口服务,想知道"时间都去哪里了",这时候我们借助工具来分析。

可以将时间大致分为两部分:一部分是从我们请求到网站服务端所经历的耗时,另一部分是服务端自身处理该服务完毕后响应回来的时间,这些都是可以作为后面结果分析的判断依据。

下面将介绍两个方案,仅供参考。

# 第一种方案

使用系统curl命令模拟网站服务请求,得到各个时间段的时间。

需要注意的是:请确保curl 是最新版本,否则一些参数选项无法使用。

例子:

$ curl -w "Result: \n dnslookup: %{time_namelookup} \n connect: %{time_connect} \n appconnect: %{time_appconnect} \n pretransfer: %{time_pretransfer} \n starttransfer: %{time_starttransfer} \n total: %{time_total} \n ----\n time_redirect: %{time_redirect} \n ----\n size: %{size_download}\n" "https://www.baidu.com"
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号  <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
Result: 
 dnslookup: 0.005 
 connect: 0.009 
 appconnect: 0.128 
 pretransfer: 0.128 
 starttransfer: 0.134 
 total: 0.134 
 ----
 time_redirect: 0.000 
 ----
 size: 2443
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

参数:

  • time_namelookup:DNS解析时间,从请求开始到DNS解析完毕所用时间。
  • time_connect:建立到服务器的 TCP 连接所用的时间
  • time_appconnect:从起始到应用侧(SSL)连接/握手完成的耗时。(在7.19.0 版加入)
  • time_pretransfer:从开始至准备开始传输数据的时间
  • time_starttransfer:在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
  • time_total:全部操作耗费的时间,单位为秒。精确到毫秒。
  • size_download:下载的总字节数。
  • speed_download:下载速度,单位-字节每秒。

从上面的例子可以看到

1.DNS解析耗时:0.005秒

2.TCP建立连接的耗时:(0.009-0.005)= 0.004秒

3.SSL握手完成耗时:(0.128-0.009)= 0.119秒

4.server处理数据的时间:(0.134-0.128)= 0.006秒

5.总体的耗时:0.134秒

6.整个过程没有redirect,所以redirect的耗时为0

上面还有个小技巧:

$ cat ~/.curlrc 
-w "Result: \n dnslookup: %{time_namelookup} \n connect: %{time_connect} \n appconnect: %{time_appconnect} \n pretransfer: %{time_pretransfer} \n starttransfer: %{time_starttransfer} \n total: %{time_total} \n ----\n time_redirect: %{time_redirect} \n ----\n size: %{size_download}\n"

$ curl "https://www.baidu.com"
1
2
3
4

我们把-w参数的值写到curl配置文件~/.curlrc,这样我们按照之前的curl命令访问具体网址就可以得到各个时间。

# 第二种方案

当你拥有Python的环境时,可以借助一个库,可以方便清晰的了解每个阶段的耗时,这个库就是httpstat

注意:该工具还是依赖底层curl命令,所以确保curl是最新版本。

这里我以Python3环境为例,介绍下如何使用。

  • 安装库
$ pip3 install httpstat
1
  • 检查httpstat命令是否存在
$ which httpstat
~/3rd/Python-3.7.4/bin/httpstat
1
2
  • 访问网站
$ httpstat "https://www.baidu.com"
Connected to 163.177.151.110:443 from 10.10.10.10:41350

HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 2443
Content-Type: text/html
Date: Mon, 25 May 2020 03:14:53 GMT
Etag: "58860402-98b"
Last-Modified: Mon, 23 Jan 2017 13:24:18 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

Body stored in: /tmp/tmp35tdqpkp

  DNS Lookup   TCP Connection   TLS Handshake   Server Processing   Content Transfer
[    41ms    |       5ms      |     45ms      |        6ms        |        1ms       ]
             |                |               |                   |                  |
    namelookup:41ms           |               |                   |                  |
                        connect:46ms          |                   |                  |
                                    pretransfer:91ms              |                  |
                                                      starttransfer:97ms             |
                                                                                 total:98ms   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

是不是很直观? so easy。

# 总结

  • 如果你不想用python,那么第一种方案最直接高效。
  • 如果你想用python,那么第二种方案显示效果最佳。

是不是很简单?相信你学会了。


欢迎关注我的公众号testerzhang,原创技术文章第一时间推送。

公众号二维码

Last Updated: 1/1/2022, 11:13:04 PM