Web屋さんはみんな大好きabコマンドは便利ですが、オプションは複雑で数も多く、複数のホストにそれぞれに対応するオプションを指定してテストしたりすると結構カオスになりがちです。
最近では、httperfやweighttp等のabに変わる次のHTTPベンチマークツールが出てきていますが、やっぱりまだまだ現役で良く使うのはabコマンドだと思います。
そこで、今回はabコマンドの複数のベンチマークオプションのパターンを1つのRubyスクリプトに定義しておいて、それをabコマンドで読み込む事で動的に任意のパターンでベンチマークを行うab-mrubyを作りました。外出しで書いたRubyスクリプトとabコマンドの連携は、ab-mrubyと見てわかるように、mrubyを使っています。
これによって、RubyでURL別にベンチマークパターンを定義しておけば、URL毎にいちいちオプションを指定する必要がありません。ab-mrubyで指定するベンチマーク対象URLから、動的にベンチマークパターンを決定することができます。また、オプションの羅列に比べて非常に可読性も良いので、後から見ても、どういうベンチマークパターンだったかすぐわかるようになります。
使い方
上記のように書くだけではいまいちわかりにくいので、使い方を見てみましょう。基本的には、ab-mrubyコマンドは今までのような引数にオプションを渡す使い方でも使えるようにしています。その上で、-mオプションでRubyスクリプトを渡せるようにしました。
./ab-mruby -m ab-mruby.conf.rb http://blog.example.jp/
で、実際にベンチマークパターンはRubyで以下のように書きます。対象のホストやURL毎にパターンを定義します。
# Usage: ./ab-mruby -m ab-mruby.conf.rb [http[s]://]hostname[:port]/path
#
# add_config(
# "TotalRequests" => 100, # int
# "Concurrency" => 10, # int max 20000
# "KeepAlive" => true, # true or false or nil
# "VerboseLevel" => 1, # int 1 ~ 5
# "ShowProgress" => true, # true, false or nil
# "ShowPercentile" => true, # true, false or nil
# "ShowConfidence" => true, # true, false or nil
# "WaitSocketError" => true, # true, false or nil
# "RequestTimeOut" => 30, # int sec
# "BechmarkTimelimit" => 50000, # int sec
# "WindowSize" => nil, # int byte
# "HeadMethodOnly" => false, # true, false or nil
# "Postfile" => nil, # './post.txt',
# "Putfile" => nil, # './put.txt',
# "ContentType" => nil, # 'application/x-www-form-urlencoded',
# "OutputGnuplotFile" => nil, # './gnu.txt'
# "OutputCSVFile" => nil, # './csv.txt'
# "AddCookie" => nil, # 'Apache=1234'
# "AddHeader" => 'User-Agent: ab-mruby', # 'User-Agent: test'
# "BasicAuth" => nil, # 'user:pass'
# "Proxy" => nil, # 'proxy[:port]'
# "ProxyAuth" => nil, # 'user:pass'
# "OutputHtml" => false, # true, false or nil
# "BindAddress" => nil, # 'matsumoto-r.jp'
# "SSLCipher" => 'DHE-RSA-AES128-SHA', # 'DHE-RSA-AES256-SHA' or get from [openssl ciphers -v]
# "SSLProtocol" => 'SSL3', # 'SSL2', 'SSL3', 'TLS1', 'TLS1.1', 'TLS1.2' or 'ALL'
# )
# get config value from C
p get_config("TargetURL").to_s
p get_config("TargetPort").to_s
p get_config("TargetHost").to_s
p get_config("TargetPath").to_s
p get_config("TargetisSSL").to_s
# defined config pattern
# for blog.example.jp
if get_config("TargetHost").to_s == "blog.example.jp"
add_config(
"TotalRequests" => 10, # int
"Concurrency" => 1, # int max 20000
"KeepAlive" => true, # true or false or nil
"VerboseLevel" => 1, # int 1 ~ 5
"ShowProgress" => true, # true, false or nil
"ShowPercentile" => true, # true, false or nil
"ShowConfidence" => true, # true, false or nil
"WaitSocketError" => true, # true, false or nil
"RequestTimeOut" => 30, # int sec
"BechmarkTimelimit" => 50000, # int sec
"WindowSize" => nil, # int byte
"HeadMethodOnly" => false, # true, false or nil
"Postfile" => nil, # './post.txt',
"Putfile" => nil, # './put.txt',
"ContentType" => nil, # 'application/x-www-form-urlencoded',
"OutputGnuplotFile" => nil, # './gnu.txt'
"OutputCSVFile" => nil, # './csv.txt'
"AddCookie" => nil, # 'Apache=1234'
"AddHeader" => 'User-Agent: ab-blog', # 'User-Agent: test'
"BasicAuth" => nil, # 'user:pass'
"Proxy" => nil, # 'proxy[:port]'
"ProxyAuth" => nil, # 'user:pass'
"OutputHtml" => false, # true, false or nil
"BindAddress" => nil, # 'matsumoto-r.jp'
"SSLCipher" => nil, # 'DHE-RSA-AES256-SHA' or get from [openssl ciphers -v]
"SSLProtocol" => nil, # 'SSL2', 'SSL3', 'TLS1', 'TLS1.1', 'TLS1.2' or 'ALL'
)
# for moblog.example.jp
elsif get_config("TargetHost").to_s == "moblog.example.jp"
add_config(
"TotalRequests" => 20, # int
"Concurrency" => 5, # int max 20000
"KeepAlive" => false, # true or false or nil
"VerboseLevel" => 5, # int 1 ~ 5
"ShowProgress" => true, # true, false or nil
"ShowPercentile" => true, # true, false or nil
"ShowConfidence" => true, # true, false or nil
"WaitSocketError" => true, # true, false or nil
"RequestTimeOut" => 30, # int sec
"BechmarkTimelimit" => 50000, # int sec
"WindowSize" => nil, # int byte
"HeadMethodOnly" => false, # true, false or nil
"Postfile" => nil, # './post.txt',
"Putfile" => nil, # './put.txt',
"ContentType" => nil, # 'application/x-www-form-urlencoded',
"OutputGnuplotFile" => nil, # './gnu.txt'
"OutputCSVFile" => nil, # './csv.txt'
"AddCookie" => nil, # 'Apache=1234'
"AddHeader" => 'User-Agent: ab-moblog', # 'User-Agent: test'
"BasicAuth" => nil, # 'user:pass'
"Proxy" => nil, # 'proxy[:port]'
"ProxyAuth" => nil, # 'user:pass'
"OutputHtml" => false, # true, false or nil
"BindAddress" => nil, # 'matsumoto-r.jp'
"SSLCipher" => nil, # 'DHE-RSA-AES256-SHA' or get from [openssl ciphers -v]
"SSLProtocol" => nil, # 'SSL2', 'SSL3', 'TLS1', 'TLS1.1', 'TLS1.2' or 'ALL'
)
# other pattern
else
add_config(
"TotalRequests" => 100, # int
"Concurrency" => 10, # int max 20000
"KeepAlive" => false, # true or false or nil
"VerboseLevel" => 1, # int 1 ~ 5
)
end
# for use ssl
if get_config("TargetisSSL")
add_config(
"SSLCipher" => 'DHE-RSA-AES128-SHA', # 'DHE-RSA-AES256-SHA' or get from [openssl ciphers -v]
"SSLProtocol" => 'SSL3', # 'SSL2', 'SSL3', 'TLS1', 'TLS1.1', 'TLS1.2' or 'ALL'
)
end
このRubyスクリプトを見ると、感の良いひとは大体わかると思います。
add_configでベンチマークパラメータをab-mrubyコマンド用に定義するのですが、add_configする前にget_configで、ab内からベンチマーク対象のURLに関する情報(ab-mrubyコマンドの引数に渡したベンチマーク対象URL)を引き出す事ができます。
p get_config("TargetURL").to_s
p get_config("TargetPort").to_s
p get_config("TargetHost").to_s
p get_config("TargetPath").to_s
p get_config("TargetisSSL").to_s
上記のように、URLやホスト名、ポートやSSLの有無等が得られます。この情報を利用することで、動的にベンチマークパターンを定義できます。例えばblog.exmaple.jpだったらadd_configで以下のようなパラメータでベンチマークを行い、
add_config(
"TotalRequests" => 10, # int
"Concurrency" => 1, # int max 20000
"KeepAlive" => true, # true or false or nil
"VerboseLevel" => 1, # int 1 ~ 5
"ShowProgress" => true, # true, false or nil
"ShowPercentile" => true, # true, false or nil
"ShowConfidence" => true, # true, false or nil
"WaitSocketError" => true, # true, false or nil
"RequestTimeOut" => 30, # int sec
"BechmarkTimelimit" => 50000, # int sec
"WindowSize" => nil, # int byte
"HeadMethodOnly" => false, # true, false or nil
"Postfile" => nil, # './post.txt',
"Putfile" => nil, # './put.txt',
"ContentType" => nil, # 'application/x-www-form-urlencoded',
"OutputGnuplotFile" => nil, # './gnu.txt'
"OutputCSVFile" => nil, # './csv.txt'
"AddCookie" => nil, # 'Apache=1234'
"AddHeader" => 'User-Agent: ab-blog', # 'User-Agent: test'
"BasicAuth" => nil, # 'user:pass'
"Proxy" => nil, # 'proxy[:port]'
"ProxyAuth" => nil, # 'user:pass'
"OutputHtml" => false, # true, false or nil
"BindAddress" => nil, # 'matsumoto-r.jp'
"SSLCipher" => nil, # 'DHE-RSA-AES256-SHA' or get from [openssl ciphers -v]
"SSLProtocol" => nil, # 'SSL2', 'SSL3', 'TLS1', 'TLS1.1', 'TLS1.2' or 'ALL'
)
また、別のmoblog.exmaple.jpというホスト名だった場合は、以下のようなベンチマークパラメータでベンチマークを行います。
add_config(
"TotalRequests" => 20, # int
"Concurrency" => 5, # int max 20000
"KeepAlive" => false, # true or false or nil
"VerboseLevel" => 5, # int 1 ~ 5
"ShowProgress" => true, # true, false or nil
"ShowPercentile" => true, # true, false or nil
"ShowConfidence" => true, # true, false or nil
"WaitSocketError" => true, # true, false or nil
"RequestTimeOut" => 30, # int sec
"BechmarkTimelimit" => 50000, # int sec
"WindowSize" => nil, # int byte
"HeadMethodOnly" => false, # true, false or nil
"Postfile" => nil, # './post.txt',
"Putfile" => nil, # './put.txt',
"ContentType" => nil, # 'application/x-www-form-urlencoded',
"OutputGnuplotFile" => nil, # './gnu.txt'
"OutputCSVFile" => nil, # './csv.txt'
"AddCookie" => nil, # 'Apache=1234'
"AddHeader" => 'User-Agent: ab-moblog', # 'User-Agent: test'
"BasicAuth" => nil, # 'user:pass'
"Proxy" => nil, # 'proxy[:port]'
"ProxyAuth" => nil, # 'user:pass'
"OutputHtml" => false, # true, false or nil
"BindAddress" => nil, # 'matsumoto-r.jp'
"SSLCipher" => nil, # 'DHE-RSA-AES256-SHA' or get from [openssl ciphers -v]
"SSLProtocol" => nil, # 'SSL2', 'SSL3', 'TLS1', 'TLS1.1', 'TLS1.2' or 'ALL'
)
また、上記に該当しないホストだった場合は、下記のシンプルなベンチマークパラメータを利用する、という設定になります。
add_config(
"TotalRequests" => 100, # int
"Concurrency" => 10, # int max 20000
"KeepAlive" => false, # true or false or nil
"VerboseLevel" => 1, # int 1 ~ 5
)
end
また、もしベンチマーク対象がSSLだった場合は、CipherやSSLプロトコルを任意のものに指定してやる、という設定も可能です。
add_config(
"SSLCipher" => 'DHE-RSA-AES128-SHA', # 'DHE-RSA-AES256-SHA' or get from [openssl ciphers -v]
"SSLProtocol" => 'SSL3', # 'SSL2', 'SSL3', 'TLS1', 'TLS1.1', 'TLS1.2' or 'ALL'
)
end
最後に
これで、これまで覚えにくかったabコマンドのオプションを、Rubyで簡単に書く事ができて、さらにホスト毎のベンチマークパラメータを個別に条件分岐で記述しておいておくことで、ホスト毎のベンチマークパターンを動的に生成することができますね!
また、このRubyスクリプトに各ベンチマーク対象のホストを定義しておけば、複数のホストに対しても、ab-mruby -m でRubyスクリプトを渡すだけで、煩わしいオプションを個別に引数で書くことなく汎用的にベンチマークすることが可能になりますね!
これでさらにabコマンドが使いやすく、オプションもホスト毎に管理しやすくなるので、abコマンドによるベンチマークテストが捗る事間違い無しでしょう。
このツールは、今考えているHTTPのテストフレームワークの一貫で作ったものなので、さらにプロジェクトを進めていって、また良いものができたら公開しますし、最後にできあがるHTTPのテストフレームワークもいつかは公開したいと思いますのでお楽しみに!
0 Comments.