profile

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2020 License: Apache-2.0 Imports: 10 Imported by: 7

README

multi-profile

PkgGoDev GoVersion License

BuildStatus LatestRelease

LatestTag LastCommit Issues PullRequests

Multi-profiling support package for Go.

This project was inspired by pkg/profile but there is a fundamental difference: multi-profile offers the possibility to start multiple profiling at the same time.

Installation

go get github.com/bygui86/multi-profile

Usage

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

For example:

package main

import "github.com/bygui86/multi-profile/v2"

func main() {
    defer profile.CPUProfile(&profile.Config{}).Start().Stop()
    
    // ...
}

Using profile specific method, you can create the kind of profiling you want giving a Config as input.

package main

import "github.com/bygui86/multi-profile/v2"

func main() {
    defer profile.CPUProfile(&profile.Config{}).Start().Stop()
    defer profile.MemProfile(&profile.Config{}).Start().Stop()
    defer profile.GoroutineProfile(&profile.Config{}).Start().Stop()

    // ...
}

(i)️ INFO see examples folder for all available profiles and samples.

/!\ WARN if not using EnableInterruptHook option (see below) ALWAYS remember to defer Stop() function, otherwise the profile won't stop and flush to file properly.

Options

(i)️️ INFO see examples for all usage samples.

Path

You can customize the path in which a profile file is going to be written.

Use field Path and UseTempPath in the Config.

Interruption hook

You can enable an interruption hook that runs a new goroutine waiting for interruption signals (syscall.SIGTERM, syscall.SIGINT and os.Interrupt). If one of those signals arrives, the profiling packages stop the Profile and flushes results to file. Enabling this option, you can avoid deferring Stop() function in the main.

Use EnableInterruptHook field in the Config.

Quiet mode

You can suppress all logs.

Use field Quiet in the Config.

Closer function

You can call a function right after stopping the profiling.

Use CloserHook field in the Config.

Panic in case of profile failure

Per default the profile won't cause a panic in case of failure, it will simply log the error. In case you want to panic the whole application just set PanicIfFail to true in the Config.

Contributing

I welcome pull requests, bug fixes and issue reports.

To propose an extensive change, please discuss it first by opening an issue.

Thanks

Documentation

Overview

Package profile provides a simple way to manage multiple runtime/pprof profiling of your Go application

Index

Constants

View Source
const (

	// DefaultPath holds the default path where to create pprof file
	DefaultPath = "./"

	/*
		DefaultMemProfileRate holds the default memory profiling rate
		See also http://golang.org/pkg/runtime/#pkg-variables
	*/
	DefaultMemProfileRate = 4096

	// DefaultMemProfileRate holds the default memory profiling type
	DefaultMemProfileType = MemProfileHeap

	// Supported memory profiles
	MemProfileHeap   MemProfileType = "heap"
	MemProfileAllocs MemProfileType = "allocs"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	/*
		Path holds the base path where various profiling files are  written
		If blank, the base path will be the current directory "./"
	*/
	Path string

	// UseTempPath let the path be generated by "ioutil.TempDir"
	UseTempPath bool

	// PanicIfFail holds the flag to decide whether a profile failure causes a panic
	PanicIfFail bool

	// EnableInterruptHook controls whether to start a goroutine to wait for interruption signals to stop profiling
	EnableInterruptHook bool

	// Quiet suppresses informational messages during profiling
	Quiet bool

	/*
		MemProfileRate holds the rate for the memory profile
		See DefaultMemProfileRate for default value
	*/
	MemProfileRate int

	/*
		MemProfileType holds the type for the memory profile
		Available values:   heap | allocs
		See DefaultMemProfileType for default
	*/
	MemProfileType MemProfileType

	// CloserHook holds a custom cleanup function that run after profiling Stop
	CloserHook func()

	// Logger offers the possibility to inject a custom logger
	Logger Logger
}

Config holds configurations to create a new Profile

type Logger

type Logger interface {
	Debug(...interface{})
	Info(...interface{})
	Warn(...interface{})
	Error(...interface{})
	Fatal(...interface{})

	Debugf(string, ...interface{})
	Infof(string, ...interface{})
	Warnf(string, ...interface{})
	Errorf(string, ...interface{})
	Fatalf(string, ...interface{})
}

Logger defines the interface an external logger have to implement, to be passed and used by multi-profile

type MemProfileType

type MemProfileType string

MemProfileType defines which type of memory profiling you want to start

type Profile

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

Profile represents a profiling session

func BlockProfile

func BlockProfile(cfg *Config) *Profile

BlockProfile creates a block (contention) profiling object

func CPUProfile

func CPUProfile(cfg *Config) *Profile

CPUProfile creates a CPU profiling object

func GoroutineProfile

func GoroutineProfile(cfg *Config) *Profile

GoroutineProfile creates a goroutine profiling object

func MemProfile

func MemProfile(cfg *Config) *Profile

MemProfile creates a memory profiling object

func MutexProfile

func MutexProfile(cfg *Config) *Profile

MutexProfile creates a mutex profiling object

func ThreadCreationProfile

func ThreadCreationProfile(cfg *Config) *Profile

ThreadCreationProfile creates a thread creation profiling object

func TraceProfile

func TraceProfile(cfg *Config) *Profile

TraceProfile creates an execution tracing profiling object

func (*Profile) Start

func (p *Profile) Start() *Profile

Start starts a new profiling session

func (*Profile) Stop

func (p *Profile) Stop()

Stop stops the profiling and flushes any unwritten data. The caller should call the Stop method on the value returned to cleanly stop profiling.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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