mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-25 12:41:53 +08:00
refactor: 移动debug模块到common/debug子包
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
//go:build debug
|
//go:build debug
|
||||||
// +build debug
|
// +build debug
|
||||||
|
|
||||||
package main
|
package debug
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -13,19 +13,16 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
cpuProfile *os.File
|
cpuProfile *os.File
|
||||||
memProfile *os.File
|
|
||||||
traceFile *os.File
|
traceFile *os.File
|
||||||
profilesPath = "./profiles" // 性能分析文件保存目录
|
profilesPath = "./profiles"
|
||||||
)
|
)
|
||||||
|
|
||||||
func startPprof() {
|
func Start() {
|
||||||
// 创建 profiles 目录
|
|
||||||
if err := os.MkdirAll(profilesPath, 0755); err != nil {
|
if err := os.MkdirAll(profilesPath, 0755); err != nil {
|
||||||
fmt.Printf("[DEBUG] 创建 profiles 目录失败: %v\n", err)
|
fmt.Printf("[DEBUG] 创建 profiles 目录失败: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启动 CPU profiling
|
|
||||||
var err error
|
var err error
|
||||||
cpuProfile, err = os.Create(profilesPath + "/cpu.prof")
|
cpuProfile, err = os.Create(profilesPath + "/cpu.prof")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -40,7 +37,6 @@ func startPprof() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启动 trace
|
|
||||||
traceFile, err = os.Create(profilesPath + "/trace.out")
|
traceFile, err = os.Create(profilesPath + "/trace.out")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[DEBUG] 创建 trace 文件失败: %v\n", err)
|
fmt.Printf("[DEBUG] 创建 trace 文件失败: %v\n", err)
|
||||||
@@ -57,27 +53,24 @@ func startPprof() {
|
|||||||
fmt.Printf("[DEBUG] 性能分析已启动,程序结束时自动保存到 %s/\n", profilesPath)
|
fmt.Printf("[DEBUG] 性能分析已启动,程序结束时自动保存到 %s/\n", profilesPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stopPprof() {
|
func Stop() {
|
||||||
// 停止 CPU profiling
|
|
||||||
if cpuProfile != nil {
|
if cpuProfile != nil {
|
||||||
pprof.StopCPUProfile()
|
pprof.StopCPUProfile()
|
||||||
cpuProfile.Close()
|
cpuProfile.Close()
|
||||||
fmt.Printf("[DEBUG] CPU profile 已保存\n")
|
fmt.Printf("[DEBUG] CPU profile 已保存\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 停止 trace
|
|
||||||
if traceFile != nil {
|
if traceFile != nil {
|
||||||
trace.Stop()
|
trace.Stop()
|
||||||
traceFile.Close()
|
traceFile.Close()
|
||||||
fmt.Printf("[DEBUG] Trace 已保存\n")
|
fmt.Printf("[DEBUG] Trace 已保存\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入内存 profile
|
|
||||||
memProfile, err := os.Create(profilesPath + "/mem.prof")
|
memProfile, err := os.Create(profilesPath + "/mem.prof")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[DEBUG] 创建内存 profile 失败: %v\n", err)
|
fmt.Printf("[DEBUG] 创建内存 profile 失败: %v\n", err)
|
||||||
} else {
|
} else {
|
||||||
runtime.GC() // 先执行 GC
|
runtime.GC()
|
||||||
if err := pprof.WriteHeapProfile(memProfile); err != nil {
|
if err := pprof.WriteHeapProfile(memProfile); err != nil {
|
||||||
fmt.Printf("[DEBUG] 写入内存 profile 失败: %v\n", err)
|
fmt.Printf("[DEBUG] 写入内存 profile 失败: %v\n", err)
|
||||||
} else {
|
} else {
|
||||||
@@ -86,7 +79,6 @@ func stopPprof() {
|
|||||||
memProfile.Close()
|
memProfile.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入 goroutine profile
|
|
||||||
goroutineProfile, err := os.Create(profilesPath + "/goroutine.prof")
|
goroutineProfile, err := os.Create(profilesPath + "/goroutine.prof")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[DEBUG] 创建 goroutine profile 失败: %v\n", err)
|
fmt.Printf("[DEBUG] 创建 goroutine profile 失败: %v\n", err)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
//go:build !debug
|
||||||
|
// +build !debug
|
||||||
|
|
||||||
|
package debug
|
||||||
|
|
||||||
|
// 生产版本:pprof 完全不编译进来
|
||||||
|
|
||||||
|
func Start() {}
|
||||||
|
func Stop() {}
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
//go:build !debug
|
|
||||||
// +build !debug
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
// 生产版本:pprof 完全不编译进来
|
|
||||||
func startPprof() {
|
|
||||||
// 空函数,什么都不做
|
|
||||||
}
|
|
||||||
|
|
||||||
func stopPprof() {
|
|
||||||
// 空函数,什么都不做
|
|
||||||
}
|
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/shadow1ng/fscan/common"
|
"github.com/shadow1ng/fscan/common"
|
||||||
|
"github.com/shadow1ng/fscan/common/debug"
|
||||||
"github.com/shadow1ng/fscan/common/i18n"
|
"github.com/shadow1ng/fscan/common/i18n"
|
||||||
"github.com/shadow1ng/fscan/core"
|
"github.com/shadow1ng/fscan/core"
|
||||||
"github.com/shadow1ng/fscan/web"
|
"github.com/shadow1ng/fscan/web"
|
||||||
@@ -18,8 +19,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 启动 pprof(仅调试版本)
|
// 启动 pprof(仅调试版本)
|
||||||
startPprof()
|
debug.Start()
|
||||||
defer stopPprof() // 程序退出时保存性能分析数据
|
defer debug.Stop()
|
||||||
|
|
||||||
// 解析命令行参数
|
// 解析命令行参数
|
||||||
var info common.HostInfo
|
var info common.HostInfo
|
||||||
|
|||||||
Reference in New Issue
Block a user