move cpu profile option to global cmd

This commit is contained in:
c9s 2022-06-09 15:49:52 +08:00
parent 8d3f487d0d
commit f8dbd26736
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 31 additions and 3 deletions

View File

@ -127,7 +127,6 @@ var BacktestCmd = &cobra.Command{
return err return err
} }
if userConfig.Backtest == nil { if userConfig.Backtest == nil {
return errors.New("backtest config is not defined") return errors.New("backtest config is not defined")
} }

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"runtime/pprof"
"strings" "strings"
"time" "time"
@ -22,6 +23,8 @@ import (
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
var cpuProfileFile *os.File
var userConfig *bbgo.Config var userConfig *bbgo.Config
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
@ -32,7 +35,7 @@ var RootCmd = &cobra.Command{
SilenceUsage: true, SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := cobraLoadDotenv(cmd, args) ; err != nil { if err := cobraLoadDotenv(cmd, args); err != nil {
return err return err
} }
@ -53,8 +56,34 @@ var RootCmd = &cobra.Command{
}() }()
} }
cpuProfile, err := cmd.Flags().GetString("cpu-profile")
if err != nil {
return err
}
if cpuProfile != "" {
log.Infof("starting cpu profiler...")
cpuProfileFile, err = os.Create(cpuProfile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(cpuProfileFile); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
}
return cobraLoadConfig(cmd, args) return cobraLoadConfig(cmd, args)
}, },
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
pprof.StopCPUProfile()
if cpuProfileFile != nil {
return cpuProfileFile.Close() // error handling omitted for example
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return nil return nil
}, },
@ -139,6 +168,7 @@ func init() {
RootCmd.PersistentFlags().String("ftx-api-key", "", "ftx api key") RootCmd.PersistentFlags().String("ftx-api-key", "", "ftx api key")
RootCmd.PersistentFlags().String("ftx-api-secret", "", "ftx api secret") RootCmd.PersistentFlags().String("ftx-api-secret", "", "ftx api secret")
RootCmd.PersistentFlags().String("ftx-subaccount", "", "subaccount name. Specify it if the credential is for subaccount.") RootCmd.PersistentFlags().String("ftx-subaccount", "", "subaccount name. Specify it if the credential is for subaccount.")
RootCmd.PersistentFlags().String("cpu-profile", "", "cpu profile")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

View File

@ -34,7 +34,6 @@ func init() {
RunCmd.Flags().Bool("enable-grpc", false, "enable grpc server") RunCmd.Flags().Bool("enable-grpc", false, "enable grpc server")
RunCmd.Flags().String("grpc-bind", ":50051", "grpc server binding") RunCmd.Flags().String("grpc-bind", ":50051", "grpc server binding")
RunCmd.Flags().String("cpu-profile", "", "cpu profile")
RunCmd.Flags().Bool("setup", false, "use setup mode") RunCmd.Flags().Bool("setup", false, "use setup mode")
RootCmd.AddCommand(RunCmd) RootCmd.AddCommand(RunCmd)
} }