💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 常见分析指标 * Wall Time * CPU Time * Block Time * Memory Allocation * Gc Times/time spent ## 性能分析工具 ``` sudo apt-get install graphviz ``` ### go-torch 火炬图 ### 通过文件方式输出Profile go tool pprof 1.11以上版本默认有 * `go tool pprof [binary] [binary.prof]` * 查看报告:go tool pprof prof cpu.prof 示例: ``` package main import ( "log" "math/rand" "os" "runtime" "runtime/pprof" "time" ) const ( col = 10000 row = 10000 ) func fillMatrix(m *[row][col]int) { s := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < row; i++ { for j := 0; j < col; j++ { m[i][j] = s.Intn(100000) } } } func calculate(m *[row][col]int) { for i := 0; i < row; i++ { tmp := 0 for j := 0; j < col; j++ { tmp += m[i][j] } } } func main() { //创建输出文件 f, err := os.Create("cpu.prof") if err != nil { log.Fatal("could not create CPU profile: ", err) } // 获取系统信息 if err := pprof.StartCPUProfile(f); err != nil { //监控cpu log.Fatal("could not start CPU profile: ", err) } defer pprof.StopCPUProfile() // 主逻辑区,进行一些简单的代码运算 x := [row][col]int{} fillMatrix(&x) calculate(&x) f1, err := os.Create("mem.prof") if err != nil { log.Fatal("could not create memory profile: ", err) } runtime.GC() // GC,获取最新的数据信息 if err := pprof.WriteHeapProfile(f1); err != nil { // 写入内存信息 log.Fatal("could not write memory profile: ", err) } f1.Close() f2, err := os.Create("goroutine.prof") if err != nil { log.Fatal("could not create groutine profile: ", err) } if gProf := pprof.Lookup("goroutine"); gProf == nil { log.Fatal("could not write groutine profile: ") } else { gProf.WriteTo(f2, 0) } f2.Close() } ```