From 58bd0a301bbffed3ea4b4eb06c7a4d32ccd54c67 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 14 Mar 2019 14:56:30 +1100 Subject: [PATCH] Added config option to enable pprof (#253) * Added config option to enable pprof * fixed typo in Makefile --- Makefile | 12 +++++++++++- config/config.go | 5 +++++ config_example.json | 3 +++ restful_router.go | 8 ++++++++ testdata/configtest.json | 3 +++ 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 693a5bf1..01801fe0 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,8 @@ LDFLAGS = -ldflags "-w -s" GCTPKG = github.com/thrasher-/gocryptotrader LINTPKG = github.com/golangci/golangci-lint/cmd/golangci-lint@v1.15.0 LINTBIN = $(GOPATH)/bin/golangci-lint +GCTLISTENPORT=9050 +GCTPROFILERLISTENPORT=8085 get: GO111MODULE=on go get $(GCTPKG) @@ -29,4 +31,12 @@ update_deps: GO111MODULE=on go mod verify GO111MODULE=on go mod tidy rm -rf vendor - GO111MODULE=on go mod vendor \ No newline at end of file + GO111MODULE=on go mod vendor + +.PHONY: profile_heap +profile_heap: + go tool pprof -http "localhost:$(GCTPROFILERLISTENPORT)" 'http://localhost:$(GCTLISTENPORT)/debug/pprof/heap' + +.PHONY: profile_cpu +profile_cpu: + go tool pprof -http "localhost:$(GCTPROFILERLISTENPORT)" 'http://localhost:$(GCTLISTENPORT)/debug/pprof/profile' \ No newline at end of file diff --git a/config/config.go b/config/config.go index 8e651c93..9df67e67 100644 --- a/config/config.go +++ b/config/config.go @@ -104,6 +104,7 @@ type Config struct { EncryptConfig int `json:"encryptConfig"` GlobalHTTPTimeout time.Duration `json:"globalHTTPTimeout"` Logging log.Logging `json:"logging"` + Profiler ProfilerConfig `json:"profiler"` Currency CurrencyConfig `json:"currencyConfig"` Communications CommunicationsConfig `json:"communications"` Portfolio portfolio.Base `json:"portfolioAddresses"` @@ -118,6 +119,10 @@ type Config struct { SMS *SMSGlobalConfig `json:"smsGlobal,omitempty"` } +type ProfilerConfig struct { + Enabled bool `json:"enabled"` +} + // ExchangeConfig holds all the information needed for each enabled Exchange. type ExchangeConfig struct { Name string `json:"name"` diff --git a/config_example.json b/config_example.json index 945532d7..10d954ac 100644 --- a/config_example.json +++ b/config_example.json @@ -9,6 +9,9 @@ "level": "DEBUG|WARN|INFO|ERROR|FATAL", "rotate": false }, + "profiler": { + "enabled": false + }, "currencyConfig": { "forexProviders": [ { diff --git a/restful_router.go b/restful_router.go index 6b8e2323..14b94c11 100644 --- a/restful_router.go +++ b/restful_router.go @@ -7,6 +7,8 @@ import ( "github.com/gorilla/mux" log "github.com/thrasher-/gocryptotrader/logger" + + _ "net/http/pprof" ) // RESTLogger logs the requests internally @@ -114,6 +116,12 @@ func NewRouter() *mux.Router { Name(route.Name). Handler(RESTLogger(route.HandlerFunc, route.Name)) } + + if bot.config.Profiler.Enabled { + log.Debugln("Profiler enabled") + router.PathPrefix("/debug").Handler(http.DefaultServeMux) + } + return router } diff --git a/testdata/configtest.json b/testdata/configtest.json index 19f29659..4134ac9a 100644 --- a/testdata/configtest.json +++ b/testdata/configtest.json @@ -9,6 +9,9 @@ "level": "DEBUG|WARN|INFO|ERROR|FATAL", "rotate": true }, + "profiler": { + "enabled": false + }, "currencyConfig": { "forexProviders": [ {