Skip to content

Commit

Permalink
feat(core): add performance profiler (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito authored Sep 4, 2024
1 parent 0713dee commit b6d99c4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@
core/**/main
core/**/bin
core/**/client

# Output of pprof files
**/*.prof
4 changes: 4 additions & 0 deletions core/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type ServerConfig struct {
TimeoutIdle time.Duration

// Misc settings.
// Enable /debug/pprof endpoints.
Profiler bool `env:"PROFILER"`
UseEnvironment bool
DemoMode bool `env:"DEMO_MODE"`

Expand Down Expand Up @@ -64,6 +66,7 @@ const (
DefaultLoggerLevel = "info"

// Misc constants.
DefaultProfiler = false
DefaultDemoMode = false
)

Expand All @@ -84,6 +87,7 @@ func NewServerConfig(useEnv bool, version string, commit string) (*ServerConfig,
TimeoutRead: DefaultTimeoutRead,
TimeoutWrite: DefaultTimeoutWrite,
TimeoutIdle: DefaultTimeoutIdle,
Profiler: DefaultProfiler,
UseEnvironment: useEnv,
DemoMode: DefaultDemoMode,
Version: version,
Expand Down
12 changes: 12 additions & 0 deletions core/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"strconv"
Expand Down Expand Up @@ -67,6 +68,7 @@ func (s *StartCommand) ParseFlags(args []string) error {
fs.StringVar(&s.AnalyticsDB.Host, "analyticsdb", s.AnalyticsDB.Host, "Path to analytics database.")

// Misc settings.
fs.BoolVar(&s.Server.Profiler, "profiler", s.Server.Profiler, "Enable debug profiling.")
fs.BoolVar(&s.Server.UseEnvironment, "env", false, "Opt-in to allow environment variables to be used for configuration. Flags will still override environment variables.")
fs.BoolVar(&s.Server.DemoMode, "demo", s.Server.DemoMode, "Enable demo mode restricting all POST/PATCH/DELETE actions (except login).")

Expand Down Expand Up @@ -150,6 +152,16 @@ func (s *StartCommand) Run(ctx context.Context) error {
mux.Handle("/openapi.yaml", http.FileServer(http.FS(generate.OpenAPIDocument)))
mux.Handle("/api/", http.StripPrefix("/api", apiHandler))

// Start CPU profiling if enabled.
if s.Server.Profiler {
log.Warn().Msg("Enabling debug profiler...")
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}

// SPA client.
err = services.SetupAssetHandler(mux, service.RuntimeConfig)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ APP_DATABASE_HOST = '/db/app.db'
PORT = '8080'
LEVEL = 'debug'
DEMO_MODE = 'true'
PROFILER = 'true'

[[mounts]]
source = 'medama_db'
Expand Down

0 comments on commit b6d99c4

Please sign in to comment.