| 18 | ) |
| 19 | |
| 20 | func Start() { |
| 21 | if err := os.MkdirAll(profilesPath, 0755); err != nil { |
| 22 | fmt.Printf("[DEBUG] 创建 profiles 目录失败: %v\n", err) |
| 23 | return |
| 24 | } |
| 25 | |
| 26 | var err error |
| 27 | cpuProfile, err = os.Create(profilesPath + "/cpu.prof") |
| 28 | if err != nil { |
| 29 | fmt.Printf("[DEBUG] 创建 CPU profile 失败: %v\n", err) |
| 30 | } else { |
| 31 | if err := pprof.StartCPUProfile(cpuProfile); err != nil { |
| 32 | fmt.Printf("[DEBUG] 启动 CPU profile 失败: %v\n", err) |
| 33 | cpuProfile.Close() |
| 34 | cpuProfile = nil |
| 35 | } else { |
| 36 | fmt.Printf("[DEBUG] CPU profiling 已启动 -> %s/cpu.prof\n", profilesPath) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | traceFile, err = os.Create(profilesPath + "/trace.out") |
| 41 | if err != nil { |
| 42 | fmt.Printf("[DEBUG] 创建 trace 文件失败: %v\n", err) |
| 43 | } else { |
| 44 | if err := trace.Start(traceFile); err != nil { |
| 45 | fmt.Printf("[DEBUG] 启动 trace 失败: %v\n", err) |
| 46 | traceFile.Close() |
| 47 | traceFile = nil |
| 48 | } else { |
| 49 | fmt.Printf("[DEBUG] Execution trace 已启动 -> %s/trace.out\n", profilesPath) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fmt.Printf("[DEBUG] 性能分析已启动,程序结束时自动保存到 %s/\n", profilesPath) |
| 54 | } |
| 55 | |
| 56 | func Stop() { |
| 57 | if cpuProfile != nil { |