成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何使用 Go 性能工具

開發 后端
從開發到部署的整個過程都離不開基本的負載測試和性能剖析,利用 Go 的 pprof 和跟蹤工具,開發人員可以深入了解性能瓶頸、CPU 使用率和內存分配情況。

在開發過程中,從一開始到應用程序的推出都充滿了挑戰,而負載測試則是其中至關重要的一項工作。這一階段不僅僅是走過場,而是要進行嚴格的評估,確保應用程序的性能符合預期。在測試過程中發現性能不佳或意外瓶頸是常見的障礙,但這也是改進的關鍵時刻。

有了合適的剖析工具,開發人員就能迅速從診斷過渡到有針對性的改進,避免漫無目的的故障排除帶來的隱患。這種簡化的方法不僅節省了寶貴的開發時間,還強調了負載測試和剖析作為追求最佳應用程序性能不可或缺的工具的重要性。

Go 從一開始就配備了出色的工具,幫助開發人員進行負載測試。在參與過使用 Elixir 的項目后,Erlang 中大量的性能測量工具給我留下了深刻印象,而 Go 提供的工具同樣令人印象深刻。本文將探討如何使用這些工具。

一、pprof

pprof 是 Go 專用的剖析器,可輕松確定 CPU 時間用在哪里,內存分配在哪里。獲取配置文件的方法因命令行應用程序和網絡應用程序而異。

1.cpu pprof

對于 CPU 剖析,可在感興趣的代碼段前后調用 pprof.StartCPUProfile() 和 pprof.StopCPUProfile(),并將輸出保存到指定文件(在示例中為 $TMPDIR)。

package main

import (
 "fmt"
 "os"
 "path"
 "runtime/pprof"
)

func main() {
 // Create a file to save the measurement results
 fname := path.Join(os.TempDir(), "cpuprofile.out")
 cpuFile, err := os.Create(fname)

 if err != nil {
  fmt.Println(err)
  return
 }

 // Conduct the measurement pprof.StartCPUProfile(cpuFile)
 defer pprof.StopCPUProfile()

 // Perform a heavy operation
}

執行上述代碼并生成 cpuprofile.out 輸出后,可以將其加載到 pprof 中查看剖析信息。

> go tool pprof /pathtotmpdir/cpuprofile.out   
Type: cpu
Time: Dec 17, 2022 at 7:40am (JST)
Duration: 606.44ms, Total samples = 390ms (64.31%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof)

運行提供的代碼并將輸出加載到 pprof 中,可以檢查配置文件信息。輸入 "top "可顯示十大耗時條目。

(pprof) top
Showing nodes accounting for 390ms, 100% of 390ms total
      flat  flat%   sum%        cum   cum%
     380ms 97.44% 97.44%      390ms   100%  main.Prime (inline)
      10ms  2.56%   100%       10ms  2.56%  runtime.asyncPreempt
         0     0%   100%      390ms   100%  main.main
         0     0%   100%      390ms   100%  runtime.main
(pprof)

-cum選項可檢索每個函數的累計時間。

(pprof) top -cum
Showing nodes accounting for 390ms, 100% of 390ms total
      flat  flat%   sum%        cum   cum%
     380ms 97.44% 97.44%      390ms   100%  main.Prime (inline)
         0     0% 97.44%      390ms   100%  main.main
         0     0% 97.44%      390ms   100%  runtime.main
      10ms  2.56%   100%       10ms  2.56%  runtime.asyncPreempt
(pprof)

list命令顯示了功能的使用時間。

(pprof) list main.Prime
Total: 390ms
ROUTINE ======================== main.Prime in /Users/username/dev/mastering-Go-3rd/ch11/blog/clacpuprofile.go
     380ms      390ms (flat, cum)   100% of Total
         .          .     26:   }
         .          .     27:   fmt.Println("Total primes:", total)
         .          .     28:}
         .          .     29:
         .          .     30:func Prime(n int) bool {
     120ms      130ms     31:   for i := 2; i < n; i++ {
     260ms      260ms     32:           if (n % i) == 0 {
         .          .     33:                   return false
         .          .     34:           }
         .          .     35:   }
         .          .     36:   return true
         .          .     37:}
(pprof)

2.http pprof

對于網絡應用程序,導入 net/http/pprof 會收集可在 /debug/pprof 端點訪問的配置文件信息。

