Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
add pprof experimental cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsaraf committed Oct 25, 2020
1 parent ce1aed8 commit 12ac3ce
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cmd/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"runtime"
"runtime/debug"
"runtime/pprof"
"strings"
"time"

Expand Down Expand Up @@ -106,6 +107,8 @@ type inputs struct {
fixedIterations *uint64
noHeaders *bool
ui *bool
cpuProfile *string
memProfile *string
}

func validateCliParams(l logger.Logger, options inputs) {
Expand Down Expand Up @@ -162,6 +165,8 @@ func init() {
options.fixedIterations = tradeCmd.Flags().Uint64("iter", 0, "only run the bot for the first N iterations (defaults value 0 runs unboundedly)")
options.noHeaders = tradeCmd.Flags().Bool("no-headers", false, "do not use Amplitude or set X-App-Name and X-App-Version headers on requests to horizon")
options.ui = tradeCmd.Flags().Bool("ui", false, "indicates a bot that is started from the Kelp UI server")
options.cpuProfile = tradeCmd.Flags().String("cpuprofile", "", "write cpu profile to `file`")
options.memProfile = tradeCmd.Flags().String("memprofile", "", "write memory profile to `file`")

requiredFlag("botConf")
requiredFlag("strategy")
Expand All @@ -171,7 +176,42 @@ func init() {
tradeCmd.Flags().SortFlags = false

tradeCmd.Run = func(ccmd *cobra.Command, args []string) {
// TODO NS - profiling fails if we call os.Exit
if *options.cpuProfile != "" {
f, e := os.Create(*options.cpuProfile)
if e != nil {
log.Fatal("could not create CPU profile: ", e)
}
defer func() {
e := f.Close()
if e != nil {
log.Fatalf("could not close file: %s", e)
}
}()
if e := pprof.StartCPUProfile(f); e != nil {
log.Fatal("could not start CPU profile: ", e)
}
defer pprof.StopCPUProfile()
}

runTradeCmd(options)

if *options.memProfile != "" {
f, e := os.Create(*options.memProfile)
if e != nil {
log.Fatal("could not create memory profile: ", e)
}
defer func() {
e := f.Close()
if e != nil {
log.Fatalf("could not close file: %s", e)
}
}()
runtime.GC() // get up-to-date statistics
if e := pprof.WriteHeapProfile(f); e != nil {
log.Fatal("could not write memory profile: ", e)
}
}
}
}

Expand Down

0 comments on commit 12ac3ce

Please sign in to comment.