Skip to content

Instantly share code, notes, and snippets.

@gromgit
Created March 11, 2021 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gromgit/d69997e104d558659213ee8cb64dfaac to your computer and use it in GitHub Desktop.
Save gromgit/d69997e104d558659213ee8cb64dfaac to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
tapdir=$HOMEBREW_REPOSITORY/Library/Taps
tmpd=$(mktemp -d)
trap 'rm -fr "$tmpd" "${tapdir}"/test/homebrew-tap[0-9]*' EXIT
# Set up taps for testing
# Tap with Formula
[[ -d "${tapdir}"/gromgit/homebrew-dev ]] || brew tap gromgit/dev
# Tap with HomebrewFormula
[[ -d "${tapdir}"/mikosa/homebrew-awsp ]] || brew tap mikosa/awsp https://github.com/mikosa/aws-permission
# Tap with formulae in root dir
[[ -d "${tapdir}"/kmalinich/homebrew-facter ]] || brew tap kmalinich/facter
# Clean up synthetic tap collection, just in case
rm -fr "${tapdir}"/test/homebrew-tap[0-9]*
cat >${tmpd}/clear_cache <<'EOF'
#!/usr/bin/env bash
case $(uname -s) in
Linux) echo 3 | sudo tee /proc/sys/vm/drop_caches ;;
Darwin) : ;; # TODO
*) echo "Unknown system";;
esac
EOF
cat >${tmpd}/old_find <<'EOF'
#!/usr/bin/env bash
formula=$1
if [ -f "$HOMEBREW_REPOSITORY/Library/Taps/homebrew/homebrew-core/Formula/${formula}.rb" ]; then
formula_path="$HOMEBREW_REPOSITORY/Library/Taps/homebrew/homebrew-core/Formula/${formula}.rb"
else
formula_path="$(find "$HOMEBREW_REPOSITORY/Library/Taps" -name "${formula}.rb" -print -quit)"
fi
[ -z "$formula_path" ] && exit 1
echo "$formula_path"
EOF
cat >${tmpd}/new_glob <<'EOF'
#!/usr/bin/env bash
formula=$1
if [ -f "$HOMEBREW_REPOSITORY/Library/Taps/homebrew/homebrew-core/Formula/${formula}.rb" ]; then
formula_path="$HOMEBREW_REPOSITORY/Library/Taps/homebrew/homebrew-core/Formula/${formula}.rb"
else
formula_path="$(
shopt -s nullglob
echo "$HOMEBREW_REPOSITORY/Library/Taps"/*/*/{Formula/,HomebrewFormula/,}"${formula}.rb" | head -n 1
)"
fi
[ -z "$formula_path" ] && exit 1
echo "$formula_path"
EOF
cat >${tmpd}/new_find <<'EOF'
#!/usr/bin/env bash
formula=$1
if [ -f "$HOMEBREW_REPOSITORY/Library/Taps/homebrew/homebrew-core/Formula/${formula}.rb" ]; then
formula_path="$HOMEBREW_REPOSITORY/Library/Taps/homebrew/homebrew-core/Formula/${formula}.rb"
else
formula_path="$(
find $HOMEBREW_REPOSITORY/Library/Taps \
-type f \( \
-regex "^$HOMEBREW_REPOSITORY/Library/Taps/[^/]*/[^/]*/Formula/${formula}\.rb\$" -o \
-regex "^$HOMEBREW_REPOSITORY/Library/Taps/[^/]*/[^/]*/HomebrewFormula/${formula}\.rb\$" -o \
-regex "^$HOMEBREW_REPOSITORY/Library/Taps/[^/]*/[^/]*/${formula}\.rb\$" \
\) \
-print -quit
)"
fi
[ -z "$formula_path" ] && exit 1
echo "$formula_path"
EOF
chmod +x ${tmpd}/*
sudo -v
# check <expected> <cmd> <arg>...
check() {
local expected=$1; shift
local result=$("$@")
if [[ $result == $expected ]]; then
echo "$@ : OK"
else
echo "$@ : FAIL (expected: '$expected', got: '$result')"
fi
}
echo "===== Running normal benchmark ====="
hyperfine -p "${tmpd}/clear_cache" -i --export-markdown /tmp/prefix.normal.md \
"${tmpd}/new_glob umka" \
"${tmpd}/old_find umka" \
"${tmpd}/new_find umka" \
"${tmpd}/new_glob endgame" \
"${tmpd}/old_find endgame" \
"${tmpd}/new_find endgame" \
"${tmpd}/new_glob facter" \
"${tmpd}/old_find facter" \
"${tmpd}/new_find facter" \
"${tmpd}/new_glob google-chrome" \
"${tmpd}/old_find google-chrome" \
"${tmpd}/new_find google-chrome" \
"${tmpd}/new_glob nonexistent" \
"${tmpd}/old_find nonexistent" \
"${tmpd}/new_find nonexistent"
# STRESS TEST: Synthetic tap collection with 30,000 formulae
echo "===== Building synthetic tap collection ====="
for tap in test/homebrew-tap{0..99}; do
mkdir -p $tap/Formula $tap/HomebrewFormula
for formula in f{0..99}; do
touch $tap/Formula/${formula}.rb $tap/HomebrewFormula/${formula}_hf.rb $tap/${formula}_root.rb
done
done
echo "===== Running stress test ====="
hyperfine -p "${tmpd}/clear_cache" -i --export-markdown /tmp/prefix.stress.md \
"${tmpd}/new_glob f42" \
"${tmpd}/old_find f42" \
"${tmpd}/new_find f42" \
"${tmpd}/new_glob f42_hf" \
"${tmpd}/old_find f42_hf" \
"${tmpd}/new_find f42_hf" \
"${tmpd}/new_glob f42_root" \
"${tmpd}/old_find f42_root" \
"${tmpd}/new_find f42_root"
echo "===== Running correctness test ====="
for cmd in old_find new_glob new_find; do
check "*/homebrew-dev/Formula/umka.rb" "${tmpd}/${cmd}" umka
check "*/homebrew-awsp/HomebrewFormula/endgame.rb" "${tmpd}/${cmd}" endgame
check "*/homebrew-facter/facter.rb" "${tmpd}/${cmd}" facter
check "" "${tmpd}/${cmd}" google-chrome
check "" "${tmpd}/${cmd}" nonexistent
check "*/homebrew-tap*/Formula/f42.rb" "${tmpd}/${cmd}" f42
check "*/homebrew-tap*/HomebrewFormula/f42_hf.rb" "${tmpd}/${cmd}" f42_hf
check "*/homebrew-tap*/f42_root.rb" "${tmpd}/${cmd}" f42_root
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment