hyperfine Comandos
hyperfine é uma ferramenta de benchmarking de linha de comando escrita em Rust. Fornece análise estatística de tempos de execução de comandos, gerencia execuções de aquecimento, suporta benchmarks parametrizados e exporta resultados em múltiplos formatos.
Instalação
Linux/Ubuntu
# Ubuntu/Debian
sudo apt install hyperfine
# Fedora
sudo dnf install hyperfine
# Arch Linux
sudo pacman -S hyperfine
# Cargo (Rust)
cargo install hyperfine
# From GitHub releases
wget https://github.com/sharkdp/hyperfine/releases/latest/download/hyperfine_1.19.0_amd64.deb
sudo dpkg -i hyperfine_1.19.0_amd64.deb
# Verify
hyperfine --version
Benchmarking Básico
# Benchmark a single command
hyperfine 'sleep 0.5'
# Benchmark with a specific number of runs
hyperfine --runs 20 'my_command'
# Benchmark with minimum number of runs
hyperfine --min-runs 50 'my_command'
# Set minimum benchmark time (seconds)
hyperfine --min-benchmarking-time 10 'my_command'
Comparando Comandos
# Compare two commands
hyperfine 'find . -name "*.py"' 'fd -e py'
# Compare multiple commands
hyperfine 'grep -r TODO .' 'rg TODO' 'ag TODO'
# Compare with readable labels
hyperfine --command-name 'GNU grep' 'grep -r TODO .' \
--command-name 'ripgrep' 'rg TODO' \
--command-name 'silver searcher' 'ag TODO'
# Compare sorting implementations
hyperfine 'sort large_file.txt' 'sort --parallel=4 large_file.txt'
# Compare different compilers
hyperfine 'gcc -O2 -o test test.c && ./test' \
'clang -O2 -o test test.c && ./test'
Execuções de Aquecimento
# Run warmup iterations before benchmarking (populates caches)
hyperfine --warmup 5 'cat large_file.txt | wc -l'
# Warmup is essential for:
# - Filesystem cache warming
# - JIT compilation warmup
# - DNS cache population
hyperfine --warmup 10 'curl -s http://localhost:8080/api/data > /dev/null'
# Prepare command (run before each benchmark, not timed)
hyperfine --prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches' \
'cat large_file.txt | wc -l'
Configuração e Limpeza
# Run a setup command before the benchmark suite
hyperfine --setup 'make build' './my_program'
# Run a prepare command before EACH iteration (not timed)
hyperfine --prepare 'cp original.txt test.txt' 'sort -o test.txt test.txt'
# Run a cleanup command after each iteration
hyperfine --cleanup 'rm -f output.txt' 'my_command > output.txt'
# Combine setup, prepare, and cleanup
hyperfine \
--setup 'gcc -O2 -o benchmark benchmark.c' \
--prepare 'echo 3 | sudo tee /proc/sys/vm/drop_caches > /dev/null' \
--cleanup 'rm -f /tmp/bench_output' \
'./benchmark > /tmp/bench_output'
Listas de Parâmetros
# Benchmark with a range of parameter values
hyperfine --parameter-scan threads 1 8 './my_program --threads {threads}'
# Parameter scan with step size
hyperfine --parameter-scan size 100 1000 --parameter-step-size 100 \
'./process --size {size}'
# Parameter list (explicit values)
hyperfine --parameter-list lang 'python3,node,ruby' '{lang} script.{lang}'
# Multiple parameter lists
hyperfine --parameter-list compiler 'gcc,clang' \
--parameter-list opt '0,1,2,3' \
'{compiler} -O{opt} -o test test.c && ./test'
# Benchmark different file sizes
hyperfine --parameter-list size 'small,medium,large' \
--prepare 'cp data_{size}.csv input.csv' \
'./process input.csv'
Formatos de Exportação
# Export as JSON (most detailed)
hyperfine --export-json results.json 'my_command'
# Export as CSV
hyperfine --export-csv results.csv 'my_command'
# Export as Markdown table
hyperfine --export-markdown results.md 'command1' 'command2'
# Export as AsciiDoc
hyperfine --export-asciidoc results.adoc 'command1' 'command2'
# Export all formats at once
hyperfine \
--export-json results.json \
--export-csv results.csv \
--export-markdown results.md \
'command1' 'command2'
# The JSON output includes:
# - Mean, median, min, max times
# - Standard deviation
# - Individual run times
# - Parameter values (if used)
Análise Estatística
# Show statistical outlier detection
hyperfine 'my_command'
# Output shows: mean, min, max, and flags outliers
# Increase precision with more runs
hyperfine --runs 100 'my_command'
# Ignore the first N runs as outliers
hyperfine --ignore-failure 'my_command'
# Show individual run times
hyperfine --show-output 'my_command'
# Time the shell startup overhead
hyperfine --shell=none 'my_command'
# Use a specific shell
hyperfine --shell /bin/bash 'my_command'
hyperfine --shell /bin/zsh 'my_command'
# No shell (direct execution — avoids shell overhead)
hyperfine -N './my_binary arg1 arg2'
Controle de Saída
# Show command output (don't suppress stdout/stderr)
hyperfine --show-output 'echo hello'
# Style options
hyperfine --style full 'my_command' # Full progress bar (default)
hyperfine --style basic 'my_command' # Simple text output
hyperfine --style nocolor 'my_command' # No ANSI colors
hyperfine --style color 'my_command' # Colored output
hyperfine --style none 'my_command' # Minimal output
# Sort results by mean time
hyperfine --sort mean-time 'cmd1' 'cmd2' 'cmd3'
# Sort by command name
hyperfine --sort command 'cmd1' 'cmd2' 'cmd3'
Exemplos Práticos
# Compare JSON parsers
hyperfine --warmup 3 \
'python3 -c "import json; json.load(open(\"data.json\"))"' \
'python3 -c "import orjson; orjson.loads(open(\"data.json\",\"rb\").read())"'
# Benchmark database queries
hyperfine --warmup 2 --runs 30 \
'psql -c "SELECT count(*) FROM users" mydb' \
'psql -c "SELECT count(*) FROM users WHERE active = true" mydb'
# Compare file compression
hyperfine --warmup 1 --prepare 'cp original.tar test.tar' \
'gzip test.tar' \
--prepare 'cp original.tar test.tar' \
'zstd test.tar' \
--prepare 'cp original.tar test.tar' \
'lz4 test.tar'
# Benchmark Docker build cache
hyperfine --warmup 1 --runs 5 \
'docker build -t test .'
# Compare regex engines
hyperfine \
"grep -cP '\d{3}-\d{3}-\d{4}' large_log.txt" \
"rg -c '\d{3}-\d{3}-\d{4}' large_log.txt"
# Benchmark HTTP endpoints
hyperfine --warmup 5 \
'curl -s http://localhost:8080/api/v1/users > /dev/null' \
'curl -s http://localhost:8080/api/v2/users > /dev/null'
# Test I/O performance with cold cache
hyperfine \
--prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches > /dev/null' \
'cat /path/to/large/file > /dev/null'
Interpretando Resultados
# Example output:
# Benchmark 1: fd -e py
# Time (mean +/- sigma): 12.3 ms +/- 1.2 ms [User: 8.1 ms, System: 4.0 ms]
# Range (min ... max): 10.5 ms ... 16.8 ms 50 runs
#
# - mean: average execution time
# - sigma: standard deviation (lower = more consistent)
# - User: CPU time in user mode
# - System: CPU time in kernel mode
# - Range: fastest and slowest runs
# - "X runs" were performed for statistical significance
#
# Summary:
# fd -e py ran 12.45 +/- 3.21 times faster than find . -name "*.py"
Referência Rápida
| Flag | Purpose |
|---|---|
--runs N | Set exact number of benchmark runs |
--warmup N | Warmup runs before measuring |
--prepare CMD | Run before each benchmark iteration |
--cleanup CMD | Run after each benchmark iteration |
--setup CMD | Run once before the entire suite |
--parameter-scan | Iterate over a numeric range |
--parameter-list | Iterate over explicit values |
--export-json | Export detailed JSON results |
--export-csv | Export CSV results |
--export-markdown | Export Markdown table |
--command-name | Label for display |
-N / --shell=none | Skip shell, run binary directly |
--show-output | Don’t suppress stdout/stderr |
--ignore-failure | Continue on non-zero exit codes |