Skip to content

Instantly share code, notes, and snippets.

@izhar
Last active December 5, 2017 15:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save izhar/67a52dfd75a3efde65b1 to your computer and use it in GitHub Desktop.
Save izhar/67a52dfd75a3efde65b1 to your computer and use it in GitHub Desktop.
Tsung getting started

###Benchmarking the system

We'll use tsung to benchmark the system.

####Tsung quick install

$ sudo apt-get install -y build-essential erlang gnuplot libtemplate-perl
$ sudo apt-get update
$ wget http://tsung.erlang-projects.org/dist/tsung-1.5.1.tar.gz
$ tar zxvf ./tsung-1.5.1.tar.gz
$ cd ./tsung-1.5.1
$ ./configure
$ make
$ sudo make install

Create a simple tsung load test xml file

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd" []>
<tsung loglevel="warning">

  <clients>
    <!-- make sure to use hostnames - no IP or 'localhost' -->
    <client host="ip-10-171-71-217" cpu="2" maxusers="30000000"/>
  </clients>

  <servers>
    <!--server host="ec2-54-237-9-50.compute-1.amazonaws.com" port="3000" type="tcp"/-->
    <server host="localhost" port="3000" type="tcp"/>
  </servers>

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

  <sessions>
    <session name="stewie_analytics_load" weight="1" type="ts_http">
      <request>
	      <http
		url="url="/v1/usage?account_id=12&amp;event_type_id=1&amp;calendar_date=142767360000"
                method="GET" />
      </request>
    </session>
  </sessions>
</tsung>

Start the test

$ tsung -f simple-test.xml start
Starting Tsung
"Log directory is: /home/ubuntu/.tsung/log/20150409-0611"

Genrating the reports

$ mkdir some_reports_dir
$ cd <above report dir>
$ /usr/lib/tsung/bin/tsung_stats.pl --stats /home/ubuntu/.tsung/log/20150409-0611/tsung.log
-- point browser to the generated graph.html

Using dynamic parameter substitution

You can have tsung send dynamic data by calling user functions. To have tsung invoke user function, you need to place mark-up elements in the xml file. The docs are found here.

Example:

The same url invoked as above, but with dynamic account_is and event_type_id params.

The xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd" []>
<tsung loglevel="warning">

  <clients>
    <client host="ip-10-63-70-59" cpu="2" maxusers="90000000"/>
  </clients>

  <servers>
    <server host="10.171.71.217" port="3000" type="tcp"/>
  </servers>

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

  <sessions>
    <session name="stewie_analytics_load" weight="1" type="ts_http">
      <request subst="true">
              <http
                url="/v1/usage?account_id=%%stewie_usage:one_of_account_id%%&amp;event_type_id=1&amp;calendar_date=142767360000"
                method="GET" />
      </request>
    </session>
  </sessions>
</tsung>

The erlang callback module:

-module(stewie_usage).
-export([one_of_account_id/1,one_of_event_type_id/1,one_of_calendar_date/1]).

one_of_account_id({Pid, DynData}) ->
   case random:uniform(6) of
       1 -> "17";
       2 -> "12";
       3 -> "41";
       4 -> "39";
       5 -> "40";
       6 -> "33"
   end.

one_of_event_type_id({Pid, DynData}) ->
   case random:uniform(2) of
       1 -> "1";
       2 -> "4"
   end.

one_of_calendar_date({Pid, DynData}) ->
   case random:uniform(2) of
       1 -> "1";
       2 -> "4"
   end.

compile the above code:

$ ll
-rw-rw-r-- 1 ubuntu ubuntu  503 Apr  9 11:18 stewie_usage.erl
$
$ erlc ./stewie_usage.erl 
stewie_usage.erl:4: Warning: variable 'DynData' is unused
stewie_usage.erl:4: Warning: variable 'Pid' is unused
stewie_usage.erl:14: Warning: variable 'DynData' is unused
stewie_usage.erl:14: Warning: variable 'Pid' is unused
stewie_usage.erl:20: Warning: variable 'DynData' is unused
stewie_usage.erl:20: Warning: variable 'Pid' is unused
$
$ ll
-rw-rw-r-- 1 ubuntu ubuntu  944 Apr  9 11:20 stewie_usage.beam
-rw-rw-r-- 1 ubuntu ubuntu  503 Apr  9 11:18 stewie_usage.erl

copy to the tsung ebin dir:

sudo cp ./stewie_usage.beam /usr/lib/erlang/lib/tsung-1.5.1/ebin/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment