Tsung.xml官方说明文档

分类:测试 |

Tsung.xml(HTTP)

配置文件模板路径:/usr/share/doc/tsung/examples/http_simple_xml

<?xml version="1.0"?><!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd"><tsung loglevel="notice" version="1.0">

  <!-- Client side setup -->
  <clients>
    <client host="localhost" use_controller_vm="true"/>
  </clients>

  <!-- Server side setup --><servers>
  <server host="myserver" port="80" type="tcp"></server></servers>

  <!-- to start os monitoring (cpu, network, memory). Use an erlang
  agent on the remote machine or SNMP. erlang is the default --> 
  <monitoring>
    <monitor host="myserver" type="snmp"></monitor>
  </monitoring>

  <load>
  <!-- several arrival phases can be set: for each phase, you can set
  the mean inter-arrival time between new clients and the phase
  duration -->
   <arrivalphase phase="1" duration="10" unit="minute">
     <users interarrival="2" unit="second"></users>
   </arrivalphase>
  </load>

  <options>
   <option type="ts_http" name="user_agent">
    <user_agent probability="80">Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050513 Galeon/1.3.21</user_agent>
    <user_agent probability="20">Mozilla/5.0 (Windows; U; Windows NT 5.2; fr-FR; rv:1.7.8) Gecko/20050511 Firefox/1.0.4</user_agent>
   </option>
  </options>

  <!-- start a session for a http user. the probability is the
  frequency of this type os session. The sum of all session's
  probabilities must be 100 -->

 <sessions>
  <session name="http-example" probability="100" type="ts_http">

    <!-- full url with server name, this overrides the "server" config value -->

    <request> <http url="/" method="GET" version="1.1"></http> </request>
    <request> <http url="/images/accueil1.gif" method="GET" version="1.1" if_modified_since="Fri, 14 Nov 2003 02:43:31 GMT"></http> </request>
    <request> <http url="/images/accueil2.gif" method="GET" version="1.1" if_modified_since="Fri, 14 Nov 2003 02:43:31 GMT"></http> </request>
    <request> <http url="/images/accueil3.gif" method="GET" version="1.1" if_modified_since="Fri, 14 Nov 2003 02:43:31 GMT"></http> </request>

    <thinktime value="20" random="true"></thinktime>

    <request> <http url="/index.en.html" method="GET" version="1.1" ></http> </request>

  </session>
 </sessions></tsung>

文件结构客户端和服务器配置基本配置

非分布式负载

<!-- 客户端 --><clients>
    <client host="localhost" use_controller_vm="true"/></clients><!-- 服务器 --><servers>
    <server host="192.168.1.1" port="80" type="tcp"></server></servers>

添加服务器设置

<servers>
    <server host="server1" port="80" type="tcp" weight="4"></server>
    <server host="server2" port="80" type="tcp" weight="1"></server></servers>
  • weight:每个服务器会有个权重值,根据权重值分配会话,可以使整形和浮点型参数

  • type:tcp, ssl, udp, websocket(适用1.5.0及更高版本) 
    (IPv6, 使用 tcp6, ssl6 or udp6 1.4.2 及更高版本)

高级配置

分布式测试,负载均衡测试,分配不同的流量

<clients>
    <client host="louxor" weight="1" maxusers="800">
        <ip value="10.9.195.12"></ip>
        <ip value="10.9.195.13"></ip>
    </client>
    <client host="memphis" weight="3" maxusers="600" cpu="2"/>
    </clients><servers>
    <server host="10.9.195.1" port="8080" type="tcp"></server></servers>

maxusers:该参数用来设置单个线程中创建socket连接的数目

Monitoring

Tsung通过和远程代理进行通信来对服务器进行监控。 
相关统计数据包括:CPU活动,平均负载和内存使用情况。 
默认代理为erlang

<monitor batch="true" host="torque" type="erlang"></monitor>

Erlang

远程代理由Tsung启动,通过Erlang通信检索服务器上活动的统计数据。 
例如,下面配置为六台主机集群配置基于Erlang代理监控集群定义。

<monitoring>
    <monitor host="geronimo" type="erlang"></monitor>
    <monitor host="bigfoot-1" type="erlang"></monitor>
    <monitor host="bigfoot-2" type="erlang"></monitor>
    <monitor host="f14-1" type="erlang"></monitor>
    <monitor host="f14-2" type="erlang"></monitor>
    <monitor host="db" type="erlang"></monitor></monitoring>

Erlang对MySQL数据库的监控:

