Go 代码的测试方法

性能测试

注册 pprof 服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 // 开启服务
pprofOpen := c.GetBool("pprof.open")
if pprofOpen {
// 运行pprof服务器,其路由已在pprof包自动注册
pprofPort := c.GetInt("pprof.port")
s.pprofSvr = &http.Server{
Addr: ":" + strconv.Itoa(pprofPort),
Handler: nil,
}
go func() {
err := s.pprofSvr.ListenAndServe()
log.Infof("pprof server down: %+v", err)
}()
}

// 停止服务
c := config.GetConfig()
pprofOpen := c.GetBool("pprof.open")
if pprofOpen {
wg.Add(1)
go func() {
defer wg.Done()
// 开始关闭pprof服务器
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.pprofSvr.Shutdown(ctx); err != nil {
log.Infof("Server Shutdown: ", err)
}
}()
}
wg.Wait()
1
2
go tool pprof --seconds 60 http://127.0.0.1:8098/debug/pprof/profile
>> web
1
2
3
[pprof]
open = true
port = 8098