压力测试工具Tsung安装和使用

时间:2021-09-08 01:48:07

General

Tsung is an open-source multi-protocol distributed load testing tool, It can be used to stress HTTP, WebDAV, SOAP, PostgreSQL, MySQL, LDAP and Jabber/XMPP servers. Tsung is a free software released under the GPLv2 license.

The purpose of Tsung is to simulate users in order to test the scalability and performance of IP based client/server applications. You can use it to do load and stress testing of your servers. Many protocols have been implemented and tested, and it can be easily extended.

It can be distributed on several client machines and is able to simulate hundreds of thousands of virtual users concurrently (or even millions if you have enough hardware ...).

Tsung is developed in Erlang, an open-source language made by Ericsson for building robust fault-tolerant distributed applications.

Tsung Home: http://tsung.erlang-projects.org/


Installation

Install tsung in CentOS, before installation, please make sure below tools are installed:

  • yum install gcc -y
  • yum install perl -y
  • yum install unixODBC
  • yum install unixODBC-devel

1. Install erlang


















































































































download erlang from http://www.erlang.org/download.html, and select R14B04 version
# wget http://www.erlang.org/download/otp_src_R14B04.tar.gz
# tar -zxvf otp_src_R14B04.tar.gz
# cd otp_src_R14B04
# ./configure --prefix=/usr/local/erlang
# make
# make install

If raise "configure: error: No curses library functions found" in configure step, please install ncurses-devel and re-try configure:

# yum install -y ncurses-devel


2. Install Tsung

download tsung from http://tsung.erlang-projects.org/dist/.

# wget http://tsung.erlang-projects.org/dist/tsung-1.4.2.tar.gz
# tar -zxvf tsung-1.4.2.tar.gz
# cd tsung-1.4.2
# ./configure --prefix=/usr/local/tsung --with-erlang=/usr/local/erlang
# make
# make install


3. Install perl Template

Template used for Tsung reports generated. Template-Toolkit downlaod link:http://www.template-toolkit.org/download/index.html

# wget http://cpan.org/modules/by-module/Template/Template-Toolkit-2.24.tar.gz
# tar -zxvf Template-Toolkit-2.24.tar.gz
# cd Template-Toolkit-2.24
# perl Makefile.PL
# make
# make test
# make install


4. Install gnuplot

gnuplot is the chat generator.

# yum install -y gnuplot gd libpng zlib

Note: use "gnuplot" command to check the tools installed like:

# gnuplot> set terminal


Tsung usage

1 Create Tsung workdir:

$ mkdir ~/.tsung

2. Configure file, use HTTP test as a sample:

$ cp /usr/local/tsung/share/doc/tsung/examples/http_simple.xml ~/.tsung/tsung.xml

Modify the tsung.xml, more usage you can get from: http://tsung.erlang-projects.org/user_manual.html#htoc1

3. Run Tsung for your test

$ tsung start

4. Test report After the tsung test finished, go to the folder which naming as create time and run the perl script:

$ cd ~/.tsung/log/xxxxx
$ /usr/local/tsung/lib/tsung/bin/tsung_stats.pl

5. Display the test results from Firefox or IE If the server installed python, you can start a http server like:

$ cd ~/.tsung/log/xxxxx
$ python -m SimpleHTTPServer

Then you can get reports from: http://serverip:8000


Practice


tsung-recorder

Tsung use tsung-recorder to record your test actions and auto generate the

  1. To start it, run tsung-recorder -p <PLUGIN> start, where PLUGIN can be http, webdav or pgsql for PostgreSQL. The default plugin is http.
  2. The proxy is listening to port 8090. You can change the port with -L portnumber.
  3. To stop it, use tsung-recorder stop.
  4. The recorded session is created as ~/.tsung/tsung_recorderYYYMMDD-HH:MM.xml; if it doesn’t work, take a look at ~/.tsung/log/tsung.log-tsung_recorder@hostname
  5. During the recording, you can add custom tag in the XML file, this can be useful to set transactions or comments: tsung-recorder record_tag "<transaction name=’login’>’’
  6. Once a session has been created, you can insert it in your main configuration file, either by editing by hand the file, or by using an ENTITY declaration, like:
<!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd" [
<!ENTITY mysession1 SYSTEM "/home/nniclausse/.tsung/tsung_recorder20051217-13:11.xml">
]>
...
<sessions>
&mysession1;
</sessions>


Notes:

  • 将本机测试环境代理服务器IP设置为Tsung机的IP(10.192.110.6),并将端口改为8090
  • 在tsung机上运行 tsung-recorder start 开始录制
  • 在本机测试环境中网页里操作需要录制的步骤
  • 在tsung机上运行 tsung-recorder stop 结束录制


One Tsung cases sample

1. Server and Client configure in tsung.xml file like:

<!-- Client side setup -->
<clients>
<client host="server1" use_controller_vm="true" maxusers="10000" />
</clients>
 
<!-- Server side setup -->
<servers>
<server host="172.11.10.1" port="80" type="tcp"></server>
</servers>

2. Phases setting in tsung.xml file like:

<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="1" unit="minute">
<users maxnumber="1" interarrival="1" unit="second"></users>
</arrivalphase>
</load>

3. Add session cases file in tsung.xml file like:

<!DOCTYPE tsung SYSTEM "/usr/local/tsung/share/tsung/tsung-1.0.dtd" [
<!ENTITY mysession1 SYSTEM "/root/.tsung/cases_list/tsung_recorder20120828-1339.xml">
]>
...
<sessions>
&mysession1;
</sessions>

Make sure /usr/local/tsung/share/tsung/tsung-1.0.dtd existed.

Sample of /root/.tsung/cases_list/tsung_recorder20120828-1339.xml:

<session name='rec20120828-1339' probability='100'  type='ts_http'>
 
<!--Mock encoded boxid-->
<setdynvars sourcetype="random_string" length="20">
<var name="useridend" />
</setdynvars>
 
<!-- Use 3des_test.php to mock the 3DES'ed boxid for the auth request -->
<request subst="true">
<dyn_variable name="boxid" re="urlencode:(.*)"/>
<http url='/app/test/test.php?type=encode&amp;string=010004%%_useridend%%' version='1.1' method='GET'></http>
</request>
 
<request subst="true">
<dyn_variable name="auth" re="&lt;auth relpy=&quot;(.*)&quot;/&gt;"/>
<dyn_variable name="entoken" re="&lt;token value=&quot;(.*)&quot;/&gt;"/>
<http url='/app/test/index.php?version=1.1&amp;action=auth&amp;userid=%%_userid%%' version='1.1' method='GET'></http>
</request>
 
<if var="auth" eq="success">
 
<!-- decode the token -->
<request subst="true">
<dyn_variable name="token" re="Decode:(.*)"/>
<http url='/app
/test/test.php?type=decode&amp;string=%%_entoken%%'
version='1.1' method='GET'></http>

</request>
 
<!-- sleep time to mock PAD client operation, if thinktime more than 2 sec, upgrade request will be failed -->
<thinktime random='true' value='1'/>
 
<!-- upgrade request -->
<request subst="true">
<dyn_variable name="upgraderesult" re="&lt;upgrade result=&quot;([^&quot;]*)&quot;&gt;"/>
<dyn_variable name="upgradever" re="&lt;swinfo version=&quot;([^&quot;]*)&quot;"/>
<http url='/app/test/index.php?version=1.1&amp;action=update&amp;hw_version=10.0.0&amp;swversion=3&amp;token=%%_token%%' version='1.1' method='GET'></http>
</request>
 
<if var="upgraderesult" eq="0">
<!-- download request -->
<request subst="true">
<http url='/app/test/download.php?token=%%_token%%' version='1.1' method='GET'></http>
</request>
 
<!-- confirm request -->
<request subst="true">
<http url='/app/test/index.php?action=confirm&amp;result=0&amp;oldversion=3&amp;curversion=%%_upgradever%%&amp;token=%%_token%%&amp;version=1.1' version='1.1' method='GET'></http>
</request>
</if>
</if>
 
 
</session>

Descriptions:

  • random_string:
<setdynvars sourcetype="random_string" length="20">
<var name="useridend" />
</setdynvars>

Means a dynamic variable can be a random string "useridend", its lenght 20. BTW, a dynamic variable can be a random number by random_number

  • <dyn_variable name="userid" re="urlencode:(.*)"/>: means get boxid values using regular expression to filter from http response.
  • Using a variable like %%_userid%%
  • When you want to use dynamic variables, please using the subst attribut in request element