profile

package module
v0.0.0-...-74c9b01 Latest Latest
Warning

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

Go to latest
Published: May 11, 2021 License: BSD-3-Clause Imports: 9 Imported by: 0

README

profile

Experiment graduated to profile project.

Experimental profiling support package for Go.

  • Based on the widely-used pkg/profile: mostly-compatible API
  • Supports multi-modal profiling: multiple profiles at once
  • Configurable with idiomatic flags: -cpuprofile, -memprofile, ... just like go test

Usage

profile should be mostly compatible with pkg/profile, so examples for that package should work here as well.

The following example shows the additional features of profile, namely multi-modal profiles and flag configuration.

package main

import (
	"flag"
	"log"

	"github.com/mmcloughlin/x/profile"
)

func main() {
	log.SetPrefix("example: ")
	log.SetFlags(0)

	// Setup profiler.
	p := profile.New(
		profile.CPUProfile,
		profile.MemProfile,
	)

	// Configure flags.
	n := flag.Int("n", 1000000, "sum the integers 1 to `n`")
	p.SetFlags(flag.CommandLine)
	flag.Parse()

	// Start profiler.
	defer p.Start().Stop()

	// Sum 1 to n.
	sum := 0
	for i := 1; i <= *n; i++ {
		sum += i
	}
	log.Printf("sum: %d", sum)
}

See the registered flags:

$ go run ./example/ -h
...
  -cpuprofile file
    	write a cpu profile to file
  -memprofile file
    	write an allocation profile to file
  -memprofilerate rate
    	set memory allocation profiling rate (see runtime.MemProfileRate)
  -n n
    	sum the integers 1 to n (default 1000000)

Profile the application:

$ go run ./example/ -n 1000000000 -cpuprofile cpu.out -memprofile mem.out
example: cpu profile: started
example: mem profile: started
example: sum: 500000000500000000
example: cpu profile: stopped
example: mem profile: stopped

$ ls *.out
cpu.out	mem.out

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BlockProfile

func BlockProfile(p *Profile)

func CPUProfile

func CPUProfile(p *Profile)

func GoroutineProfile

func GoroutineProfile(p *Profile)

func MemProfile

func MemProfile(p *Profile)

func MutexProfile

func MutexProfile(p *Profile)

func Quiet

func Quiet(p *Profile)

func ThreadcreationProfile

func ThreadcreationProfile(p *Profile)

func TraceProfile

func TraceProfile(p *Profile)

func WithLogger

func WithLogger(l *log.Logger) func(p *Profile)

Types

type Profile

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

func New

func New(options ...func(*Profile)) *Profile

func Start

func Start(options ...func(*Profile)) *Profile

func (*Profile) Configure

func (p *Profile) Configure(options ...func(*Profile))

func (*Profile) SetFlags

func (p *Profile) SetFlags(f *flag.FlagSet)

func (*Profile) Start

func (p *Profile) Start() *Profile

func (*Profile) Stop

func (p *Profile) Stop()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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