我們在做服務器程序的時候,經常要知道一個請求的響應時間,借以優化或者定位問題。 通常的做法是在代碼里面加入日志計算時間,這個方法有問題,時間不準確。因為數據從網卡到應用程序,從應用到網卡的時間沒有被計算在內。 而且這個時間隨著系統的負載有很大的變化。
那同學說,我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地址方式,而不是網絡接口方式搞定。
祝大家玩的開心。
原文轉自:http://blogread.cn/it/article/2980?f=wb