advent-of-code 2022.0.66

Solutions to Advent of Code
Documentation
#!/bin/sh
set -e -u

# rm -Rf ../../target/release/deps/benchmark-*

# See CARGO_PROFILE_RELEASE_DEBUG=true for why CARGO_PROFILE_RELEASE_DEBUG=true
CARGO_PROFILE_BENCH_DEBUG=true CARGO_PROFILE_RELEASE_DEBUG=true cargo build --release --bench benchmark
# RUSTFLAGS='-g'

FILE_TO_RUN=`find ../../target/release/deps -type f -perm u=rwx | grep benchmark | grep -v iai`

FILTER=$1
echo "Running benchmark with filter $FILTER"

AS_ROOT=0
FLAMEGRAPH=flamegraph
if [ `uname` = Darwin ]; then
    FLAMEGRAPH="sudo flamegraph"
    AS_ROOT=1
fi

# https://bheisler.github.io/criterion.rs/book/user_guide/command_line_options.html
# --profile-time:
# To iterate each benchmark for a fixed length of time without saving, analyzing or
# plotting the results, use cargo bench -- --profile-time <num_seconds>. This is
# useful when profiling the benchmarks. It reduces the amount of unrelated clutter
# in the profiling results and prevents Criterion.rs' normal dynamic sampling logic
# from greatly increasing the runtime of the benchmarks.
$FLAMEGRAPH \
    $FILE_TO_RUN \
    --bench \
    --profile-time 20 \
    "$FILTER"

if [ $AS_ROOT = 1 ]; then
  sudo chown $USER flamegraph.svg
fi

cp flamegraph.svg flamegraph-$FILTER.svg

if [ `which open` &> /dev/null ]; then
  open flamegraph-$FILTER.svg
fi