Linuxメモ : hyperfine, benchでコマンドのベンチマーク
hyperfine, benchというコマンドを複数回実行してベンチマークができるツールがあったので試してみる。
hyperfine
hyperfineはRustで書かれたコマンドのベンチマークツール。
作者の人は同じくRust製のfdコマンド(findコマンドの代替)を開発しており、デモではfindとfdのベンチマークをしている(fdの方が高速)。
hyperfineは後述するbenchにインスパイアされたとのこと。

インストール
Rustで書かれているのでcargoでインストールできる。
$ cargo install hyperfine
その他、Arch Linux, Ubuntuでのインストール方法などはREADMEを参照。
ヘルプ情報。
$ hyperfine --help
hyperfine 0.4.0
A command-line benchmarking tool.
USAGE:
hyperfine [OPTIONS] <command>...
OPTIONS:
-w, --warmup <NUM> Perform NUM warmup runs before the actual benchmark. This can
be used to fill (disk) caches for I/O-heavy programs.
-m, --min-runs <NUM> Perform at least NUM runs for each command (default: 10).
-p, --prepare <CMD> Execute CMD before each timing run. This is useful for
clearing disk caches, for example.
-s, --style <TYPE> Set output style type (default: auto). Set this to 'basic' to
disable output coloring and interactive elements. Set it to
'full' to enable all effects even if no interactive terminal
was detected. [values: auto, basic,
full]
-i, --ignore-failure Ignore non-zero exit codes.
-h, --help Print this help message.
-V, --version Show version information.
ARGS:
<command>... Command to benchmark
使い方
基本的な使い方は引数としてベンチマークしたいコマンドを指定する。
$ hyperfine 'sleep 0.3'

スペース区切りで複数コマンドを指定することも可能。

デフォルトでは各コマンドを最低10回実行するようになっているが--min-runsで回数の変更は可能。
$ hyperfine --min-runs 5 'sleep 0.3'
その他、ディスクキャッシュが結果に影響する場合は--warmup, --prepareオプションを使うとよいとのこと。
bench
benchはHaskellで書かれたコマンドのベンチマークツール。
インストール
macOSならbrewでインストールできる(Relasesにバイナリもある)。
$ brew install bench
Haskellを使っている人なら下記コマンドでインストールできる。
$ stack setup $ stack install bench
ヘルプ情報。
$ bench --version
bench version 1.0.8
$ bench --help
Command-line tool to benchmark other programs
Usage: bench ((-v|--version) | COMMAND(S) [-I|--ci CI] [-L|--time-limit SECS]
[--resamples COUNT] [--regress RESP:PRED..] [--raw FILE]
[-o|--output FILE] [--csv FILE] [--json FILE] [--junit FILE]
[-v|--verbosity LEVEL] [-t|--template FILE] ([-m|--match MATCH]
[NAME...] | [-n|--iters ITERS] [-m|--match MATCH] [NAME...] |
[-l|--list] | [--version]))
Available options:
-h,--help Show this help text
-v,--version Version number
COMMAND(S) The command line(s) to benchmark
-I,--ci CI Confidence interval
-L,--time-limit SECS Time limit to run a benchmark
--resamples COUNT Number of bootstrap resamples to perform
--regress RESP:PRED.. Regressions to perform
--raw FILE File to write raw data to
-o,--output FILE File to write report to
--csv FILE File to write CSV summary to
--json FILE File to write JSON summary to
--junit FILE File to write JUnit summary to
-v,--verbosity LEVEL Verbosity level
-t,--template FILE Template to use for report
-m,--match MATCH How to match benchmark names ("prefix", "glob",
"pattern", or "ipattern")
-n,--iters ITERS Run benchmarks, don't analyse
-m,--match MATCH How to match benchmark names ("prefix", "glob",
"pattern", or "ipattern")
-l,--list List benchmarks
--version Show version info
使い方
同じくベンチマークしたいコマンドを指定すればよい。hyperfineと違い実行中の進捗状況は表示されない。

複数コマンドの指定も可能。

--outputオプションを指定すると結果をHTML出力できる(brewでインストールした版だとbench: TemplateNotFound "default"というエラーになったのでReleasesからバイナリをダウンロードして実行)。
$ bench 'sleep 0.3' --output example.html

レポートの見方はこのページの下部に書かれている。