profiler

package
v1.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2025 License: MIT Imports: 11 Imported by: 0

README

Flow框架性能分析模块

这是Flow框架的性能分析模块,提供了全面的性能监控和分析功能,帮助开发者优化应用性能、排查性能问题。

功能特点

  • CPU分析:监控和分析CPU使用情况,找出热点函数
  • 内存分析:跟踪内存分配和使用情况,找出内存泄漏
  • 协程分析:监控协程的创建和运行情况
  • 阻塞分析:发现可能的死锁和阻塞点
  • 互斥锁分析:检测互斥锁争用情况
  • 实时监控:通过Web界面实时查看应用性能指标
  • 性能分析报告:生成详细的性能分析报告

快速开始

在应用中集成
package main

import (
    "time"
    
    "github.com/zzliekkas/flow/profiler"
)

func main() {
    // 创建性能分析器(使用默认配置)
    prof := profiler.NewProfiler(nil)
    
    // 启动性能分析
    prof.Start()
    
    // 在程序结束时停止分析
    defer func() {
        result, _ := prof.Stop()
        println("性能分析结果:", result.FilePath)
    }()
    
    // 您的应用代码...
    
    // 可以随时获取内存统计
    memStats := prof.GetMemoryStats()
    println(prof.FormatMemoryStats(memStats))
}
使用命令行工具
package main

import (
    "time"
    
    "github.com/zzliekkas/flow/profiler"
)

func main() {
    // 创建性能分析命令
    cmd := profiler.NewProfileCmd()
    
    // 配置分析参数
    cmd.SetOutputDir("./profiles")
       .SetDuration(60 * time.Second)
       .SetHTTP(true)
       .SetHTTPAddr("localhost:6060")
    
    // 运行分析
    if err := cmd.Run(); err != nil {
        panic(err)
    }
    
    // 您的应用代码...
}
通过HTTP接口访问

启用HTTP服务后,可以通过以下URL访问性能分析功能:

详细配置

ProfilerConfig 配置项
type ProfilerConfig struct {
    // 输出目录
    OutputDir string
    
    // 是否启用HTTP分析服务
    EnableHTTP bool
    
    // HTTP服务地址
    HTTPAddr string
    
    // 是否启用内存分析
    EnableMemory bool
    
    // 是否启用CPU分析
    EnableCPU bool
    
    // 是否启用阻塞分析
    EnableBlock bool
    
    // 是否启用协程分析
    EnableGoroutine bool
    
    // 采样率
    Rate int
}

使用pprof工具分析结果

生成的profile文件可以使用Go自带的pprof工具进行分析:

# 分析CPU profile
go tool pprof -http=:8080 ./profiles/cpu-20230601-120000.pprof

# 分析内存profile
go tool pprof -http=:8080 ./profiles/memory-20230601-120000.pprof

# 分析协程profile
go tool pprof -http=:8080 ./profiles/goroutine-20230601-120000.pprof

最佳实践

  1. 生产环境谨慎使用:性能分析可能会对应用性能产生一定影响,生产环境应谨慎使用
  2. 设置合理的采样率:高采样率会提供更精确的数据,但也会带来更大的性能开销
  3. 分析单一问题:每次只关注一个性能问题,避免同时启用过多分析功能
  4. 定期分析:定期对应用进行性能分析,及早发现潜在问题
  5. 与监控系统结合:将性能分析与监控系统结合,在发现异常时自动触发分析

常见问题

Q: 性能分析会影响应用性能吗?

A: 会有一定影响,特别是CPU分析。建议在非关键环境测试,或者在生产环境短时间开启。

Q: 如何分析分布式应用?

A: 可以在每个节点单独启用分析器,然后汇总结果,或使用分布式跟踪工具如Jaeger结合使用。

Q: 内存泄漏如何排查?

A: 可以定期使用TakeMemorySnapshot获取内存快照,然后使用pprof比较不同时间点的内存状态。

后续规划

  • 分布式性能分析支持
  • 性能瓶颈自动识别
  • 与APM系统集成
  • 更友好的Web界面
  • 自定义监控指标

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Example

func Example()

Example 性能分析模块使用示例

