From dbb5a7ddc114b29520e2f313112650582db84393 Mon Sep 17 00:00:00 2001 From: Andrew Jackson Date: Wed, 13 Mar 2019 14:57:56 +1100 Subject: [PATCH 1/2] Added config option to enable pprof --- 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 693a5bf11ac..b456ab4ce9f 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/cpu' \ No newline at end of file diff --git a/config/config.go b/config/config.go index 8e651c934e9..9df67e67a5a 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 945532d709e..10d954ac27c 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 6b8e2323bfe..14b94c113d0 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 19f2965913b..4134ac9a526 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": [ { From aca43c07d2ffdd51d020206673a8dda00a22efe8 Mon Sep 17 00:00:00 2001 From: Andrew Jackson Date: Thu, 14 Mar 2019 08:43:59 +1100 Subject: [PATCH 2/2] fixed typo in Makefile --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index b456ab4ce9f..01801fe0788 100644 --- a/Makefile +++ b/Makefile @@ -38,5 +38,5 @@ 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/cpu' \ No newline at end of file +profile_cpu: + go tool pprof -http "localhost:$(GCTPROFILERLISTENPORT)" 'http://localhost:$(GCTLISTENPORT)/debug/pprof/profile' \ No newline at end of file