Files
yellowjacket/backend/profiling/timing.go
T
logan 2a3a79652c add dev-only profiling with pprof, runtime/trace, and operation timing
Wire up Go's standard profiling toolkit so it's automatically available
in dev builds and completely absent from production. The profiling
package uses build tags (dev/!dev) to eliminate all pprof, trace, and
timing code from release binaries with zero new dependencies.

- backend/profiling: pprof HTTP server on :6060, /debug/trace endpoint,
  block/mutex profiling, and TimeOp helper for structured operation timing
- scripts/profile.sh: interactive menu-driven script that auto-selects
  free ports (8080-8089) so multiple profiles can be open simultaneously
- Instrumented key operations: app init, database init, player load/restore,
  queue set/restore
- Makefile targets: profile, profile-cpu, profile-heap, profile-trace
- .gitignore: exclude trace-*.out and *.pprof artifacts
2026-02-20 18:27:25 -06:00

30 lines
646 B
Go

//go:build dev
package profiling
import (
"log/slog"
"time"
)
// TimeOp starts a timer and returns a function that, when called, logs the
// elapsed duration. Intended for use with defer:
//
// defer profiling.TimeOp(logger, "database.Init")()
//
// The extra () is required — defer evaluates the outer call immediately
// (capturing the start time) and defers the returned closure.
func TimeOp(logger *slog.Logger, operation string) func() {
start := time.Now()
logger.Debug("operation started", "op", operation)
return func() {
logger.Info(
"operation completed",
"op", operation,
"duration", time.Since(start),
)
}
}