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 CodeHeaderDefault?Meaning
%ncounty在迭代期间完成的 request 个数
%aavgy平均 response 时间
%ssumyresponse 时间的总和
%xsqs response 时间的平方和
%mminy最小 response time
%Mmaxy最大 response time
%hmedy中间 response time
%Sstddevyresponse 时间值标准差
%vvar response 时间的总体方差
%Iiter# 迭代次数
%telapsed 从第一次迭代开始流逝的秒数
%TtimestampyUnix 时间戳
%%  % 字符
\t  tab 字符
\n  换行字符
95,99Adds a prefixy百分比指示符

你能够通过自定义控制选项 -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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值