APIの負荷テストをする際にSiegeを使ったけど、ハマったこと


ちょっとした話。

APIの負荷テストにSiegeを使う

今、会社で利用している既存のAPIに、新規機能を追加する案件を請け負っています。
それにあたって、負荷テスト等が必要なのですが何も知識が無いので色々なツールで遊んでいました。
タイトルに書いたSiegeは、そのツール群の中の一つです。

クエリパラメータつきのURLを試すには……??

ひとまず、-hオプションで使い方を見てみました。

$ siege -h
SIEGE 4.0.4
Usage: siege [options]
       siege [options] URL
       siege -g URL
Options:
  -V, --version             VERSION, prints the version number.
  -h, --help                HELP, prints this section.
  -C, --config              CONFIGURATION, show the current config.
  -v, --verbose             VERBOSE, prints notification to screen.
  -q, --quiet               QUIET turns verbose off and suppresses output.
  -g, --get                 GET, pull down HTTP headers and display the
                            transaction. Great for application debugging.
  -p, --print               PRINT, like GET only it prints the entire page.
  -c, --concurrent=NUM      CONCURRENT users, default is 10
  -r, --reps=NUM            REPS, number of times to run the test.
  -t, --time=NUMm           TIMED testing where "m" is modifier S, M, or H
                            ex: --time=1H, one hour test.
  -d, --delay=NUM           Time DELAY, random delay before each requst
  -b, --benchmark           BENCHMARK: no delays between requests.
  -i, --internet            INTERNET user simulation, hits URLs randomly.
  -f, --file=FILE           FILE, select a specific URLS FILE.
  -R, --rc=FILE             RC, specify an siegerc file
  -l, --log[=FILE]          LOG to FILE. If FILE is not specified, the
                            default is used: PREFIX/var/siege.log
  -m, --mark="text"         MARK, mark the log file with a string.
                            between .001 and NUM. (NOT COUNTED IN STATS)
  -H, --header="text"       Add a header to request (can be many)
  -A, --user-agent="text"   Sets User-Agent in request
  -T, --content-type="text" Sets Content-Type in request
      --no-parser           NO PARSER, turn off the HTML page parser
      --no-follow           NO FOLLOW, do not follow HTTP redirects

Copyright (C) 2017 by Jeffrey Fulmer, et al.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.

うーん、クエリパラメータをつけてAPIを叩きたい時はどうすれば??

POSTメソッドのやり方ばっかり!

どうやら、--content-typeをつければ良い感じらしい事がググってみて分かりました。
ただ、具体例が全部POSTメソッドばかりでした。
例えば以下のような。

$ siege --concurrent=50 --content-type="text/plain" 'http://example.com/api POST plain-text-content-here'

引用元:https://thornelabs.blog/posts/using-siege-to-load-test-your-website-and-api.html

GETメソッドで試す

じゃあGETメソッドはどうするのか、色々試してみて分かりました。以下のようにすれば、問題ありませんでした。

$ siege --concurrent=50 --content-type="text/plain" 'http://example.com/api?type_id=1&id=1000'

このやり方に気づくのに、半日かかりました。
知識不足が原因でしょうか。。。
何はともあれ、やり方が分かって良かったです。
他の人が同じようにハマらないようにQiitaの記事にしました🤗