偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

我們一起聊聊 Go 性能工具

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

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

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

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

  • pprof
  • trace

pprof

pprof 是 Go 專用的剖析器,可輕松確定 CPU 時間用在哪里,內(nèi)存分配在哪里。獲取配置文件的方法因命令行應(yīng)用程序和網(wǎng)絡(luò)應(yīng)用程序而異。

cpu pprof

對于 CPU 剖析,可在感興趣的代碼段前后調(diào)用 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
}

執(zhí)行上述代碼并生成 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選項可檢索每個函數(shù)的累計時間。

(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)

http pprof

對于網(wǎng)絡(luò)應(yīng)用程序,導入 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 類似的配置文件檢查命令,即使在應(yīng)用程序停止后也能對配置文件進行檢查。

> 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

在我們的項目中,我們經(jīng)常通過 k8s 上的端口轉(zhuǎn)發(fā)訪問配置文件信息。

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

callgraph

除了 CLI 驗證外,您還可以使用 Web 界面在瀏覽器中查看調(diào)用圖(這非常棒)。在與之前相同的命令中添加 -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

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

圖片圖片

火焰圖

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

圖片圖片

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

檢測內(nèi)存泄漏

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

# 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

對于內(nèi)存泄漏調(diào)查,可以指定堆配置文件集合。

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

# Continue processing for a fixed period before retrieving again
go tool pprof http://localhost:8080/debug/pprof/heap?secnotallow=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 可讓用戶深入了解運行時如何調(diào)度 goroutine,為調(diào)查爭用問題或 GC 問題提供有關(guān)堆、操作系統(tǒng)線程數(shù)和 goroutine 狀態(tài)的寶貴數(shù)據(jù)。在命令行應(yīng)用程序中可通過 runtime/trace 軟件包訪問,在網(wǎng)絡(luò)應(yīng)用程序中可通過 net/http 軟件包訪問。

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

總結(jié)

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

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

責任編輯:武曉燕 來源: 愛發(fā)白日夢的后端
相關(guān)推薦

2023-03-26 23:47:32

Go內(nèi)存模型

2024-07-11 08:26:00

2021-11-04 06:58:31

CSS性能設(shè)備

2021-08-27 07:06:10

IOJava抽象

2024-02-20 21:34:16

循環(huán)GolangGo

2023-08-10 08:28:46

網(wǎng)絡(luò)編程通信

2023-08-04 08:20:56

DockerfileDocker工具

2023-06-30 08:18:51

敏捷開發(fā)模式

2023-09-10 21:42:31

2022-05-24 08:21:16

數(shù)據(jù)安全API

2023-07-04 13:36:00

同步工具類Phaser

2025-06-11 02:10:00

2025-03-13 05:00:00

2024-05-17 08:47:33

數(shù)組切片元素

2024-06-27 08:54:22

Go模塊團隊

2022-11-12 12:33:38

CSS預(yù)處理器Sass

2025-03-27 02:00:00

SPIJava接口

2023-12-28 09:55:08

隊列數(shù)據(jù)結(jié)構(gòu)存儲

2022-01-04 12:08:46

設(shè)計接口

2023-07-27 07:46:51

SAFe團隊測試
點贊
收藏

51CTO技術(shù)棧公眾號