percona之tcprstat 介紹

tcprstat 簡介

tcprstat 是一個免費、開源的 TCP 分析工具,可以用來觀測網絡通信狀態以及計算 request 和 response 之間的延遲。通過它可以獲得應答時間統計信息並將其顯示輸出。輸出格式類似於 Unix 中各種 -stat 工具的模式,如 vmstat、iostat、mpstat 。該工具可以選擇觀測指定 port 上的通信負載,這種方式使其在針對單實例 daemon 進程 request-response 時間測量上具有實際意義,例如針對 mysqld、httpd、memcached 等。

tcprstat 的優點:

  • 輕量級。無需寫和分析大塊日誌文件。
  • request 和 response 之間時間間隔的精度爲微秒。
  • 輸出結果易於導入到 spreadsheet 中,易於通過命令行腳本進行處理,易於通過 gnuplot 進行繪圖。
  • 協議無關性。可被用於各種具有 request-response 模式的 client-server 協議模型。

tcprstat 是與 tcpstat 相近的工具,但是其專注於 response 時間的測量,而不是網絡通信負載的量和包大小。所以它是更加符合目標驅動性能優化(Goal-Driven Performance Optimization)的技術。

安裝 tcprstat

shell> unzip tcprstat.zip 
shell> cd tcprstat
shell> chmod +x bootstrap
shell> ./bootstrap    #安裝需要依賴於automake 、autoconf
shell> ./configure    #安裝需要依賴於bison、flex
shell> make && make install

tcprstat命令行選項
shell> ./tcprstat --help     #運行 tcpstat 需要 root 權限。
tcprstat 0.3.1, libpcap version 1.1.1.
Usage: tcprstat [--port <port>] [--format=<format>] [--interval=<sec>]
             [--header[=<header>] | --no-header] [--iterations=<it>]
             [--read=<file>]
       tcprstat --version | --help

    --read <file>, -r    Capture from pcap file <file>, not live.
    --local <addresses>, -l
                         <addresses> is a comma-separated list of ip
                         addresses, which are used as the local list of
                         addresses instead of pcap getting the list.
                         This is useful when working with a pcap file got
                         from another host with different addresses.
    --port <port>, -p    Capture traffic only for tcp/<port>.
    --format <format>, -f
                         Output format. Argument is a string detailing
                         how the information is presented. Accepted codes:
                             %n - Response time count
                             %a - Response time media in microseconds
                             %s - Response time sum
                             %x - Response time squares sum
                             %m - Minimum value
                             %M - Maximum value
                             %h - Median value
                             %S - Standard deviation
                             %v - Variance (square stddev)
                             %I - Iteration number
                             %t - Timestamp since iteration zero
                             %T - Unix timestamp
                             %% - A literal %
                         Default is:
    "%T\t%n\t%M\t%m\t%a\t%h\t%S\t%95M\t%95a\t%95S\t%99M\t%99a\t%99S\n".
                         Statistics may contain a percentile between
                         the percentage sign and the code: %99n, %95a.
    --header[=<header>], --no-header
                         Whether to output a header. If not supplied, a
                         header is created out of the format. By default,
                         the header is shown.
    --interval <seconds>, -t
                         Output interval. Default is 10.  設置測量的時間間隔,以秒爲單位。
    --iterations <n>, -n
                         Output iterations. Default is 1, 0 is infinity  迭代執行的次數;0 意味着無限循環。

    --help               Shows program information and usage.
    --version            Shows version information.

tcprstat 用法

下面是一個 tcprstat 輸出樣例,內容顯示的是 MySQL 服務器的 3306 端口上的網絡通信情況。

# 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

內容以每次一行、一行一秒的形式輸出,輸出持續了 5 秒時間。每一行都包含了時間戳,且包含了與 query 對應的 response 的時間。列輸出中包含了標準的 response 時間值,以微秒爲單位。同時還給出了各種 95% 和 99% 應答時間的值。 response 時間值的計算是通過測量最後收到包和隨後第一個發出包之間的花費時間來計算的。僅包含 TCP 控制信息的特定類型的包會被忽略。

tcprstat 的輸出內容可以被改變:包含或者省略其所測量得到的網絡通信各的種類型統計信息。默認輸出的列包含了一些在大多場景下有用的項。輸出可以通過格式代碼 %C 來進行控制,其中 C 是單個字符。下表中給出了各種格式代碼和其對應的輸出含義。

Format Code Header Default? Meaning
%n count y 在迭代期間完成的 request 個數
%a avg y 平均 response 時間
%s sum y response 時間的總和
%x sqs   response 時間的平方和
%m min y 最小 response time
%M max y 最大 response time
%h med y 中間 response time
%S stddev y response 時間值標準差
%v var   response 時間的總體方差
%I iter#   迭代次數
%t elapsed   從第一次迭代開始流逝的秒數
%T timestamp y Unix 時間戳
%%     % 字符
\t     tab 字符
\n     換行字符
95,99 Adds a prefix y 百分比指示符

你能夠通過自定義控制選項 -f 來改變 tcpstat 的輸出格式。例如,爲了輸出每秒向 MySQL 服務器 3306 端口發送的 request 數,執行下面的命令:

shell> tcprstat -f '%n\n' -p 3306 -t 1 -n 0
count
2212
2070
...

你同樣可以使用百分比指示符,以顯示佔 response 時間總體值 N% 的統計值。該工具當前默認僅支持 95% 和 99% 兩種輸出。如果想要輸出任意百分比值的統計信息,則需要在 % 字符和格式代碼之間插入指定的百分比值。默認的列輸出中將會包含相應的百分比頭部信息。下面的列子中以微秒爲單位,輸出了最大、95%、99% response 時間的值:

shell> tcprstat -f '%M\t%95M\t%99M\n' -p 3306 -t 1 -n 0
max    95_max    99_max
31221    411    3001
52721    495    2828
12173    507    1513
...

tcpstat 不僅能夠對實時網絡通信進行分析,還能夠對通過 tcpdump 創建的網絡通信抓包文件進行分析。這也就使得在微秒可以在某臺機器上進行網絡通信轉包,而在另外的機器上在任意的時間點對其進行分析。爲了將網絡包存放下來以備日後分析,需要在執行 tcpdump 時指定 -w 參數來確定保存文件名。之後,tcprstat 可以使用 -r 選項從上述文件中讀取並分析網絡通信內容。

tcprstat 通常情況下,能夠根據本地網卡接口上綁定的一系列ip地址,確定收到的 request 和發出的 response 之間的配對關係。但是,從文件中獲取信息進行分析的時候可能會無法正確處理,因爲該文件是從一個不同的 host 上獲取到的。在這種情況下,你可以使用 -l 選項自定義指定一組 ip 地址用於分析文件內容。

參考:
http://www.percona.com/docs/wiki/tcprstat:start
http://my.oschina.net/moooofly/blog/157063

整理自網絡

Svoid
2014-12-30
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章