Documentation ¶
Overview ¶
Package profile provides a simple way to manage runtime/pprof profiling of your Go application.
Index ¶
Examples ¶
Constants ¶
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 BlockProfile ¶
func BlockProfile(p *profile)
BlockProfile controls if block (contention) profiling will be enabled. It disables any previous profiling settings.
func CPUProfile ¶
func CPUProfile(p *profile)
CPUProfile controls if cpu profiling will be enabled. It disables any previous profiling settings.
Example ¶
package main import ( "github.com/pkg/profile" ) func main() { // CPU profiling is the default profiling mode, but you can specify it // explicitly for completeness. defer profile.Start(profile.CPUProfile).Stop() }
Output:
func MemProfile ¶
func MemProfile(p *profile)
MemProfile controls if memory profiling will be enabled. It disables any previous profiling settings.
Example ¶
package main import ( "github.com/pkg/profile" ) func main() { // use memory profiling, rather than the default cpu profiling. defer profile.Start(profile.MemProfile).Stop() }
Output:
func MemProfileRate ¶
func MemProfileRate(rate int) func(*profile)
MemProfileRate controls if memory profiling will be enabled. Additionally, it takes a parameter which allows the setting of the memory profile rate.
Example ¶
package main import ( "github.com/pkg/profile" ) func main() { // use memory profiling with custom rate. defer profile.Start(profile.MemProfileRate(2048)).Stop() }
Output:
func NoShutdownHook ¶
func NoShutdownHook(p *profile)
NoShutdownHook controls whether the profiling package should hook SIGINT to write profiles cleanly. Programs with more sophisticated signal handling should set this to true and ensure the Stop() function returned from Start() is called during shutdown.
Example ¶
package main import ( "github.com/pkg/profile" ) func main() { // disable the automatic shutdown hook. defer profile.Start(profile.NoShutdownHook).Stop() }
Output:
func ProfilePath ¶
func ProfilePath(path string) func(*profile)
ProfilePath controls the base path where various profiling files are written. If blank, the base path will be generated by ioutil.TempDir.
Example ¶
package main import ( "os" "github.com/pkg/profile" ) func main() { // set the location that the profile will be written to defer profile.Start(profile.ProfilePath(os.Getenv("HOME"))) }
Output:
func Start ¶
func Start(options ...func(*profile)) interface { Stop() }
Start starts a new profiling session. The caller should call the Stop method on the value returned to cleanly stop profiling.
Example ¶
package main import ( "github.com/pkg/profile" ) func main() { // start a simple CPU profile and register // a defer to Stop (flush) the profiling data. defer profile.Start().Stop() }
Output:
Example (WithFlags) ¶
package main import ( "flag" "github.com/pkg/profile" ) func main() { // use the flags package to selectively enable profiling. mode := flag.String("profile.mode", "", "enable profiling mode, one of [cpu, mem, block]") flag.Parse() switch *mode { case "cpu": defer profile.Start(profile.CPUProfile).Stop() case "mem": defer profile.Start(profile.MemProfile).Stop() case "block": defer profile.Start(profile.BlockProfile).Stop() default: // do nothing } }
Output:
Types ¶
This section is empty.