profile

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2019 License: BSD-2-Clause Imports: 9 Imported by: 0

README

profile

Simple profiling support package for Go with multi profiling support

Build Status GoDoc

installation

go get github.com/biter777/profile

usage

Enabling profiling in your application is as simple as one line at the top of your main function.

By default CPU, Memory, Mutex, Block profiling and Trace out is enabled.

import "github.com/biter777/profile"

func main() {
    defer profile.Start().Stop()
    ...
}

options

What to profile is controlled by config value passed to profile.Start.

By default CPU, Memory, Mutex, Block profiling and Trace out is enabled.

import "github.com/biter777/profile"

func main() {
    // p.Stop() must be called before the program exits to
    // ensure profiling information is written to disk.
    p := profile.Start(profile.CPUProfile, profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook)
    ...
}

Several convenience package level values are provided for cpu, memory, and block (contention) profiling.

For more complex options, consult the documentation.

contributing

(c) by Dave Cheney, modified by biter

Original package is: "github.com/pkg/profile", thnx to Dave Cheney, he is really cool.

We welcome pull requests, bug fixes and issue reports.

Before proposing a change, please discuss it first by raising an issue.

Documentation

Overview

Package profile provides a simple way to manage runtime/pprof profiling of your Go application. Multi profiling supported. (c) by Dave Cheney <dave@cheney.net>, modified by biter <biter2004@yandex.ru> ( Original package is: "github.com/pkg/profile", thnx to Dave Cheney, he is really cool )

Index

Examples

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 BlockProfile

func BlockProfile(p *Profile)

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

func CPUProfile

func CPUProfile(p *Profile)

CPUProfile enables cpu profiling. It NOT disables any previous profiling settings (multi profiling supported).

Example
package main

import (
	"github.com/biter777/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 enables memory profiling. It NOT disables any previous profiling settings (multi profiling supported).

Example
package main

import (
	"github.com/biter777/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 enables memory profiling at the preferred rate. It NOT disables any previous profiling settings (multi profiling supported).

Example
package main

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

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

func MutexProfile added in v1.2.1

func MutexProfile(p *Profile)

MutexProfile enables mutex profiling. It NOT disables any previous profiling settings (multi profiling supported).

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
package main

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

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

func ProfileAll added in v1.2.2

func ProfileAll(p *Profile)

ProfileAll set to enables CPUProfile, MemProfile, MutexProfile, BlockProfile and TraceProfile. Multi profiling supported.

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/biter777/profile"
)

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

func ProfilePathLocalDir added in v1.2.2

func ProfilePathLocalDir(p *Profile)

ProfilePathLocalDir setup the base path where various profiling files are written to: .../LocalDir/profile12345. (12345 - auto generated)

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
package main

import (
	"github.com/biter777/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/biter777/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
	}
}
Output:

func TraceProfile added in v1.2.0

func TraceProfile(p *Profile)

TraceProfile profile controls if execution tracing will be enabled. It NOT disables any previous profiling settings (multi profiling supported).

Example
package main

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

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

Types

type Profile added in v1.2.1

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

Profile represents an active profiling session.

func (*Profile) Stop added in v1.2.1

func (p *Profile) Stop()

Stop stops the profile and flushes any unwritten data.

Jump to

Keyboard shortcuts

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