#!/bin/sh
set -e -u

rm -Rf ../../target/release/deps/benchmark-*
RUSTFLAGS='-g' cargo build --release --bench benchmark

FILE_TO_RUN=`find ../../target/release/deps -type f -perm +1 | grep benchmark`

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

# 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.
sudo flamegraph \
    $FILE_TO_RUN \
    --bench \
    --profile-time 60 \
    "$FILTER"

sudo chown $USER flamegraph.svg
mv flamegraph.svg flamegraph-$FILTER.svg
open flamegraph-$FILTER.svg
