#!/usr/bin/env bash
# {{ display_name }} MCP server — generated by mcpify. Do not hand-edit.
set -euo pipefail
cd "$(dirname "$0")/.."

# `go test -cpuprofile`/`-memprofile` only accept a single package at a
# time, so profile each package separately and merge the results with
# `go tool pprof` (which merges when given multiple profile files).
mkdir -p profile
rm -f profile/*.cpu.prof profile/*.mem.prof
for pkg in $(go list ./...); do
  safe="$(echo "$pkg" | tr '/' '_')"
  go test -bench=. -cpuprofile="profile/${safe}.cpu.prof" -memprofile="profile/${safe}.mem.prof" "$pkg" >/dev/null 2>&1 || true
done

go tool pprof -top profile/*.cpu.prof > profile/cpu-top.txt
go tool pprof -top profile/*.mem.prof > profile/mem-top.txt

{
  echo "# Generated server bottleneck report"
  echo
  echo "Generated $(date -u +%Y-%m-%dT%H:%M:%SZ)."
  echo
  if [ -f coverage.out ]; then
    echo "## Coverage summary"
    echo
    echo '```'
    go tool cover -func=coverage.out | tail -20
    echo '```'
    echo
  fi
  echo "## Hottest functions (CPU)"
  echo
  echo '```'
  tail -n +5 profile/cpu-top.txt | head -20
  echo '```'
  echo
  echo "## Hottest allocators (memory)"
  echo
  echo '```'
  tail -n +5 profile/mem-top.txt | head -20
  echo '```'
} > profile/bottleneck-report.md

echo
echo "Profile written to:"
echo "  profile/*.cpu.prof, profile/*.mem.prof (interactive — go tool pprof -http=:8080 profile/*.cpu.prof)"
echo "  profile/cpu-top.txt, profile/mem-top.txt (LLM/tool-ingestable)"
echo "  profile/bottleneck-report.md         (combined coverage + hot-path summary)"
