Back to godoc.org
github.com/pkg/profile

Package profile

v1.2.1
Latest Go to latest

The highest tagged major version is .

Published: May 9, 2017 | License: BSD-2-Clause | Module: github.com/pkg/profile

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

func BlockProfile

func BlockProfile(p *Profile)

BlockProfile enables block (contention) profiling. It disables any previous profiling settings.

func CPUProfile

func CPUProfile(p *Profile)

CPUProfile enables cpu profiling. It disables any previous profiling settings.

Example

Code:

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()
}

func MemProfile

func MemProfile(p *Profile)

MemProfile enables memory profiling. It disables any previous profiling settings.

Example

Code:

package main

import (
	"github.com/pkg/profile"
)

func main() {
	// use memory profiling, rather than the default cpu profiling.
	defer profile.Start(profile.MemProfile).Stop()
}

func MemProfileRate

func MemProfileRate(rate int) func(*Profile)

MemProfileRate enables memory profiling at the preferred rate. It disables any previous profiling settings.

Example

Code:

package main

import (
	"github.com/pkg/profile"
)

func main() {
	// use memory profiling with custom rate.
	defer profile.Start(profile.MemProfileRate(2048)).Stop()
}

func MutexProfile

func MutexProfile(p *Profile)

MutexProfile enables mutex profiling. It disables any previous profiling settings.

Mutex profiling is a no-op before go1.8.

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

Code:

package main

import (
	"github.com/pkg/profile"
)

func main() {
	// disable the automatic shutdown hook.
	defer profile.Start(profile.NoShutdownHook).Stop()
}

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

Code:

package main

import (
	"github.com/pkg/profile"
	"os"
)

func main() {
	// set the location that the profile will be written to
	defer profile.Start(profile.ProfilePath(os.Getenv("HOME"))).Stop()
}

func Quiet

func Quiet(p *Profile)

Quiet suppresses informational messages during profiling.

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

Code:

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()
}
Example (WithFlags)

Code:

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, mutex, block]")
	flag.Parse()
	switch *mode {
	case "cpu":
		defer profile.Start(profile.CPUProfile).Stop()
	case "mem":
		defer profile.Start(profile.MemProfile).Stop()
	case "mutex":
		defer profile.Start(profile.MutexProfile).Stop()
	case "block":
		defer profile.Start(profile.BlockProfile).Stop()
	default:
		// do nothing
	}
}

func TraceProfile

func TraceProfile(p *Profile)

Trace profile controls if execution tracing will be enabled. It disables any previous profiling settings.

Example

Code:

package main

import (
	"github.com/pkg/profile"
)

func main() {
	// use execution tracing, rather than the default cpu profiling.
	defer profile.Start(profile.TraceProfile).Stop()
}

type Profile

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

Profile represents an active profiling session.

func (*Profile) Stop

func (p *Profile) Stop()

Stop stops the profile and flushes any unwritten data.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier