From 6d7fea3cee9505463e8e2949b1a3ec9f4cbe49c5 Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Mon, 19 Jan 2026 14:15:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E5=8A=A8debug=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=88=B0common/debug=E5=AD=90=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debug.go => common/debug/debug.go | 18 +++++------------- common/debug/stub.go | 9 +++++++++ debug_stub.go | 13 ------------- main.go | 5 +++-- 4 files changed, 17 insertions(+), 28 deletions(-) rename debug.go => common/debug/debug.go (89%) create mode 100644 common/debug/stub.go delete mode 100644 debug_stub.go diff --git a/debug.go b/common/debug/debug.go similarity index 89% rename from debug.go rename to common/debug/debug.go index 8b26372..eaa29a4 100644 --- a/debug.go +++ b/common/debug/debug.go @@ -1,7 +1,7 @@ //go:build debug // +build debug -package main +package debug import ( "fmt" @@ -13,19 +13,16 @@ import ( var ( cpuProfile *os.File - memProfile *os.File traceFile *os.File - profilesPath = "./profiles" // 性能分析文件保存目录 + profilesPath = "./profiles" ) -func startPprof() { - // 创建 profiles 目录 +func Start() { if err := os.MkdirAll(profilesPath, 0755); err != nil { fmt.Printf("[DEBUG] 创建 profiles 目录失败: %v\n", err) return } - // 启动 CPU profiling var err error cpuProfile, err = os.Create(profilesPath + "/cpu.prof") if err != nil { @@ -40,7 +37,6 @@ func startPprof() { } } - // 启动 trace traceFile, err = os.Create(profilesPath + "/trace.out") if err != nil { fmt.Printf("[DEBUG] 创建 trace 文件失败: %v\n", err) @@ -57,27 +53,24 @@ func startPprof() { fmt.Printf("[DEBUG] 性能分析已启动,程序结束时自动保存到 %s/\n", profilesPath) } -func stopPprof() { - // 停止 CPU profiling +func Stop() { if cpuProfile != nil { pprof.StopCPUProfile() cpuProfile.Close() fmt.Printf("[DEBUG] CPU profile 已保存\n") } - // 停止 trace if traceFile != nil { trace.Stop() traceFile.Close() fmt.Printf("[DEBUG] Trace 已保存\n") } - // 写入内存 profile memProfile, err := os.Create(profilesPath + "/mem.prof") if err != nil { fmt.Printf("[DEBUG] 创建内存 profile 失败: %v\n", err) } else { - runtime.GC() // 先执行 GC + runtime.GC() if err := pprof.WriteHeapProfile(memProfile); err != nil { fmt.Printf("[DEBUG] 写入内存 profile 失败: %v\n", err) } else { @@ -86,7 +79,6 @@ func stopPprof() { memProfile.Close() } - // 写入 goroutine profile goroutineProfile, err := os.Create(profilesPath + "/goroutine.prof") if err != nil { fmt.Printf("[DEBUG] 创建 goroutine profile 失败: %v\n", err) diff --git a/common/debug/stub.go b/common/debug/stub.go new file mode 100644 index 0000000..351c751 --- /dev/null +++ b/common/debug/stub.go @@ -0,0 +1,9 @@ +//go:build !debug +// +build !debug + +package debug + +// 生产版本:pprof 完全不编译进来 + +func Start() {} +func Stop() {} diff --git a/debug_stub.go b/debug_stub.go deleted file mode 100644 index f9b8f19..0000000 --- a/debug_stub.go +++ /dev/null @@ -1,13 +0,0 @@ -//go:build !debug -// +build !debug - -package main - -// 生产版本:pprof 完全不编译进来 -func startPprof() { - // 空函数,什么都不做 -} - -func stopPprof() { - // 空函数,什么都不做 -} diff --git a/main.go b/main.go index 0e1d97d..58f93f6 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "syscall" "github.com/shadow1ng/fscan/common" + "github.com/shadow1ng/fscan/common/debug" "github.com/shadow1ng/fscan/common/i18n" "github.com/shadow1ng/fscan/core" "github.com/shadow1ng/fscan/web" @@ -18,8 +19,8 @@ import ( func main() { // 启动 pprof(仅调试版本) - startPprof() - defer stopPprof() // 程序退出时保存性能分析数据 + debug.Start() + defer debug.Stop() // 解析命令行参数 var info common.HostInfo