pprof

package
v1.10.33 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2022 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package pprof provides the profiling command-line options and adapts to go tool pprof.

Examples:

if err := cmdr.Exec(buildRootCmd(),
    pprof.GetCmdrProfilingOptions(...),
); err != nil {
    log.Fatalf("error occurs in app running: %+v\n", err)
}

Examples:

func buildRootCmd() (rootCmd *cmdr.RootCommand) {
    root := cmdr.Root(appName, cmdr.Version)
    rootCmd = root.RootCommand()
    pprof.AttachToCmdr(root)
    return
}

Examples:

func buildRootCmd() (rootCmd *cmdr.RootCommand) {
    root := cmdr.Root(appName, cmdr.Version).
       Copyright(copyright, "hedzr")
    rootCmd = root.RootCommand()
    pprof.AttachToCmdr(root.RootCmdOpt(), ...)
    return
}

Index

Constants

View Source
const DefaultMemProfileRate = 4096

DefaultMemProfileRate is the default memory profiling rate. See also http://golang.org/pkg/runtime/#pkg-variables

Variables

This section is empty.

Functions

func AttachToCmdr

func AttachToCmdr(root *cmdr.RootCmdOpt, types ...string)

AttachToCmdr attaches the profiling options to root command.

For example:

cmdr.Exec(buildRootCmd())

func buildRootCmd() (rootCmd *cmdr.RootCommand) {
    root := cmdr.Root(appName, cmdr.Version).
        Copyright(copyright, "hedzr").
        Description(desc, longDesc).
        Examples(examples)
    rootCmd = root.RootCommand()
    pprof.AttachToCmdr(root.RootCmdOpt())
    return
}

The variadic param `types` allows which profiling types are enabled by default, such as `cpu`, `mem`. While end-user enables profiling with `app -ep`, those profiles will be created and written. The available type names are: "cpu", "mem", "mutex", "block", "thread-create", "trace", and "go-routine".

func EnableCPUProfile

func EnableCPUProfile(cpuProfilePath string) (closer func())

EnableCPUProfile enables cpu profiling. And review the pprof result in a web ui:

go tool pprof -http=:8555 ./cpu.pprof

Now you can open 'http://localhost:8555/ui' in a browser

func EnableMemProfile

func EnableMemProfile(memProfilePath string) (closer func())

EnableMemProfile enables memory profiling. And review the pprof result in a web ui:

go tool pprof -http=:8555 ./mem.pprof

Now you can open 'http://localhost:8555/ui' in a browser

func GetCmdrProfilingOptions

func GetCmdrProfilingOptions(types ...string) cmdr.ExecOption

GetCmdrProfilingOptions returns an opt for cmdr.Exec(root, opts...). And, it adds profiling options to cmdr system.

For example:

cmdr.Exec(buildRootCmd(),
    profile.GetCmdrProfilingOptions(),
)

The variadic param `types` allows which profiling types are enabled by default, such as `cpu`, `mem`. While end-user enables profiling with `app -ep`, those profiles will be created and written. The available type names are: "cpu", "mem", "mutex", "block", "thread-create", "trace", and "go-routine".

func Start

func Start(types ProfType, opts ...ProfOpt) (Stop func())

Start constructs any profiling sessions from `types`. `types` is a OR-combined ProfType value, it looks like CPUProf | MemProf.

func WithCmdrProfilingOptions added in v1.10.0

func WithCmdrProfilingOptions(types ...string) cmdr.ExecOption

WithCmdrProfilingOptions returns an opt for cmdr.Exec(root, opts...). And, it adds profiling options to cmdr system.

For example:

cmdr.Exec(buildRootCmd(),
    profile.GetCmdrProfilingOptions(),
)

The variadic param `types` allows which profiling types are enabled by default, such as `cpu`, `mem`. While end-user enables profiling with `app -ep`, those profiles will be created and written. The available type names are: "cpu", "mem", "mutex", "block", "thread-create", "trace", and "go-routine".

func WithCmdrProfilingOptionsHidden added in v1.10.0

func WithCmdrProfilingOptionsHidden(types ...string) cmdr.ExecOption

WithCmdrProfilingOptionsHidden hides the commands and flags from help screen

Types

type ProfOpt

type ProfOpt func(*profile)

ProfOpt provides a wrapped functional options type for Start(...)

func WithBlockProfName

func WithBlockProfName(filename string) ProfOpt

WithBlockProfName ..

func WithCPUProfName

func WithCPUProfName(filename string) ProfOpt

WithCPUProfName ..

func WithGoRoutineProfName

func WithGoRoutineProfName(filename string) ProfOpt

WithGoRoutineProfName ..

func WithMemProfName

func WithMemProfName(filename string) ProfOpt

WithMemProfName ..

func WithMemProfileRate

func WithMemProfileRate(rate int) ProfOpt

WithMemProfileRate enables memory profiling with a special rate

func WithMemProfileType

func WithMemProfileType(typ string) ProfOpt

WithMemProfileType specify the typename of memory profiling, "heap" or "allocs"

func WithMutexProfName

func WithMutexProfName(filename string) ProfOpt

WithMutexProfName ..

func WithOutputDirectory

func WithOutputDirectory(path string) ProfOpt

WithOutputDirectory specify a directory for writing profiles. Default is '.'.

func WithThreadCreateProfName

func WithThreadCreateProfName(filename string) ProfOpt

WithThreadCreateProfName ..

func WithTraceProfName

func WithTraceProfName(filename string) ProfOpt

WithTraceProfName ..

func WithTypes

func WithTypes(types ...ProfType) ProfOpt

WithTypes allows a list of ProfType specified. For Example:

Start(0, WithTypes(CPUProf, MemProf, BlockProf))

type ProfType

type ProfType int

ProfType represents the profile type

const (
	// CPUProf represents cpu profiling
	CPUProf ProfType = 1
	// MemProf represents memory profiling
	MemProf ProfType = 2
	// MutexProf represents mutex profiling
	MutexProf ProfType = 4
	// BlockProf represents block profiling
	BlockProf ProfType = 8
	// ThreadCreateProf represents thread creation profiling
	ThreadCreateProf ProfType = 16
	// TraceProf represents trace profiling
	TraceProf ProfType = 32
	// GoRoutineProf represents go-routine profiling
	GoRoutineProf ProfType = 64
)

Jump to

Keyboard shortcuts

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