#!/usr/bin/env nix-shell
#!nix-shell -i ysh -p flamegraph -p linuxPackages.perf -p oils-for-unix

# use like `sane-profile some-command --which-takes-a flag`.
# will render a .html file showing the hot-path functions inside the command.

var cmd = ARGV
var tmpdir = "/tmp"
perf record -F 9000 -e cycles:u -g -o "$tmpdir/perf.data" -- @cmd
perf script --input "$tmpdir/perf.data" > "$tmpdir/perf.script"
stackcollapse-perf.pl "$tmpdir/perf.script" | flamegraph.pl --width 2000 > "$tmpdir/flamegraph.html"

echo "flame graph available at $tmpdir/flamegraph.html"