<monitor host="db" type="erlang"></monitor>
    <mysqladmin port="3306" username="root" password="sesame" /></monitor>

SNMP

默认版本为:v1,端口为:161

<monitoring>
    <monitor host="geronimo" type="snmp"/>
    <monitor host="f14-2" type="erlang"></monitor>
    <monitor host="db" type="snmp">
        <snmp version="v2" community="mycommunity" port="11161"/>
    </monitor></monitoring>

1.4.2版本开始自定义OID 
type : 可选为simple,counter,或者sum,或自定义函数来设置 
type can be sample, counter or sum, and optionally you can define a function (with erlang syntax) to be applied 
to the value (eval attribute).

<monitor host="127.0.0.1" type="snmp">
    <snmp version="v2">
        <oid value="1.3.6.1.4.1.42.2.145.3.163.1.1.2.11.0"name="heapused" type="sample" eval="fun(X)-> X/100 end."/>
    </snmp></monitor>

Munin

从1.3.1开始,Tsung可以通过Munin代理检索数据

<monitoring>
    <monitor host="geronimo" type="munin"/>
    <monitor host="f14-2" type="erlang"></monitor></monitoring>

定义负载进程随机产生用户

<load>
    <arrivalphase phase="1" duration="10" unit="minute">
        <users interarrival="2" unit="second"></users>
    </arrivalphase>
    <arrivalphase phase="2" duration="10" unit="minute">
        <users interarrival="1" unit="second"></users>
    </arrivalphase>
    <arrivalphase phase="3" duration="10" unit="minute">
        <users interarrival="0.1" unit="second"></users>
    </arrivalphase></load>

代码解释 
- 第一阶段:10分钟,每隔2秒创建一个用户 
- 第二阶段:10分钟,每隔1秒创建一个用户 
- 第三阶段:10分钟,每隔0.1秒创建一个用户 
另一种设置方法:10分钟,每秒产生10个用户

<arrivalphase phase="1" duration="10" unit="minute">
    <users arrivalrate="10" unit="second"></users></arrivalphase>

或者通过maxnumber属性设置每个阶段最大用户数

<arrivalphase phase="1" duration="10" unit="minute">
    <users maxnumber="100" arrivalrate="10" unit="second"></users></arrivalphase><arrivalphase phase="2" duration="10" unit="minute">
    <users maxnumber="200" arrivalrate="10" unit="second"></users></arrivalphase>
  • 第一阶段:10分钟最大产生100用户

  • 第二阶段:10分钟最大产生200用户 
    从1.2.1开始,loop:可以通过该属性设置,进行循环 
    在1.5.1中,可以通过session_setup标签,为指定阶段设置会话信息

<arrivalphase phase="3" duration="1" unit="minute">
    <session_setup name="http_test_1" probability="80"/>
    <session_setup name="fake" probability="20"/>
    <users interarrival="1" unit="second"/></arrivalphase>

静态生成用户

<load>
    <arrivalphase phase="1" duration="10" unit="minute">
        <users interarrival="2" unit="second"></users>
    </arrivalphase>
    <user session="http-example" start_time="185" unit="second"></user>
    <user session="http-example" start_time="10" unit="minute"></user>
    <user session="foo" start_time="11" unit="minute"></user></load><sessions>
    <session name="http-example" probability="0" type="ts_http">
        <request> <http url="/" method="GET"></http> </request>
    </session>
    <session name="foobar" probability="0" type="ts_http">
        <request> <http url="/bar" method="GET"></http> </request>
    </session>
    <session name="foo" probability="100" type="ts_http">
        <request> <http url="/" method="GET"></http> </request>
    </session><sessions>

上述设置中:包含两个会话信息,probability为0的会话将不会被执行,定义了三个用户,分别在测试开始185秒的http-example会话,一个开始为10分钟后,另一个在11分钟后的foo会话。 
在1.5.1版本中:可以同时开始多个会话信息,通过制定相同的前缀名称。

<user session="foo*" start_time="10" unit="second"/>

负载测试时间

Tsung运行时间取决于测试阶段的总时长,设置测试时间停止Tsung

<load duration="1" unit="hour">
    <arrivalphase phase="1" duration="10" unit="minute">
        <users interarrival="2" unit="second"></users>
    </arrivalphase></load>

duration : 最大值 < 50days,单位可取:seconds, minute,hour.

设置属性Thinktimes, SSL, Buffers

默认设置为全局属性,

