profile

package
v0.0.0-...-f5d25ad Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package profile implements profiling with generic events. An event can be defined by the user of this package and just needs to implement fmt.Stringer. This profiler works with string-based events. Use the profiler like this in your code.

...
type Evt string
func (e Evt) String() string { return string(e) }
...
const MyEvt = Evt("my expensive func")
...
func main() {
    prof = profile.NewProfiler()
    MyExpensiveFunc()
    fmt.Println(prof.Profile().String()) // will print the profile
}
...
func MyExpensiveFunc() {
    defer prof.Enter(MyEvt).Exit()
    ...
}

The above example will print a profile with one event, the respective timestamps and durations etc.

NOTE: You don't need an actual profiler. All methods will also work on nil profilers, such as the following.

var prof *profile.Profiler
defer prof.Enter(MyEvt).Exit()

Enter(...) will just create an empty Event, and Exit() will do nothing. This was implemented this way, so that no no-op implementation of a profiler is neccessary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clearer

type Clearer interface {
	Clear()
}

Clearer wraps a basic clear method, which will clear the components contents. What exactly is cleared, must be documented by the component.

type Event

type Event struct {
	Object   fmt.Stringer
	Start    time.Time
	Duration time.Duration
	// contains filtered or unexported fields
}

Event is a simple profiling event. It keeps a back reference to the profiler it originated from.

func (Event) Exit

func (e Event) Exit()

Exit passes the event back to the origin profiler. When using this, unlike using (*Profiler).Exit(Event), an event duration will be set.

type Profile

type Profile struct {
	Events []Event
}

Profile is a collection of profiling events that were collected by a profiler.

func (Profile) String

func (p Profile) String() string

type Profiler

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

Profiler is a profiler that can collect events.

func NewProfiler

func NewProfiler() *Profiler

NewProfiler returns a new, ready to use profiler.

func (*Profiler) Clear

func (p *Profiler) Clear()

Clear removes all collected events.

func (*Profiler) Enter

func (p *Profiler) Enter(object fmt.Stringer) Event

Enter creates a profiling event. Use like this:

defer profiler.Enter(MyEvent).Exit()

func (*Profiler) Exit

func (p *Profiler) Exit(evt Event)

Exit collects the given event. You can call Exit multiple times with the same event, it will them appear multiple times in the profiler's profile.

func (*Profiler) Profile

func (p *Profiler) Profile() Profile

Profile returns a profile with all collected events from the profiler. The collected events are NOT cleared after this. To clear all events, use (*Profiler).Clear().

Jump to

Keyboard shortcuts

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