func FormatBytes

func FormatBytes(bytes uint64) string

FormatBytes 格式化字节大小为易读形式

Types

type ProfileCmd

type ProfileCmd struct {
	// contains filtered or unexported fields
}

ProfileCmd 性能分析命令

func NewProfileCmd

func NewProfileCmd() *ProfileCmd

NewProfileCmd 创建新的性能分析命令

func (*ProfileCmd) GetProfiler

func (c *ProfileCmd) GetProfiler() *Profiler

GetProfiler 获取分析器实例

func (*ProfileCmd) Run

func (c *ProfileCmd) Run() error

Run 运行性能分析命令

func (*ProfileCmd) SetCPUOnly

func (c *ProfileCmd) SetCPUOnly(cpuOnly bool) *ProfileCmd

SetCPUOnly 设置仅进行CPU分析

func (*ProfileCmd) SetDuration

func (c *ProfileCmd) SetDuration(d time.Duration) *ProfileCmd

SetDuration 设置分析持续时间

func (*ProfileCmd) SetHTTP

func (c *ProfileCmd) SetHTTP(http bool) *ProfileCmd

SetHTTP 设置是否启用HTTP服务

func (*ProfileCmd) SetHTTPAddr

func (c *ProfileCmd) SetHTTPAddr(addr string) *ProfileCmd

SetHTTPAddr 设置HTTP服务地址

func (*ProfileCmd) SetMemOnly

func (c *ProfileCmd) SetMemOnly(memOnly bool) *ProfileCmd

SetMemOnly 设置仅进行内存分析

func (*ProfileCmd) SetOutputDir

func (c *ProfileCmd) SetOutputDir(dir string) *ProfileCmd

SetOutputDir 设置输出目录

type ProfileResult

type ProfileResult struct {
	// 分析类型
	Type string

	// 文件路径
	FilePath string

	// 分析时长
	Duration time.Duration

	// 分析开始时间
	StartTime time.Time

	// 分析结束时间
	EndTime time.Time

	// 内存统计
	MemStats *runtime.MemStats
}

ProfileResult 分析结果

type Profiler

type Profiler struct {
	// contains filtered or unexported fields
}

Profiler 性能分析器

func NewProfiler

func NewProfiler(config *ProfilerConfig) *Profiler

NewProfiler 创建新的性能分析器

func (*Profiler) FormatMemoryStats

func (p *Profiler) FormatMemoryStats(stats *runtime.MemStats) string

FormatMemoryStats 格式化内存统计信息

func (*Profiler) GetGoroutineStats

func (p *Profiler) GetGoroutineStats() string

GetGoroutineStats 获取协程统计信息

func (*Profiler) GetMemoryStats

func (p *Profiler) GetMemoryStats() *runtime.MemStats

GetMemoryStats 获取内存统计信息

func (*Profiler) RegisterHTTPHandlers

func (p *Profiler) RegisterHTTPHandlers()

RegisterHTTPHandlers 注册HTTP处理器

func (*Profiler) Start

func (p *Profiler) Start() error

Start 启动性能分析

func (*Profiler) StartCPUProfile

func (p *Profiler) StartCPUProfile() error

StartCPUProfile 启动CPU分析

func (*Profiler) Stop

func (p *Profiler) Stop() (*ProfileResult, error)

Stop 停止性能分析

func (*Profiler) StopCPUProfile

func (p *Profiler) StopCPUProfile()

StopCPUProfile 停止CPU分析

func (*Profiler) TakeMemorySnapshot

func (p *Profiler) TakeMemorySnapshot(label string) (string, error)

TakeMemorySnapshot 获取内存快照

type ProfilerConfig

type ProfilerConfig struct {
	// 输出目录
	OutputDir string

	// 是否启用HTTP分析服务
	EnableHTTP bool

	// HTTP服务地址
	HTTPAddr string

	// 是否启用内存分析
	EnableMemory bool

	// 是否启用CPU分析
	EnableCPU bool

	// 是否启用阻塞分析
	EnableBlock bool

	// 是否启用协程分析
	EnableGoroutine bool

	// 采样率
	Rate int
}

ProfilerConfig 性能分析器配置

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL