2017年5月12日 星期五

用 Tsung 對網站做壓力測試

需要對網站做大量連線測試,
找到這篇
How to Generate Millions of HTTP Requests
作者測試幾個工具最後找到最適用的是 Tsung
目前測試的設定檔一秒可以送出大約 2900 個 https request

在 Arch Linux 上直接從AUR安裝 Tsung

如果要產生HTML報告,

可能會遇到下列錯誤:
Can't locate Template.pm in @INC (you may need to install the Template module) (
@INC contains: /usr/lib/perl5/site_perl /usr/share/perl5/site_perl /usr/lib/perl
5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5/core_perl /usr/share/p
erl5/core_perl .) at /usr/lib/tsung/bin/tsung_stats.pl line 570.


要安裝 perl-template-toolkit
測試 HTTPS 連線如果遇錯誤,
可以依照網頁 Use tsung to test https site 的方式
先單獨測試 erlang 的 SSL連線
# erl
Erlang R14B04 (erts-5.8.5) [64-bit] [rq:1] [async-threads:0] [kernel-poll:false]

Eshell V5.8.5 (abort with ^G)
1> ssl:start().
ok
2> {ok, Socket} = ssl:connect("web_server_name", 443, [], infinity).

我遇到的問題是系統沒有更新到最新版,
所以 ssl 版本不符。

tsung 提供的設定檔範例在 /usr/share/doc/tsung/examples
複製一份 http_simple.xml 到 /tmp 修改
執行測試:
time tsung -f /tmp/http_simple.xml start
會在 ~/.tusng/log/ 下以日期和 pid 產生資料夾存放紀錄。

測試https連線設定檔:

<?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" maxusers="40000"/>
</clients>

<!-- Server side setup -->
<servers>
<server host="www.mysite.com" port="443" type="ssl"></server>
</servers>

<load>
<arrivalphase phase="1" duration="2" unit="second">
<users arrivalrate="4000" 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>

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

<request> <http url="https://www.mysite.com/" method="GET" version="1.1"></http> </request>

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


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


會連續2秒送出4000個連線請求,
但是實際完成時間要等所有連線都完成或未完成的連線逾時。
 
方便產生html 報告,
可以在 ~/.bashrc 加入(指令產生完會以firefox開啟)

alias treport="/usr/lib/tsung/bin/tsung_stats.pl;firefox report.html"
到~/.tusng/log/[紀錄資料夾] 產生html報告:
$/usr/lib/tsung/bin/tsung_stats.pl
或輸入上方新增的指令
$treport
會產生 report.html
如果最下方看到 HTTP return code 是正常HTTP連線結果代碼( 200 或 301 之類) 表示有成功的連線,
點選左方 Simultaneous Users 查看連線情形,
如果一開始最高只大約800,
就需要修改系統參數。
檢查單一程序允許開啟檔案數量:
$ulimit -n
預設只有 1024
因為 ulimit 無法以sudo 執行,
如果要單獨提高單獨 user 的數值,
好像只能從更改設定檔下手。

#vi /etc/security/limits.conf
myAccount hard nofile 50000
myAccount soft nofile 50000

但是還有整個系統的檔案開啟數量限制,
在 Tsung 的 FAQ 看到建議設定:

#vi /etc/sysctl.conf 或 /etc/sysctl.d/tsung.conf
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 1024 65000

fs.file-max = 65000



但是設定後在使用 android 模擬器時一直無法使用,

找到 不要在linux上啟用net.ipv4.tcp_tw_recycle參數
確定 net.ipv4.tcp_tw_recycle = 1 會讓 NAT 連接容易失敗,
造成 adb 使用 NAT 連線就會無法連接到模擬器。

如果發送測試的電腦有做NAT相關用途,
設定要改成

#vi /etc/sysctl.conf 或 /etc/sysctl.d/tsung.conf
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65000

fs.file-max = 65000

另外在找資料時看到 The Secret to 10 Million Concurrent Connections -The Kernel is the Problem, Not the Solution
提到 Unix 系統作為大量連線server的問題。

沒有留言:

張貼留言