Skip to content

Commit

Permalink
feat: add CPU profiling support in main function
Browse files Browse the repository at this point in the history
- Implemented conditional CPU profiling based on the BIRDNET_GO_PROFILE environment variable.
- Created a unique profile file with a timestamp for better traceability.
- Updated error handling for configuration loading to improve robustness.
  • Loading branch information
tphakala committed Dec 19, 2024
1 parent e727ac3 commit 1727e15
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"embed"
"fmt"
"os"
"runtime/pprof"
"time"

"github.com/spf13/viper"
"github.com/tphakala/birdnet-go/cmd"
Expand All @@ -21,19 +23,40 @@ var assetsFs embed.FS
var viewsFs embed.FS

func main() {
// Check if profiling is enabled
if os.Getenv("BIRDNET_GO_PROFILE") == "1" {
fmt.Println("Profiling enabled")
// Create a unique profile name with timestamp
now := time.Now()
profilePath := fmt.Sprintf("profile_%s.pprof", now.Format("20060102_150405"))

f, err := os.Create(profilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating profile file: %v\n", err)
os.Exit(1)
}
defer f.Close()

if err := pprof.StartCPUProfile(f); err != nil {
fmt.Fprintf(os.Stderr, "Error starting CPU profile: %v\n", err)
os.Exit(1)
}
defer pprof.StopCPUProfile()
}

// publish the embedded assets and views directories to controller package
httpcontroller.AssetsFs = assetsFs
httpcontroller.ViewsFs = viewsFs

// Load the configuration
settings, err := conf.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading configuration: %v\n", err)
settings := conf.Setting()
if settings == nil {
fmt.Fprintf(os.Stderr, "Error loading configuration\n")
os.Exit(1)
} else {
fmt.Printf("BirdNET-Go build date: %s, using config file: %s\n", buildDate, viper.ConfigFileUsed())
}

fmt.Printf("🐦 \033[37mBirdNET-Go build date: %s, using config file: %s\033[0m\n", buildDate, viper.ConfigFileUsed())

// Execute the root command
rootCmd := cmd.RootCommand(settings)
if err := rootCmd.Execute(); err != nil {
Expand Down

0 comments on commit 1727e15

Please sign in to comment.