Skip to content

Instantly share code, notes, and snippets.

@georgeOsdDev
Last active August 26, 2019 13:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save georgeOsdDev/9988060 to your computer and use it in GitHub Desktop.
Save georgeOsdDev/9988060 to your computer and use it in GitHub Desktop.
Tsungによるwebsocket負荷テスト(日本語説明付き)
<!--
http://george-osd-blog.heroku.com/25
http://george-osd-blog.heroku.com/26
-->
<?xml version="1.0"?>
<!-- インストールしたTsungのPathとあっていること -->
<!DOCTYPE tsung SYSTEM "/home/ec2-user/opt/tsung-latest/share/tsung/tsung-1.0.dtd">
<tsung loglevel="notice" version="1.0">
<!-- 負荷テストを実施するクライアントの設定 -->
<clients>
<!-- hostnameで設定すること -->
<client host="tsung1" >
<ip scan="true" value="eth0"/>
</client>
<client host="tsung2" >
<ip scan="true" value="eth0"/>
</client>
<client host="tsung3" >
<ip scan="true" value="eth0"/>
</client>
</clients>
<!-- テスト対象のサーバー -->
<servers>
<server host="MY_TARGET_HOST.com" port="80" type="tcp"></server -->
<!-- wss を使う場合 -->
<!-- server host="MY_TARGET_HOST.com" port="443" type="ssl"></server -->
<!--
 Amazon ELB を使う場合
port 80 を ELBのinboundに設定すると`upgrade`ヘッダーが削除されてしまうので
80以外のportをtcp設定でELBを設定する。
-->
<!-- server host="MY_ELB.ap-northeast-1.elb.amazonaws.com" port="8000" type="tcp"></server -->
</servers>
<!-- コネクション数の指定 -->
<load>
<!--
phase1 は テスト対象サーバおよび Tsungのワームアップとして少なめに指定
設定内容は5分間で毎分10ユーザーずつ最大50ユーザーまでクライアントを生成する。
-->
<arrivalphase phase="1" duration="5" unit="minute">
<users arrivalrate="10" unit="minute" maxnumber="50" ></users>
</arrivalphase>
<!--
phase2 が実際の計測対象
設定内容は240分間で毎分100ユーザーずつ最大1000ユーザーまでクライアントを生成する。
-->
<arrivalphase phase="2" duration="240" unit="minute">
<users arrivalrate="100" unit="minute" maxnumber="1000" ></users>
</arrivalphase>
</load>
<!-- 各種オプション-->
<options>
<option name="ports_range" min="1025" max="65535"/> <!-- クライアントが使うポートレンジ -->
<option name="websocket_path" value="/chat"/> <!-- クライアントwebsocketのパス -->
</options>
<!--
実際のクライアントの処理内容
生成されたクライアントはいずれかのsessionを行うことになるため、probabilityの合計値は100とする必要がある
-->
<sessions>
<!-- このsessionは70%のクライアントが実施する -->
<session probability="70" name="websocket-example" type="ts_websocket">
<!-- websocketコネクションの確立-->
<request>
<websocket frame="text" type="connect" path="/chat"></websocket>
</request>
<!-- connect が成功したらログインする(アプリのロジック) -->
<!-- リクエスト内で変数を使う場合 `subst="true"` とする-->
<request subst="true">
<!--
今回のアプリではwebsocket上でのログイン処理はJSON形式でuserIdとpasswordを送る
サーバ側はログイン処理後にwebsocketに対して何かしらのレスポンスを返す
サーバからのレスポンスを待たない場合は`ack="no_ack"`をつける(後述)
userIdをユニークにするため、組み込み関数の`%%ts_user_server:get_unique_id%%`を使用する
`%%`で挟むことでErlangの変数として認識される
`%%ts_user_server:get_unique_id%%`は全Tsungクライアント間でユニークな数字となる。
`frame="text"`を指定しない場合デフォルトはbinary
-->
<websocket frame="text" type="message">
{"seq":0,"tag":"login","userId":"tsunguser%%ts_user_server:get_unique_id%%","password":"password"}
</websocket>
</request>
<!--
ログイン完了後 websocketのコネクションを維持するため
ここでは9999回ループ (%%_heartbeat%%と書くと現在のループの回数が取得できる)
でhというメッセージを送り続ける
-->
<for var="heartbeat" from="1" to="9999" incr="1">
<!-- thinktimeでスリープ処理 (単位は秒)-->
<thinktime value="25"/>
<request>
<!-- `ack="no_ack"` を書かない場合サーバからのレスポンスを待つ -->
<websocket frame="text" ack="no_ack" type="message">h</websocket>
</request>
</for>
</session>
<!--
このsessionは30%のクライアントが実施する
オンライン状態である全1000ユーザのうち300ユーザはアクティブな処理を行う
-->
<session probability="30" name="websocket-example" type="ts_websocket">
<request subst="true">
<websocket frame="text" type="connect" path="/chat"></websocket>
</request>
<websocket frame="text" type="message">
{"seq":0,"tag":"login","userId":"tsunguser%%ts_user_server:get_unique_id%%","password":"password"}
</websocket>
<!-- 定期的にサーバにメッセージを送る -->
<for var="counter" from="1" to="9999" incr="1">
<thinktime value="72"/>
<request subst="true">
<websocket frame="text" type="message">
{"body":"something chat message","seq":%%_counter%%}
</websocket>
</request>
</for>
</session>
</sessions>
</tsung>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment