技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 系统运维 --> 调查服务器响应时间的利器 tcprstat

调查服务器响应时间的利器 tcprstat

浏览:1439次  出处信息

我们在做服务器程序的时候,经常要知道一个请求的响应时间,借以优化或者定位问题。 通常的做法是在代码里面加入日志计算时间,这个方法有问题,时间不准确。因为数据从网卡到应用程序,从应用到网卡的时间没有被计算在内。 而且这个时间随着系统的负载有很大的变化。
那同学说,我wireshark, tcpdump抓包人肉统计不行吗。 可以的,只不过我会很同情你,此举需要耐心且不具可持续性。 所以我们希望有个工具能够最少费力的做这个事情。

这时候来自percona的tcprstat来救助了! 这个工具原本开发用来调查mysqld的性能问题,所以不要奇怪它的默认端口是3306, 但是我们可以用这个工具来调查典型的request->response类型的服务器。

什么是tcprstat:

tcprstat is a free, open-source TCP analysis tool that watches network traffic and computes the delay between requests and responses. From this it derives response-time statistics and prints them out. The output is similar to other Unix -stat tools such as vmstat, iostat, and mpstat. The tool can optionally watch traffic to only a specified port, which makes it practical for timing requests and responses to a single daemon process such as mysqld, httpd, memcached, or any of a variety of other server processes.

文档很详细: 请参考: http://www.percona.com/docs/wiki/tcprstat:start

不愿意编译的同学直接从这里下载64位系统的编译好的二进制: http://github.com/downloads/Lowercases/tcprstat/tcprstat-static.v0.3.1.x86_64

源码编译也挺容易的: 由于它自带libpcap包, 这个包有可能在configure的时候没认识好netlink, 只要把config.h里面的netlink那个define注释掉就好。

编译好了, 典型使用很简单:

# tcprstat -p 3306 -t 1 -n 5
timestamp	count	max	min	avg	med	stddev	95_max	95_avg	95_std	99_max	99_avg	99_std
1283261499	1870	559009	39	883	153	13306	1267	201	150	6792	323	685
1283261500	1865	25704	29	578	142	2755	889	175	107	23630	333	1331
1283261501	1887	26908	33	583	148	2761	714	176	94	23391	339	1340
1283261502	2015	304965	35	624	151	7204	564	171	79	8615	237	507
1283261503	1650	289087	35	462	146	7133	834	184	120	3565	244	358

但是这个tcprstat在bonding的网卡下有点问题:

# /sbin/ifconfig
bond0     Link encap:Ethernet  HWaddr A4:BA:DB:28:B5:AB
          inet addr:10.232.31.19  Bcast:10.232.31.255  Mask:255.255.255.0
          inet6 addr: fe80::a6ba:dbff:fe28:b5ab/64 Scope:Link
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:19451951688 errors:0 dropped:4512 overruns:0 frame:0
          TX packets:26522074966 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:6634368171533 (6.0 TiB)  TX bytes:32576206882863 (29.6 TiB)
...
# tcprstat -p 3306 -t 1 -n 5
pcap: SIOCGIFFLAGS: bonding_masters: No such device

解决方案是:

以下是代码片段:
# sudo tcprstat -p 3306 -t 1 -n 0 -l `/sbin/ifconfig | grep ’addr:[^ ]\+’ -o | cut -f 2 -d : | xargs echo | sed -e ’s/ /,/g’`

用IP地址方式,而不是网络接口方式搞定。

祝大家玩的开心。

QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1