『ベンチマークツールを自作するな』の格言通り、計測する時は既存のツールを使うのが良いです。
今回、仕事でHTTPサーバに多量のリクエストを発生させる必要があったので、ベンチマークツールを使って負荷をかけることにしました。

この手のツールで有名なのはApache Benchですが、最近出てきたheyというGo言語製のツールがシングルバイナリで結果も見やすく、使い勝手が良かったので紹介します。

インストール

公式のGitHubリポジトリでバイナリを配布しているので、それを持ってくるのが楽です。
今回、私はmacでベンチマークを実施したので、homebrewからインストールしました。
インストールしたら、ヘルプを表示させるとかして導入できていることを確認します。

% hey --help
Usage: hey [options...] <url>

Options:
  -n  Number of requests to run. Default is 200.
  -c  Number of workers to run concurrently. Total number of requests cannot
      be smaller than the concurrency level. Default is 50.
  -q  Rate limit, in queries per second (QPS) per worker. Default is no rate limit.
  -z  Duration of application to send requests. When duration is reached,
      application stops and exits. If duration is specified, n is ignored.
      Examples: -z 10s -z 3m.
  -o  Output type. If none provided, a summary is printed.
      "csv" is the only supported alternative. Dumps the response
      metrics in comma-separated values format.

  -m  HTTP method, one of GET, POST, PUT, DELETE, HEAD, OPTIONS.
  -H  Custom HTTP header. You can specify as many as needed by repeating the flag.
      For example, -H "Accept: text/html" -H "Content-Type: application/xml" .
  -t  Timeout for each request in seconds. Default is 20, use 0 for infinite.
  -A  HTTP Accept header.
  -d  HTTP request body.
  -D  HTTP request body from file. For example, /home/user/file.txt or ./file.txt.
  -T  Content-type, defaults to "text/html".
  -a  Basic authentication, username:password.
  -x  HTTP Proxy address as host:port.
  -h2 Enable HTTP/2.

  -host	HTTP Host header.

  -disable-compression  Disable compression.
  -disable-keepalive    Disable keep-alive, prevents re-use of TCP
                        connections between different HTTP requests.
  -disable-redirects    Disable following of HTTP redirects
  -cpus                 Number of used cpu cores.
                        (default for current machine is 4 cores)

主要なオプション

基本的には、heyコマンドにURLを与えてやれば動きますが、オプションをつけることで自分の望む形でベンチマークを行うことができます。
すべてのオプションは前掲のヘルプに表示されている通りですが、私がよく使うものを抜粋して日本語訳をつけて紹介します。

-n  リクエスト数。デフォルト200リクエスト
-c  クライアントの数(同時並行リクエスト数)デフォルトは50で、リクエスト数より少ない必要がある。
-q  QPS(Query per second:秒間クエリ数)のリミット
-t  タイムアウト。デフォルト20秒
-cpus 使用するCPU数(環境によってデフォルト値が違っていて、私の環境では4コア)

使い方

オプションをつけて、下記の要領で叩くだけです。

% hey -n 500 -c 50 -t 3 https://www.10nin.com

結果は次のように表示されます。出力も見やすく、私はとても好きです。
-oオプションをつけることでCSVファイルに出力したりできるので、分析するのにも役立つと思います。

% hey -n 500 -c 50 -t 3 https://www.10nin.com
Summary:
  Total:	0.8534 secs
  Slowest:	0.3554 secs
  Fastest:	0.0258 secs
  Average:	0.0780 secs
  Requests/sec:	585.9177


Response time histogram:
  0.026 [1]	|
  0.059 [355]	|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.092 [94]	|■■■■■■■■■■■
  0.125 [0]	|
  0.158 [0]	|
  0.191 [0]	|
  0.224 [0]	|
  0.257 [0]	|
  0.289 [0]	|
  0.322 [14]	|■■
  0.355 [36]	|■■■■


Latency distribution:
  10% in 0.0401 secs
  25% in 0.0445 secs
  50% in 0.0497 secs
  75% in 0.0601 secs
  90% in 0.3084 secs
  95% in 0.3309 secs
  99% in 0.3499 secs

Details (average, fastest, slowest):
  DNS+dialup:	0.0166 secs, 0.0258 secs, 0.3554 secs
  DNS-lookup:	0.0034 secs, 0.0000 secs, 0.0354 secs
  req write:	0.0000 secs, 0.0000 secs, 0.0010 secs
  resp wait:	0.0608 secs, 0.0254 secs, 0.2280 secs
  resp read:	0.0006 secs, 0.0001 secs, 0.0167 secs

Status code distribution:
  [200]	500 responses