Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

框架没有接入pprof 进行性能剖析吗? #806

Closed
hubyao opened this issue Jul 7, 2021 · 18 comments
Closed

框架没有接入pprof 进行性能剖析吗? #806

hubyao opened this issue Jul 7, 2021 · 18 comments

Comments

@hubyao
Copy link

hubyao commented Jul 7, 2021

框架没有接入pprof 进行性能剖析,需要自己进行接入

@hubyao hubyao changed the title 框架没有接人pprof 进行性能剖析 框架没有接入pprof 进行性能剖析吗? Jul 7, 2021
@zljohn-ux
Copy link

木有,hc也需要手动

@hubyao hubyao closed this as completed Jul 26, 2021
@jsonMark
Copy link
Contributor

jsonMark commented Mar 3, 2022

木有,hc也需要手动

同求!

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


No, hc also needs to be manually

Same request!

@miaogaolin
Copy link
Contributor

miaogaolin commented May 30, 2022

go-zero 如何接入 pprof 工具,我这简单总结了下:
https://laomiao.site/go-zero-%E5%A6%82%E4%BD%95%E6%8E%A5%E5%85%A5-pprof

image

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


How go-zero accesses the pprof tool, I briefly summarize it:
https://laomiao.site/go-zero-%E5%A6%82%E4%BD%95%E6%8E%A5%E5%85%A5-pprof

@kevwan
Copy link
Contributor

kevwan commented May 30, 2022

有内置的,看 core/prof

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


There are built-in ones, see core/prof package

@miaogaolin
Copy link
Contributor

有内置的,看 core/prof

好的,我看看,谢谢

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


has built-in, see core/prof package

ok, i'll take a look, thanks

@jsonMark
Copy link
Contributor

go-zero 如何接入 pprof 工具,我这简单总结了下: https://laomiao.site/go-zero-%E5%A6%82%E4%BD%95%E6%8E%A5%E5%85%A5-pprof
楼主方便放一个完整demo吗?
下面这些代码
defer server.Stop()
server.Start()
需要注释掉吗?

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


How go-zero accesses the pprof tool, I briefly summarize: https://laomiao.site/go-zero-%E5%A6%82%E4%BD%95%E6%8E%A5%E5% 85%A5-pprof
Is it convenient for the landlord to put a complete demo?
the following codes
defer server.Stop()
server.Start()
Need to comment out?

@miaogaolin
Copy link
Contributor

go-zero 如何接入 pprof 工具,我这简单总结了下: https://laomiao.site/go-zero-%E5%A6%82%E4%BD%95%E6%8E%A5%E5%85%A5-pprof
楼主方便放一个完整demo吗?
下面这些代码
defer server.Stop()
server.Start()
需要注释掉吗?

这个就不需要了,删掉

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


How go-zero accesses the pprof tool, I briefly summarize: https://laomiao.site/go-zero-%E5%A6%82%E4%BD%95%E6%8E%A5%E5 %85%A5-pprof
Is it convenient for the landlord to put a complete demo?
the following code
defer server.Stop()
server.Start()
Need to comment out?

This is not needed, delete it

@miaogaolin
Copy link
Contributor

image

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


image

@jsonMark
Copy link
Contributor

//我根据博主文档整理一份完整demo,goctl1.3.5生成的API
package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/service"
	"log"
	"net/http"

	"zero-api/service/gateway/web/internal/config"
	"zero-api/service/gateway/web/internal/handler"
	"zero-api/service/gateway/web/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "etc/gateway-api.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	//新增代码开始***********//
	svcGroup := service.NewServiceGroup()
	defer svcGroup.Stop()
	//新增代码结束***********//

	ctx := svc.NewServiceContext(c)
	server := rest.MustNewServer(c.RestConf)

	//新增代码开始***********//
	svcGroup.Add(server)
	//新增代码结束***********//

	//注释原有代码
	//defer server.Stop()

	handler.RegisterHandlers(server, ctx)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)

	//注释原有代码
	//server.Start()

	//新增代码开始***********//
	svcGroup.Add(pprofServer{})
	svcGroup.Start()
	//新增代码结束***********//
}

//新增代码开始***********//
type pprofServer struct{}

func (pprofServer) Start() {
	addr := "127.0.0.1:39599"
	fmt.Printf("Start pprof server, listen addr %s\n", addr)
	err := http.ListenAndServe(addr, nil)
	if err != nil {
		log.Fatal(err)
	}
}

func (pprofServer) Stop() {
	fmt.Printf("Stop pprof server\n")
}
//新增代码结束***********//


//goctl1.3.5官方模版生成的代码
func main02() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	ctx := svc.NewServiceContext(c)
	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	handler.RegisterHandlers(server, ctx)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}

@maojinhua
Copy link

core/prof

你好,使用kill -usr2 后 打不开trace.pprof文件,其他文件可以打开

go tool pprof core-1-trace-1201111253.pprof
core-1-trace-1201111253.pprof: parsing profile: unrecognized profile format
failed to fetch any source profiles

@huge-kumo
Copy link

我是这样实现的,创建/ProjectName/api/internal/handler/monitor.go文件,然后在里面增加pprof相关的Handler,最后在main里面handler.RegisterMonitorHandler(server, ctx)导入就好啦。

package handler

import (
	"github.com/zeromicro/go-zero/rest"
	"net/http"
	"net/http/pprof"
	"ticket_saas/api/internal/svc"
)

func RegisterMonitorHandler(server *rest.Server, serverCtx *svc.ServiceContext) {
	server.AddRoutes(
		rest.WithMiddlewares(
			[]rest.Middleware{},
			[]rest.Route{
				{
					Method:  http.MethodGet,
					Path:    "/debug/pprof",
					Handler: pprof.Index,
				},
				{
					Method:  http.MethodGet,
					Path:    "/debug/allocs",
					Handler: pprof.Handler("allocs").ServeHTTP,
				},
				{
					Method:  http.MethodGet,
					Path:    "/debug/block",
					Handler: pprof.Handler("block").ServeHTTP,
				},
				{
					Method:  http.MethodGet,
					Path:    "/debug/cmdline",
					Handler: pprof.Handler("cmdline").ServeHTTP,
				},
				{
					Method:  http.MethodGet,
					Path:    "/debug/goroutine",
					Handler: pprof.Handler("goroutine").ServeHTTP,
				},
				{
					Method:  http.MethodGet,
					Path:    "/debug/heap",
					Handler: pprof.Handler("heap").ServeHTTP,
				},
				{
					Method:  http.MethodGet,
					Path:    "/debug/mutex",
					Handler: pprof.Handler("mutex").ServeHTTP,
				},
				{
					Method:  http.MethodGet,
					Path:    "/debug/profile",
					Handler: pprof.Handler("profile").ServeHTTP,
				},
				{
					Method:  http.MethodGet,
					Path:    "/debug/threadcreate",
					Handler: pprof.Handler("threadcreate").ServeHTTP,
				},
				{
					Method:  http.MethodGet,
					Path:    "/debug/trace",
					Handler: pprof.Handler("trace").ServeHTTP,
				},

				{
					Method:  http.MethodGet,
					Path:    "/debug/symbol",
					Handler: pprof.Symbol,
				},
				{
					Method:  http.MethodGet,
					Path:    "/debug/vars",
					Handler: http.DefaultServeMux.ServeHTTP,
				},
			}...,
		),
	)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants