#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
export GOMAXPROCS=2
export TZ=UTC
go version
go env GOOS GOARCH CGO_ENABLED
go test -count=1 -v ./...
go test -race -count=1 ./...
go vet ./...
go build -trimpath -ldflags='-X main.revision=demo-v1' -o "$tmp/demo" .
"$tmp/demo" -out "$tmp/profiles"
cat "$tmp/profiles/metadata.json"
echo '--- CPU ---'
go tool pprof -top -nodecount=7 "$tmp/demo" "$tmp/profiles/cpu.pprof"
echo '--- labels ---'
go tool pprof -tags "$tmp/demo" "$tmp/profiles/cpu.pprof"
echo '--- live heap ---'
go tool pprof -top -sample_index=inuse_space -nodecount=5 "$tmp/demo" "$tmp/profiles/heap.pprof"
echo '--- cumulative allocation ---'
go tool pprof -top -sample_index=alloc_space -nodecount=5 "$tmp/demo" "$tmp/profiles/heap.pprof"
echo '--- block ---'
go tool pprof -top -nodecount=5 "$tmp/demo" "$tmp/profiles/block.pprof"
echo '--- mutex ---'
go tool pprof -top -nodecount=5 "$tmp/demo" "$tmp/profiles/mutex.pprof"
echo '--- overwrite negative fixture ---'
set +e
"$tmp/demo" -out "$tmp/profiles"
code=$?
set -e
printf 'second_run_exit=%s\n' "$code"
test "$code" -eq 1
echo 'raw profiles and binary are removed by EXIT trap'