<option name="thinktime" value="3" random="false" override="true"/>
<option name="ssl_ciphers"value="EXP1024-RC4-SHA,EDH-RSA-DES-CBC3-SHA"/>
<option name="tcp_snd_buffer" value="16384"></option>
<option name="tcp_rcv_buffer" value="16384"></option>
<option name="udp_snd_buffer" value="16384"></option>
<option name="udp_rcv_buffer" value="16384"></option>

在1.6.0中,可禁用SSL 会话缓存(默认开启)

<option name="ssl_reuse_sessions" value="false"/>

TCP连接超时

1.6.0新属性 
指定尽力TCP连接超时毫秒值,缺省值为无限制

<option name="connect_timeout" value="5000" />

或者通过set_option标签为每一个会话连接设置超时时间

<set_option name="connect_timeout" value="1000" />

开启TCP地址重复绑定

<option name=”tcp_reuseaddr” value=”true” />

重连次数与超时

1.6.0新属性 
默认重连次数为3,设置为0时,代表禁止重连

<option name="max_retries" value="5" />

重连超时:默认为10毫秒

<set_option name="retry_timeout" value="1000" />

超时消息确认

设置idle超时,默认10min和全局ack超时,默认无穷

<option name="idle_timeout" value="300000"></option>
<option name="global_ack_timeout" value="6000000"></option>

Hibernate

1.3.1 
在thinktimes阶段中模拟用户降低内存消耗,默认值为10s 

Rate_limit

1.4.1 
现在每个客户端(令牌桶算法)的带宽需求,单位为KB/s,默认值为1024。

<option name="rate_limit" value="1024"></option>

Ports_range

当要在同一客户端下产生30000个连接,必须限制TCP端口号

<option name="ports_range" min="1025" max="65535"/>

设置随机数种子

<option name="seed" value="42"/>

设置BOSH路径

<option name="bosh_path" value="/http-bind/"/>

Websocket选项

使用Websocket服务器模式,设置传输数据格式

<option name="websocket_path" value="/chat"/><!-- send websocket data with text frame, default binary--><option name="websocket_frame" value="text"/>

XMPP/Jabber选项

默认设置

<option type="ts_jabber" name="global_number" value="5" />
<option type="ts_jabber" name="userid_max" value="100" />
<option type="ts_jabber" name="domain" value="jabber.org" />
<option type="ts_jabber" name="username" value="myuser" />
<option type="ts_jabber" name="passwd" value="mypasswd" />
<option type="ts_jabber" name="muc_service" value="conference.localhost"/>

HTTP选项

设置用户代理

<option type="ts_http" name="user_agent"><user_agent probability="80">Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050513 Galeon/1.3.21</user_agent><user_agent probability="20">Mozilla/5.0 (Windows; U; Windows NT 5.2; fr-FR; rv:1.7.8) Gecko/20050511 Firefox/1.0.4</user_agent></option>

AMQP选项

<option type="ts_amqp" name="heartbeat" value="30" />

会话

会话主要定义脚本内容,描述执行的请求 
每个会话有probability属性,用于判断新创建的用户执行哪一个会话,所有会话的probability总值为100。从1.5.0开始,可以通过weight代替probability属性使用。

<session name="s1" weight="2" type="ts_http">
<session name="s2" weight="1" type="ts_http">

http

<sessions>
    <session name="http-example" probability="70" type="ts_http">
        <request> <http url="/" method="GET" version="1.1">
                    </http> </request>
        <request> <http url="/images/logo.gif"method="GET" version="1.1"if_modified_since="Fri, 14 Nov 2003 02:43:31 GMT">
                    </http></request>
        <thinktime value="20" random="true"></thinktime>
        <transaction name="index_request">
        <request><http url="/index.en.html"method="GET" version="1.1" >
                    </http> </request>
        <request><http url="/images/header.gif"method="GET" version="1.1">
                    </http> </request>
        </transaction>
        <thinktime value="60" random="true"></thinktime>
        <request>
            <http url="/" method="POST" version="1.1"contents="bla=blu">
                </http> </request>
        <request>
            <http url="/bla" method="POST" version="1.1"contents="bla=blu&amp;name=glop">
            <www_authenticate userid="Aladdin"passwd="open sesame"/></http>
        </request>
    </session>
    <session name="backoffice" probability="30" ...>
        ...    </session></sessions>

HTTP 报头

<request>
    <http url="/bla" method="POST" contents="bla=blu&amp;name=glop">
        <www_authenticate userid="Aladdin" passwd="open sesame"/>
        <http_header name="Cache-Control" value="no-cache"/>
        <http_header name="Referer" value="http://www.w3.org/"/>
    </http></request>

从外部文件读取报文内容

