add cpu profile option

This commit is contained in:
c9s 2021-05-18 15:38:22 +08:00
parent 9406682944
commit 34106cf65e

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime/pprof"
"syscall" "syscall"
"time" "time"
@ -26,6 +27,7 @@ func init() {
RunCmd.Flags().String("totp-account-name", "", "") RunCmd.Flags().String("totp-account-name", "", "")
RunCmd.Flags().Bool("enable-webserver", false, "enable webserver") RunCmd.Flags().Bool("enable-webserver", false, "enable webserver")
RunCmd.Flags().Bool("enable-web-server", false, "legacy option, this is renamed to --enable-webserver") RunCmd.Flags().Bool("enable-web-server", false, "legacy option, this is renamed to --enable-webserver")
RunCmd.Flags().String("cpu-profile", "", "cpu profile")
RunCmd.Flags().String("webserver-bind", ":8080", "webserver binding") RunCmd.Flags().String("webserver-bind", ":8080", "webserver binding")
RunCmd.Flags().Bool("setup", false, "use setup mode") RunCmd.Flags().Bool("setup", false, "use setup mode")
RootCmd.AddCommand(RunCmd) RootCmd.AddCommand(RunCmd)
@ -79,7 +81,6 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer
return nil return nil
} }
func BootstrapBacktestEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error { func BootstrapBacktestEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
if err := environ.ConfigureDatabase(ctx); err != nil { if err := environ.ConfigureDatabase(ctx); err != nil {
return err return err
@ -94,7 +95,6 @@ func BootstrapBacktestEnvironment(ctx context.Context, environ *bbgo.Environment
return nil return nil
} }
func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error { func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
if err := environ.ConfigureDatabase(ctx); err != nil { if err := environ.ConfigureDatabase(ctx); err != nil {
return err return err
@ -126,7 +126,7 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config, enableWebServer
return err return err
} }
if err := environ.Init(ctx) ; err != nil { if err := environ.Init(ctx); err != nil {
return err return err
} }
@ -203,6 +203,11 @@ func run(cmd *cobra.Command, args []string) error {
return err return err
} }
cpuProfile, err := cmd.Flags().GetString("cpu-profile")
if err != nil {
return err
}
var userConfig = &bbgo.Config{} var userConfig = &bbgo.Config{}
if !setup { if !setup {
@ -239,6 +244,20 @@ func run(cmd *cobra.Command, args []string) error {
return err return err
} }
if cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
return runConfig(ctx, userConfig, enableWebServer, webServerBind) return runConfig(ctx, userConfig, enableWebServer, webServerBind)
} }