Documentation
¶
Overview ¶
Package pprofutils provides utilities for runtime profiling of Go applications. It supports both CPU and memory profiling with customizable thresholds and callbacks.
Example usage:
pprofutils.AddCPUProfileCallback(func(stats []FunctionStat) {
// Handle CPU profile data
})
pprofutils.AddMemProfileCallback(func(metrics MemMetrics) {
// Handle memory metrics
})
Index ¶
- Variables
- func AddCPUProfileCallback(callback func(stats []FunctionStat))
- func AddMemProfileCallback(callback func(metrics []FunctionStat))
- func AutoAnalyzeFile(filename string) (string, error)
- func ClearCPUProfileCallbacks()
- func ClearMemProfileCallbacks()
- func DumpFunctionStats(stats []FunctionStat) string
- func StartCPUAndMemoryProfile(opts ...Option) error
- func StartCPUProfile(opts ...Option) error
- func StartMemoryProfile(opts ...Option) error
- type Config
- type FunctionStat
- type MemMetrics
- type MemMetricsPercent
- type Option
- func WithCPUProfileFile(file string) Option
- func WithContext(ctx context.Context) Option
- func WithFinished(h func(string)) Option
- func WithMemProfileFile(file string) Option
- func WithOnCPUProfileFinished(fn func(string, error)) Option
- func WithOnCPUProfileStarted(fn func(string)) Option
- func WithOnMemProfileFinished(fn func(string, error)) Option
- func WithOnMemProfileStarted(fn func(string)) Option
- func WithTimeout(i float64) Option
Constants ¶
This section is empty.
Variables ¶
View Source
var Exports = map[string]any{ "StartCPUAndMemoryProfile": StartCPUAndMemoryProfile, "StartCPUProfile": StartCPUProfile, "StartMemoryProfile": StartMemoryProfile, "AutoAnalyzeFile": AutoAnalyzeFile, "cpuProfilePath": WithCPUProfileFile, "memProfilePath": WithMemProfileFile, "ctx": WithContext, "onCPUProfileFinished": WithOnCPUProfileFinished, "onCPUProfileStarted": WithOnCPUProfileStarted, "onMemProfileStarted": WithOnMemProfileStarted, "onMemProfileFinished": WithOnMemProfileFinished, "timeout": WithTimeout, "callback": WithFinished, }
Functions ¶
func AddCPUProfileCallback ¶
func AddCPUProfileCallback(callback func(stats []FunctionStat))
添加CPU分析回调函数
func AddMemProfileCallback ¶
func AddMemProfileCallback(callback func(metrics []FunctionStat))
添加内存分析回调函数
func AutoAnalyzeFile ¶
AutoAnalyzeFile 分析指定的 pprof 文件并返回人类可读的分析结果
func DumpFunctionStats ¶
func DumpFunctionStats(stats []FunctionStat) string
func StartCPUProfile ¶
func StartMemoryProfile ¶
Types ¶
type FunctionStat ¶
type FunctionStat struct {
Name string
FileLocation string
Value int64
Calls int64
Percent float64
SampleType string // CPU或Memory类型
}
func AutoAnalyzeRaw ¶
func AutoAnalyzeRaw(reader io.Reader) ([]FunctionStat, error)
func (*FunctionStat) Dump ¶
func (f *FunctionStat) Dump() string
type MemMetrics ¶
type MemMetrics struct {
// 当前分配的内存大小(字节)
// 这个值表示当前正在使用的内存量,会随着内存分配和垃圾回收而变化
Alloc uint64
// 程序启动后累计分配的总内存(字节)
// 这个值只会增加,不会因为垃圾回收而减少
TotalAlloc uint64
// 当前堆内存分配量(字节)
// 与Alloc类似,但只统计堆内存,不包括栈内存
HeapAlloc uint64
// 从系统申请的堆内存总量(字节)
// 这个值表示系统实际为堆分配的内存,可能会大于HeapAlloc
// 因为包含了预留和未使用的内存
HeapSys uint64
// 系统总物理内存
SystemMemory uint64
// 系统可用物理内存
AvailableMemory uint64
}
func (MemMetrics) Percent ¶
func (m MemMetrics) Percent() MemMetricsPercent
Percent 计算程序内存占用系统物理内存的百分比
type MemMetricsPercent ¶
type Option ¶
type Option func(*Config)
func WithCPUProfileFile ¶
func WithContext ¶
func WithFinished ¶
func WithMemProfileFile ¶
func WithOnCPUProfileStarted ¶
func WithOnMemProfileStarted ¶
func WithTimeout ¶
Click to show internal directories.
Click to hide internal directories.