package main
import (
 "net/http"
 "net/http/pprof"
)
func NewHttpServer(addr string) *http.Server {
 httpsrv := newHttpServer()
 r := mux.NewRouter()
 r.HandleFunc("/", httpsrv.handleFoo).Methods("POST")
 r.HandleFunc("/", httpsrv.handleBar).Methods("GET")
 r.HandleFunc("/debug/pprof/", pprof.Index)
 r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
 r.HandleFunc("/debug/pprof/profile", pprof.Profile)
 r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
 r.HandleFunc("/debug/pprof/trace", pprof.Trace)
 srv := server.NewHttpServer(":8080")
 log.Fatal(srv.ListenAndServe())
}

http pprof 支持與 CPU pprof 類似的配置文件檢查命令,即使在應用程序停止后也能對配置文件進行檢查。

> go tool pprof http://localhost:8080/debug/pprof/profile
Fetching profile over HTTP from http://localhost:8080/debug/pprof/profile
Saved profile in /Users/username/pprof/pprof.samples.cpu.002.pb.gz
Type: cpu
Time: Feb 8, 2023 at 11:12pm (JST)
Duration: 30s, Total samples = 270ms (  0.9%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 270ms, 100% of 270ms total
Showing top 10 nodes out of 67
      flat  flat%   sum%        cum   cum%
      90ms 33.33% 33.33%       90ms 33.33%  syscall.syscall
      80ms 29.63% 62.96%       80ms 29.63%  runtime.pthread_cond_signal
      50ms 18.52% 81.48%       50ms 18.52%  runtime.kevent
      30ms 11.11% 92.59%       30ms 11.11%  runtime.pthread_cond_wait
      20ms  7.41%   100%       20ms  7.41%  syscall.syscall6
         0     0%   100%       10ms  3.70%  bufio.(*Reader).Peek
         0     0%   100%       20ms  7.41%  bufio.(*Reader).ReadLine
         0     0%   100%       20ms  7.41%  bufio.(*Reader).ReadSlice
         0     0%   100%       30ms 11.11%  bufio.(*Reader).fill
         0     0%   100%       30ms 11.11%  bufio.(*Writer).Flush
go tool pprof -http :9402 /Users/username/pprof/pprof.samples.cpu.002.pb.gz

在我們的項目中,我們經常通過 k8s 上的端口轉發訪問配置文件信息。

kubectl port-forward -n $namespace localhost 8080:8000
# This allows access at localhost:8000/debug/pprof/profile

3.callgraph

除了 CLI 驗證外,您還可以使用 Web 界面在瀏覽器中查看調用圖(這非常棒)。在與之前相同的命令中添加 -http 選項即可。您還可以下載并指定配置文件以供審查。

> go tool pprof -http :8888 http://localhost:8080/debug/pprof/profile
Fetching profile over HTTP from http://localhost:8080/debug/pprof/profile
Saved profile in /Users/username/pprof/pprof.samples.cpu.004.pb.gz
Serving web UI on http://localhost:8888

有了調用圖,您就可以快速了解程序正在調用哪些進程。

4.火焰圖

火焰圖直觀顯示應用程序花費的時間,可點擊框架深入檢查方法。

您可以點擊每個幀,進一步檢查方法內部的內容

5.檢測內存泄漏

通過比較改進前和改進后的配置文件,大大方便了內存泄漏的檢測,從而方便了泄漏解決方案的識別和驗證。

# before improvement
noglob curl -s http://localhost:8080/debug/pprof/profile > /tmp/profile-before.prof

# after improvement
noglob curl -s http://localhost:8080/debug/pprof/profile > /tmp/profile-after.prof

# Check the difference before and after
go tool pprof -http: 8000 --diff_base /tmp/profile-before.prof /tmp/profile-after.prof

對于內存泄漏調查,可以指定堆配置文件集合。

go tool pprof http://localhost:8080/debug/pprof/heap?seconds=10

# Continue processing for a fixed period before retrieving again
go tool pprof http://localhost:8080/debug/pprof/heap?seconds=10

# Check the difference (can also be viewed in the web interface with the -http option)
go tool pprof --diff_base=/Users/username/pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.001.pb.gz /Users/username/pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.002.pb.gz 

(pprof) top
Showing nodes accounting for 12.92GB, 295.37% of 4.38GB total
Dropped 2 nodes (cum <= 0.02GB)
      flat  flat%   sum%        cum   cum%
   12.92GB 295.37% 295.37%    12.93GB 295.56%  main.main
         0     0% 295.37%    12.93GB 295.56%  runtime.main
(pprof) top 20
Showing nodes accounting for 12.92GB, 295.37% of 4.38GB total
Dropped 2 nodes (cum <= 0.02GB)
      flat  flat%   sum%        cum   cum%
   12.92GB 295.37% 295.37%    12.93GB 295.56%  main.main
         0     0% 295.37%    12.93GB 295.56%  runtime.main
(pprof) list main.
Total: 4.38GB
ROUTINE ======================== main.main in /Users/username/dev/proglog/cmd/server/main.go
   12.92GB    12.93GB (flat, cum) 295.56% of Total
         .          .     15:

二、trace

trace 可讓用戶深入了解運行時如何調度 goroutine,為調查爭用問題或 GC 問題提供有關堆、操作系統線程數和 goroutine 狀態的寶貴數據。在命令行應用程序中可通過 runtime/trace 軟件包訪問,在網絡應用程序中可通過 net/http 軟件包訪問。

# fetch trace with executing web application
noglob curl http://localhost:8080/debug/pprof/trace?seconds=10 > /tmp/pprof.trace
# chech the trace
go tool trace /tmp/pprof.trace

總結

總而言之,從開發到部署的整個過程都離不開基本的負載測試和性能剖析。利用 Go 的 pprof 和跟蹤工具,開發人員可以深入了解性能瓶頸、CPU 使用率和內存分配情況。

可視化調用圖和火焰圖的功能進一步幫助找出效率低下的問題,從而進行精確的優化。利用這些工具不僅能加強調試過程,還能顯著提高應用程序的性能。采用這些方法對于確保我們的應用程序穩健高效、隨時滿足實際需求至關重要。

責任編輯:趙寧寧 來源: 愛發白日夢的后端
相關推薦

2023-12-01 07:06:14

Go命令行性能

2021-04-14 08:20:46

Lighthouse工具性能檢測

2015-08-14 09:21:09

gdb工具調試 Go

2024-02-26 00:00:00

Go性能工具

2017-06-12 18:48:00

Android性能分析工具

2021-08-04 09:33:22

Go 性能優化

2015-01-21 15:40:44

GoRuby

2023-12-26 00:58:53

Web應用Go語言

2013-03-06 10:24:12

ksar工具系統性能

2021-04-09 20:04:34

區塊鏈Go加密

2018-02-26 18:00:08

Linux運維Debian

2023-12-14 08:01:08

事件管理器Go

2025-05-22 10:15:59

JITWatchJava

2019-11-01 08:49:07

JVM監控性能

2021-06-07 14:57:46

開源開源工具Linux

2011-08-15 22:10:08

Oracle性能分析工

2013-05-22 09:38:03

GoGo語言Go性能

2022-03-21 14:13:22

Go語言編程

2024-12-25 14:03:03

2024-04-12 08:58:45

Go數據工具鏈
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美一区二区三区,视频 | 91精品国产92| 涩涩鲁亚洲精品一区二区 | 日本精品视频在线观看 | 国产电影一区二区在线观看 | 欧美精品久久久 | 国产高清视频在线观看播放 | 成人福利视频网站 | 在线观看免费毛片 | 91久久久久久 | 国产成人一区二区 | 久久不射电影网 | 亚洲国产精品一区二区第一页 | 日韩免费福利视频 | 国产一区二区三区 | 四色永久 | 在线免费观看黄色av | 国产在线色 | 日本一区二区高清不卡 | 不卡的av一区 | 久久国产精品久久国产精品 | 精品欧美一区二区三区久久久 | 日本超碰 | 欧美色性| www.国产视频 | 精品国产黄色片 | 成人深夜福利 | 91亚洲免费 | 亚洲欧美日韩在线不卡 | 久久九九免费 | 精品久久久一区 | 久久综合九色综合欧美狠狠 | 国产精彩视频 | 91亚洲欧美 | 天天综合永久入口 | av片在线播放 | 国产精品视频不卡 | 一区二区日韩 | 国产成人免费视频网站高清观看视频 | 亚洲一区毛片 | 久久亚洲天堂 |