From c5de53717f0d562e2aba2c3fc38a09a88418cb47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B0=E4=BA=AE?= Date: Wed, 23 Oct 2019 19:02:57 +0800 Subject: [PATCH] add pprof --- go.mod | 1 + go.sum | 2 + main.go | 4 + .../github.com/gin-contrib/pprof/.gitignore | 25 ++++++ .../github.com/gin-contrib/pprof/.travis.yml | 33 ++++++++ vendor/github.com/gin-contrib/pprof/LICENSE | 21 +++++ vendor/github.com/gin-contrib/pprof/README.md | 81 +++++++++++++++++++ vendor/github.com/gin-contrib/pprof/go.mod | 6 ++ vendor/github.com/gin-contrib/pprof/go.sum | 39 +++++++++ vendor/github.com/gin-contrib/pprof/pprof.go | 51 ++++++++++++ vendor/modules.txt | 2 + 11 files changed, 265 insertions(+) create mode 100644 vendor/github.com/gin-contrib/pprof/.gitignore create mode 100644 vendor/github.com/gin-contrib/pprof/.travis.yml create mode 100644 vendor/github.com/gin-contrib/pprof/LICENSE create mode 100644 vendor/github.com/gin-contrib/pprof/README.md create mode 100644 vendor/github.com/gin-contrib/pprof/go.mod create mode 100644 vendor/github.com/gin-contrib/pprof/go.sum create mode 100644 vendor/github.com/gin-contrib/pprof/pprof.go diff --git a/go.mod b/go.mod index 91ad0687..9f1a300c 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.12 require ( github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect + github.com/gin-contrib/pprof v1.2.1 github.com/gin-gonic/gin v1.4.0 github.com/go-playground/locales v0.12.1 // indirect github.com/go-playground/universal-translator v0.16.0 // indirect diff --git a/go.sum b/go.sum index e19dd75c..83555e3d 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gin-contrib/pprof v1.2.1 h1:p6mY/FsE3tDx2+Wp3ksrMe1QL5egQ7p+lsZ7WbQLT1U= +github.com/gin-contrib/pprof v1.2.1/go.mod h1:u2l4P4YNkLXYz+xBbrl7Pxu1Btng6VCD7j3O3pUPP2w= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ= diff --git a/main.go b/main.go index 48f398ab..3b52e53f 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "github.com/gin-contrib/pprof" "github.com/gin-gonic/gin" "go-gin-api/app/config" "go-gin-api/app/route" @@ -17,6 +18,9 @@ func main() { gin.SetMode(config.AppMode) engine := gin.New() + // 性能分析 - 正式环境不要使用!!! + pprof.Register(engine) + // 设置路由 route.SetupRouter(engine) diff --git a/vendor/github.com/gin-contrib/pprof/.gitignore b/vendor/github.com/gin-contrib/pprof/.gitignore new file mode 100644 index 00000000..b56be2d0 --- /dev/null +++ b/vendor/github.com/gin-contrib/pprof/.gitignore @@ -0,0 +1,25 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof +coverage* diff --git a/vendor/github.com/gin-contrib/pprof/.travis.yml b/vendor/github.com/gin-contrib/pprof/.travis.yml new file mode 100644 index 00000000..f5222e27 --- /dev/null +++ b/vendor/github.com/gin-contrib/pprof/.travis.yml @@ -0,0 +1,33 @@ +language: go +sudo: false + +matrix: + fast_finish: true + include: + - go: 1.10.x + - go: 1.11.x + env: GO111MODULE=on + - go: 1.12.x + env: GO111MODULE=on + - go: master + env: GO111MODULE=on + +git: + depth: 10 + +install: + - if [[ "${GO111MODULE}" = "on" ]]; then go mod download; else go get -t -v ./...; fi + +script: + - go test -v -covermode=atomic -coverprofile=coverage.out + +after_success: + - bash <(curl -s https://codecov.io/bash) + +notifications: + webhooks: + urls: + - https://webhooks.gitter.im/e/acc2c57482e94b44f557 + on_success: change + on_failure: always + on_start: false diff --git a/vendor/github.com/gin-contrib/pprof/LICENSE b/vendor/github.com/gin-contrib/pprof/LICENSE new file mode 100644 index 00000000..4e2cfb01 --- /dev/null +++ b/vendor/github.com/gin-contrib/pprof/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Gin-Gonic + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/gin-contrib/pprof/README.md b/vendor/github.com/gin-contrib/pprof/README.md new file mode 100644 index 00000000..4976e761 --- /dev/null +++ b/vendor/github.com/gin-contrib/pprof/README.md @@ -0,0 +1,81 @@ +# pprof + +[![Build Status](https://travis-ci.org/gin-contrib/pprof.svg)](https://travis-ci.org/gin-contrib/pprof) +[![codecov](https://codecov.io/gh/gin-contrib/pprof/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/pprof) +[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/pprof)](https://goreportcard.com/report/github.com/gin-contrib/pprof) +[![GoDoc](https://godoc.org/github.com/gin-contrib/pprof?status.svg)](https://godoc.org/github.com/gin-contrib/pprof) +[![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin) + +gin pprof middleware + +> Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool. + +## Usage + +### Start using it + +Download and install it: + +```bash +go get github.com/gin-contrib/pprof +``` + +Import it in your code: + +```go +import "github.com/gin-contrib/pprof" +``` + +### Example + +```go +package main + +import ( + "github.com/gin-contrib/pprof" + "github.com/gin-gonic/gin" +) + +func main() { + router := gin.Default() + pprof.Register(router) + router.Run(":8080") +} +``` + +### change default path prefix + +```go +func main() { + router := gin.Default() + // default is "debug/pprof" + pprof.Register(router, "dev/pprof") + router.Run(":8080") +} +``` + +### Use the pprof tool + +Then use the pprof tool to look at the heap profile: + +```bash +go tool pprof http://localhost:8080/debug/pprof/heap +``` + +Or to look at a 30-second CPU profile: + +```bash +go tool pprof http://localhost:8080/debug/pprof/profile +``` + +Or to look at the goroutine blocking profile, after calling runtime.SetBlockProfileRate in your program: + +```bash +go tool pprof http://localhost:8080/debug/pprof/block +``` + +Or to collect a 5-second execution trace: + +```bash +wget http://localhost:8080/debug/pprof/trace?seconds=5 +``` diff --git a/vendor/github.com/gin-contrib/pprof/go.mod b/vendor/github.com/gin-contrib/pprof/go.mod new file mode 100644 index 00000000..440d4d2a --- /dev/null +++ b/vendor/github.com/gin-contrib/pprof/go.mod @@ -0,0 +1,6 @@ +module github.com/gin-contrib/pprof + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gin-gonic/gin v1.4.0 +) diff --git a/vendor/github.com/gin-contrib/pprof/go.sum b/vendor/github.com/gin-contrib/pprof/go.sum new file mode 100644 index 00000000..a48bdd07 --- /dev/null +++ b/vendor/github.com/gin-contrib/pprof/go.sum @@ -0,0 +1,39 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= +github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= +github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ= +github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM= +github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= +github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c h1:uOCk1iQW6Vc18bnC13MfzScl+wdKBmM9Y9kU7Z83/lw= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= +gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= +gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= +gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/vendor/github.com/gin-contrib/pprof/pprof.go b/vendor/github.com/gin-contrib/pprof/pprof.go new file mode 100644 index 00000000..7cf185ee --- /dev/null +++ b/vendor/github.com/gin-contrib/pprof/pprof.go @@ -0,0 +1,51 @@ +package pprof + +import ( + "net/http" + "net/http/pprof" + + "github.com/gin-gonic/gin" +) + +const ( + // DefaultPrefix url prefix of pprof + DefaultPrefix = "/debug/pprof" +) + +func getPrefix(prefixOptions ...string) string { + prefix := DefaultPrefix + if len(prefixOptions) > 0 { + prefix = prefixOptions[0] + } + return prefix +} + +// Register the standard HandlerFuncs from the net/http/pprof package with +// the provided gin.Engine. prefixOptions is a optional. If not prefixOptions, +// the default path prefix is used, otherwise first prefixOptions will be path prefix. +func Register(r *gin.Engine, prefixOptions ...string) { + prefix := getPrefix(prefixOptions...) + + prefixRouter := r.Group(prefix) + { + prefixRouter.GET("/", pprofHandler(pprof.Index)) + prefixRouter.GET("/cmdline", pprofHandler(pprof.Cmdline)) + prefixRouter.GET("/profile", pprofHandler(pprof.Profile)) + prefixRouter.POST("/symbol", pprofHandler(pprof.Symbol)) + prefixRouter.GET("/symbol", pprofHandler(pprof.Symbol)) + prefixRouter.GET("/trace", pprofHandler(pprof.Trace)) + prefixRouter.GET("/allocs", pprofHandler(pprof.Handler("allocs").ServeHTTP)) + prefixRouter.GET("/block", pprofHandler(pprof.Handler("block").ServeHTTP)) + prefixRouter.GET("/goroutine", pprofHandler(pprof.Handler("goroutine").ServeHTTP)) + prefixRouter.GET("/heap", pprofHandler(pprof.Handler("heap").ServeHTTP)) + prefixRouter.GET("/mutex", pprofHandler(pprof.Handler("mutex").ServeHTTP)) + prefixRouter.GET("/threadcreate", pprofHandler(pprof.Handler("threadcreate").ServeHTTP)) + } +} + +func pprofHandler(h http.HandlerFunc) gin.HandlerFunc { + handler := http.HandlerFunc(h) + return func(c *gin.Context) { + handler.ServeHTTP(c.Writer, c.Request) + } +} diff --git a/vendor/modules.txt b/vendor/modules.txt index bd07ca7a..08aefee1 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,3 +1,5 @@ +# github.com/gin-contrib/pprof v1.2.1 +github.com/gin-contrib/pprof # github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 github.com/gin-contrib/sse # github.com/gin-gonic/gin v1.4.0