官方教程文档:http://tsung.readthedocs.io/en/latest/introduction.html
#Tsung命令参数
生成两个文件,默认在/usr/local/bin 目录,现变为在 /usr/local/tsung1.7/bin 目录下
tsung tsung-recorder
#查看参数 tsung -h
Usage: tsung <options> start|stop|debug|status
Options:
-f <file> set configuration file (default is ~/.tsung/tsung.xml)
(use - for standard input)
-l <logdir> set log directory where YYYYMMDD-HHMM dirs are created (default is ~/.tsung/log/)
-i <id> set controller id (default is empty)
-r <command> set remote connector (default is ssh)
-s enable erlang smp on client nodes
-p <max> set maximum erlang processes per vm (default is 250000)
-X <dir> add additional erlang load paths (multiple -X arguments allowed)
-m <file> write monitoring output on this file (default is tsung.log)
(use - for standard output)
-F use long names (FQDN) for erlang nodes
-L <lifetime> SSL session lifetime (600sec by default)
-w <delay> warmup delay (default is 1 sec)
-n disable web GUI (started by default on port 8091)
-k keep web GUI (and controller) alive after the test has finished
-v print version information and exit
-6 use IPv6 for Tsung internal communications
-x <tags> list of requests tag to be excluded from the run (separated by comma)
-h display this help and exit
#xml配置文件在
默认使用配置文件:~/.tsung/tsung.xml
配置文件默认目录:/usr/share/doc/tsung/examples,先改为:/usr/local/tsung1.7/share/doc/tsung/examples,http 测试配置文件 http_distributed.xml
#测试配置文件
tsung -f /opt/tsung/http_distributed_yunke138_live.xml -l /opt/tsung/log/ start
生成的日志文件存放在 ~/.tsung/log 目录下,先改为:/opt/tsung/log,按日期-时间格式生成日志文件 ~/.tsung/log/20180120-1000/tsung.log
生成报表,之后会在http_simple生成图表文件 tsung_stats.pl 在目录下 /usr/local/tsung1.7/lib/tsung/bin
生成的报告文件会默认放在运行tsung_stats.pl时,所在的前目录下。
cd /opt/tsung/log/result
/usr/local/tsung1.7/lib/tsung/bin/tsung_stats.pl --stats /opt/tsung/log/20180124-1218/tsung.log
通过python搭建临时服务器来查看报表结果
/usr/bin/python -m SimpleHTTPServer 9090
http://主机服务器IP:9090/report.html
#配置文件详解
clients
单机压测时,配置产生用户的方式 maxusers 一般为800,最大3000,超过3000,会开启更多的erlang VM。
<clients>
<client host="localhost" use_controller_vm="true" maxusers="30000"/>
</clients>
集群压测时,配置产生用户的方式
<clients>
<client client host="client1" maxusers="12000" cpu="4" weight="1"/>
<client client host="client2" maxusers="12000" cpu="8" weight="2"/>
...
</clients>
tsung运行时,可以由很多虚拟机组成,当前client机器最多可生成的用户数,当 use_controller_vm=ture,用户数>maxusers,tsung会生成新的vm。
#单机
<servers>
<server host="garden.blue.jude.poppen.lab" port="80" type="tcp"></server>
</servers>
#服务集群 将cpu的值设置为与机器的核数
<servers>
<server client host="server1" maxusers="120000" cpu="4" weight="1"></server>
<server client host="server2" maxusers="120000" cpu="8" weight="2"></server>
</servers>
tsung 会根据weight发起请求的server
Monitoring,可以配置成erlang及snmp监控服务
<!--监控方式配置-->
<monitoring>
<!--
<monitor host="tsung-slave1" type="snmp"></monitor>
-->
<monitor host="slave" type="erlang"></monitor>
</monitoring>
load配置访问的负载,访问可以配置成多个段,由phase决定,持续时间:duration 单位:unit
users中maxnumber表明生成的最大用户数,interarrival 每多少秒产生一个新用户
<load>
<arrivalphase phase="1" duration="3" unit="minute">
<users maxnumber="100" interarrival="0.02" unit="second" ></users>
</arrivalphase>
</load>
options 配置请求访问的信息 为全局变量信息
<options>
<!--<option name="thinktime" value="3" random="true" override="false"></option>-->
<option name="tcp_rcv_buffer" value="16384"></option>
<option name="tcp_snd_buffer" value="16384"></option>
<option name="ports_range" min="1025" max="65535"/>
<option type="ts_http" name="user_agent">
<user_agent probability="100">TPSLIVE/1.8.0/15 (vivo,vivo Y51A,21,5.0.2;869917021552802;WIFI)</user_agent>
</option>
<!--<option name="file_server" value="/opt/tsung/live_url.csv"></option>-->
</options>
session 配置session,可配置多个session请求多个IP及配置请求的概率,每个session中的probability和为100,类型为http
session中可以用for来设置请求次数
<for from="1" to="@loop" incr="1" var="counter">
<sessions>
<session name="yunboke001" probability="100" type="ts_http">
<transaction name="login_and_watch_live">
<!-- 登录请求-->
<request>
<dyn_variable name="id" jsonpath="data.id" />
<dyn_variable name="token" jsonpath="data.token" />
<http url="/urlpath1" contents="platform=Android&loginName=xx1&deviceId=xx2&app_version=1.8.0&brand=vivo Y51A&pwdOriginal=xx3&lan_id=2&location_id=2&os_version=5.0.2&model=vivo,vivo Y51A,21,5.0.2" content_type="application/x-www-form-urlencoded;charset=utf-8" method="POST" version="1.1">
</http>
</request>
<!-- 每个用户请求10次 -->
<!-- 查看私密直播请求-->
<for from="1" to="10" incr="1" var="counter">
<!--<request>
<http url="/urlpath2?id=xxx&isPrivate=xxx" content_type="application/x-www-form-urlencoded;charset=utf-8" method="GET" version="1.1">
</http>
</request>-->
<!-- 查看私密直播列表请求-->
<!--<request>
<http url="/urlpath3?page=xxx&limit=6" content_type="application/x-www-form-urlencoded;charset=utf-8" method="GET" version="1.1">
</http>
</request>-->
<setdynvars sourcetype="random_number" start="0" end="2">
<var name="videoType" />
</setdynvars>
<setdynvars sourcetype="random_number" start="1" end="2">
<var name="menuType" />
</setdynvars>
<setdynvars sourcetype="random_number" start="1" end="6">
<var name="page" />
</setdynvars>
<request subst="true">
<http url="/urlpath3?videoType=%%_videoType%%&menuType=%%_menuType%%&token=%%_token%%&userId=%%_id%%&page=%%_page%%&limit=20" content_type="application/x-www-form-urlencoded;charset=utf-8" method="GET" version="1.1"></http>
</request>
<thinktime min="1" max="2" random="true"></thinktime>
</for>
</transaction>
<!--<transaction name="index_request"></transaction>-->
</session>
</sessions>
其中,要使用随机数 request中 subst="true"
<setdynvars sourcetype="random_number" start="0" end="2">
<var name="videoType" />
</setdynvars>
<setdynvars sourcetype="random_number" start="1" end="2">
<var name="menuType" />
</setdynvars>
<setdynvars sourcetype="random_number" start="1" end="6">
<var name="page" />
</setdynvars>
http中可定义header
<http_header name="Authorization" value="111"/>
<http_header name="Cookie" value="authToken=%%_auth_token%%; Path=/"/>
<!-- content-Type:POST请求参数的格式,如果是json格式可以这样写 -->
<http_header name="Content-Type" value="application/json"/>
thinktime设置两个请求的间隔
<thinktime value="1"/>
#ip和端口的问题
1台机器:最多可用端口6万
1个会话:tsung默认发送、接收缓冲区大小16K,建立连接10K,1个用户占用内存按50K算,6万用户,占用3000MB~=3GB。
模拟100万用户:机器数:100万/6万~=17 内存:17*3GB=51GB