<http url="mypage" method="POST" contents_from_file="/tmp/myfile" />

Cookie,cookie值非持久化的,所以必须在所有的请求中添加

<http url="/"><add_cookie key="foo" value="bar"/><add_cookie key="id" value="123"/></http>

认证: 
使用数字证书

<!-- 1. First request return 401. We use dynvars to fetch nonce and realm --><request>
    <dyn_variable name="nonce" header="www-authenticate/nonce"/>
    <dyn_variable name="realm" header="www-authenticate/realm"/>
    <http url="/digest" method="GET" version="1.1"/></request><!--
2. This request will be authenticated. Type="digest" is important.
We use the nonce and realm values returned from the previous
If the webserver returns the nextnonce we set it to the nonce dynvar
for use with the next request.
Else it stays set to the old value
--><request subst="true">
    <dyn_variable name="nonce" header="authentication-info/nextnonce"/>
    <http url="/digest" method="GET" version="1.1">
    <www_authenticate userid="user" passwd="passwd" type="digest" realm="%%_realm%%" nonce="%%_nonce%%"/>
    </http></request>

OAuth认证

<!-- Getting a Request Token --><request><dyn_variable name="access_token" re="oauth_token=([^&amp;]*)"/><dyn_variable name="access_token_secret" re="oauth_token_secret=([^&amp;]*)" /><http url="/oauth/example/request_token.php" method="POST" version="1.1" contents="empty"><oauth consumer_key="key" consumer_secret="secret" method="HMAC-SHA1"/></http></request><!-- Getting an Access Token --><request subst=’true’><dyn_variable name="access_token" re="oauth_token=([^&amp;]*)"/><dyn_variable name="access_token_secret" re="oauth_token_secret=([^&amp;]*)"/><http url="/oauth/example/access_token.php" method="POST" version="1.1" contents="empty"><oauth consumer_key="key" consumer_secret="secret" method="HMAC-SHA1" access_token="%%_access_</http>
</request>
<!-- Making Authenticated Calls -->
<request subst="true"><http url="/oauth/example/echo_api.php" method="GET" version="1.1"><oauth consumer_key="key" consumer_secret="secret" access_token="%%_access_token%%" access_token_</http></request>

高级功能Dynamic substitutionsReading external file动态变量检查服务器响应Loops,If,Foreach

1.3.0开始,可在会话中添加有条件或无条件的循环。 
1.4.0开始,可以通过增强for循环遍历动态变量列表 
for

<for from="1" to="10" incr="1" var="counter">    ...
    <request> <http url="/page?id=%%_counter%%"></http></request>    ...</for>

repeat

<repeat name="myloop" max_repeat="40">    ...
    <request>
        <dyn_variable name="result" re="Result: (.*)"/>
        <http url="/random" method="GET" version="1.1"></http>
    </request>...<until var

if(版本1.3.1以后) 
其他操作符:gt,gte,lt,lte

<if var="tsung_userid" eq="3">
    <request> <http url="/foo"/> </request>
    <request> <http url="/bar"/> </request></if>

foreach

<foreach name="element" in="list">
    <request subst="true">
        <http url="%%_element%%" method="GET" version="1.1"/>
    </request></foreach>

通过include,exclude标签,可以进行元素的包含和排除

<foreach name="element" in="list" include="^/.*$">
<foreach name="element" in="list" exclude="http:\/\/.*\.tld\.com\/.*$">

检索图片

<request subst="true">
    <dyn_variable name="img_list" xpath="//img/@src"/>
    <http url="/mypage.html" method="GET" version="1.1"/></request><foreach name="img" in="img_list">
    <request subst="true">
        <http url="%%_img%%" method="GET" version="1.1"/>
    </request></foreach>

速率限制

1.4.0 目前只能限制流入速率

<session name="http-example" probability="70" type="ts_http">
    <set_option name="rate_limit" value="64" />    ...</session>

排除请求

在启动测试前,通过-x 忽略指定名字的请求 
tsung -f SCENARIO.xml -x image start

<request>
    <http url="/" method="GET"></http></request><request tag="image">
    <http url="/foo.png" method="GET"></http></request>

客户端证书

1.5.1 
可以使用客户端证书进行SSL认证,通过动态变量为证书设置参数

<session name="https-with-cert" probability="70" type="ts_http">
    <set_option name="certificate">
    <certificate cacertfile="/etc/ssl/ca.pem"
        keyfile="%%_keyfile%%" keypass="%%_keypass%%"  certfile="/home/nobody/.tsung/client.pem"/>
    </set_option></session>