Skip to content

Commit

Permalink
add pprof http server support with a build tag switch
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Nov 15, 2024
1 parent 56fdf0a commit 2e39200
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkg/cmd/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build pprof
// +build pprof

package cmd

import _ "net/http/pprof"
23 changes: 21 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime/pprof"
"strings"
"time"
_ "time/tzdata"

"github.com/heroku/rollrus"
"github.com/joho/godotenv"
Expand All @@ -20,8 +21,6 @@ import (

"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/util"

_ "time/tzdata"
)

var cpuProfileFile *os.File
Expand Down Expand Up @@ -81,6 +80,24 @@ var RootCmd = &cobra.Command{
}()
}

enableProfileServer, err := cmd.Flags().GetBool("enable-profile-server")
if err != nil {
return err
}

if enableProfileServer {
profileServerBind, err := cmd.Flags().GetString("profile-server-bind")
if err != nil {
return err
}

go func() {
if err := http.ListenAndServe(profileServerBind, nil); err != nil {
log.WithError(err).Errorf("profile server error")
}
}()
}

cpuProfile, err := cmd.Flags().GetString("cpu-profile")
if err != nil {
return err
Expand Down Expand Up @@ -195,6 +212,8 @@ func init() {
RootCmd.PersistentFlags().String("max-api-secret", "", "max api secret")

RootCmd.PersistentFlags().String("cpu-profile", "", "cpu profile")
RootCmd.PersistentFlags().Bool("enable-profile-server", false, "enable profile server binding")
RootCmd.PersistentFlags().String("profile-server-bind", "localhost:6060", "profile server binding")

viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

Expand Down

0 comments on commit 2e39200

Please sign in to comment.