ruff/scripts/benchmarks/run_formatter.sh
Charlie Marsh 4df9e07a79
Add a benchmarking script for the formatter CLI (#7340)
## Summary

This PR adds a benchmarking script for the formatter, which benchmarks
the Ruff formatter against Black, yapf, and autopep8.

Three benchmarks are included:

1. Format everything.
2. Format everything, but use a single thread.
3. Format everything, but `--check` (don't write to disk).

There's some nuance in figuring out the right combination of arguments
to each command, but the _main_ nuance is to ensure that we always run
the given formatter (and modify the target repo in-place) prior to
benchmarking it, so that the formatters aren't disadvantaged by the
existing formatting of the target repo. (E.g.: prior to benchmarking
Black's preview style, we need to make sure we format the target repo
with Black's preview style; otherwise, preview style appears much
slower.)

Part of https://github.com/astral-sh/ruff/issues/7309.
2023-09-13 13:15:48 -04:00

74 lines
3.9 KiB
Bash
Executable file

#!/usr/bin/env sh
###
# Benchmark the Ruff formatter's performance against a variety of similar tools.
#
# Expects to be run from the repo root after invoking `cargo build --release`,
# in an environment with access to `black`, `autopep8`, and `yapf` (most recently:
# `black` v23.9.1, `autopep8` v2.0.4, and `yapf` v0.40.1).
#
# Example usage:
#
# ./scripts/benchmarks/run_formatter.sh ~/workspace/zulip
###
TARGET_DIR=${1}
# In each case, ensure that we format the code in-place before invoking a given tool. This ensures
# a fair comparison across tools, since every tool is then running on a repository that already
# matches that tool's desired formatting.
#
# For example, if we're benchmarking Black's preview style, we first run `black --preview` over the
# target directory, thus ensuring that we're benchmarking preview style against a codebase that
# already conforms to it. The same goes for yapf, autoepp8, etc.
# Benchmark 1: Write to disk.
hyperfine --ignore-failure \
--prepare "./target/release/ruff format ${TARGET_DIR}" \
"./target/release/ruff format ${TARGET_DIR}" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --safe" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --safe" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --fast" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --fast" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --safe --preview" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --safe --preview" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --fast --preview" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --fast --preview" \
--prepare "autopep8 ${TARGET_DIR} --recursive --in-place" \
"autopep8 ${TARGET_DIR} --recursive --in-place" \
--prepare "yapf ${TARGET_DIR} --parallel --recursive --in-place" \
"yapf ${TARGET_DIR} --parallel --recursive --in-place"
# Benchmark 2: Write to disk, but only use one thread.
hyperfine --ignore-failure \
--prepare "./target/release/ruff format ${TARGET_DIR}" \
"RAYON_NUM_THREADS=1 ./target/release/ruff format ${TARGET_DIR}" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --safe" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --workers=1 --safe" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --fast" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --workers=1 --fast" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --safe --preview" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --workers=1 --safe --preview" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --fast --preview" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --workers=1 --fast --preview" \
--prepare "autopep8 ${TARGET_DIR} --recursive --in-place" \
"autopep8 ${TARGET_DIR} --in-place --recursive --jobs=1" \
--prepare "yapf ${TARGET_DIR} --parallel --recursive --in-place" \
"yapf ${TARGET_DIR} --recursive --in-place"
# Benchmark 3: Check formatting, but don't write to disk.
hyperfine --ignore-failure \
--prepare "./target/release/ruff format ${TARGET_DIR}" \
"./target/release/ruff format ${TARGET_DIR} --check" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --safe" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --check --safe" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --fast" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --check --fast" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --safe --preview" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --check --safe --preview" \
--prepare "BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --fast --preview" \
"BLACK_CACHE_DIR=/dev/null black ${TARGET_DIR} --check --fast --preview" \
--prepare "autopep8 ${TARGET_DIR} --recursive --in-place" \
"autopep8 ${TARGET_DIR} --recursive --diff" \
--prepare "yapf ${TARGET_DIR} --parallel --recursive --in-place" \
"yapf ${TARGET_DIR} --parallel --recursive --quiet"