go: github.com/MiniProfiler/go/miniprofiler Index | Files

package miniprofiler

import "github.com/MiniProfiler/go/miniprofiler"

Package miniprofiler is a simple but effective mini-profiler for websites.

To use this package, change your HTTP handler functions to use this signature:

func(miniprofiler.Timer, http.ResponseWriter, *http.Request)

Register them in the usual way, wrapping them with NewHandler.

By default, all requests are profiled. This should be changed to profile only developer requests. Set miniprofiler.Enable to a function that returns true if profiling is enabled. It might resemble this:

miniprofiler.Enable = func(r *http.Request) bool {
	return isUserAuthenticated(r)
}

By default, profile results are stored in memory in a concurrent-safe data structure. To store in redis, memcache, or something else, set miniprofiler.Store and miniprofiler.Get to functions to back the profile data. The key is Profile.Id.

Send output of t.Includes() to your HTML (it is empty if Enable returns false).

Step

The Step function can be used to profile more specific parts of your code. It should be called with the name of the step and a closure. Further Timers are used so concurrent work can be done and results applied to the correct location.

t.Step("something", func(t miniprofiler.Timer) {
	// do some work
	// t.Step("another", func(t miniprofiler.Timer) { ... })
})

StepCustomTiming

StepCustomTiming can be used to record any kind of call (redis, RPC, etc.)

t.StepCustomTiming(
	"redis",       // call type
	"get",         // execute type
	"get key_name" // command string
	func() {
		// do work
	}
)

Example

This is a small example using this package.

package main

import (
	"fmt"
	"net/http"

	"github.com/MiniProfiler/go/miniprofiler"
)

func Index(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) {
	t.Step("something", func(t miniprofiler.Timer) {
		t.StepCustomTiming("RPC", "get", "get key_name", func() {
			// some RPC call
		})
	})
	fmt.Fprintf(w, "<html><body>%v</body></html>", p.Includes())
}

func main() {
	http.Handle("/", miniprofiler.NewHandler(Index))
	http.ListenAndServe(":8080", nil)
}

Configuration

Refer to the variables section of the documentation: http://godoc.org/github.com/MiniProfiler/go/miniprofiler#pkg-variables.

Other implementations and resources: http://miniprofiler.com.

Frameworks

Various frameworks have explicit support.

Google App Engine: http://godoc.org/github.com/MiniProfiler/go/miniprofiler_gae

Revel: http://godoc.org/github.com/MiniProfiler/go/miniprofiler_revel

Martini: http://godoc.org/github.com/MiniProfiler/go/miniprofiler_martini

Beego: http://godoc.org/github.com/MiniProfiler/go/miniprofiler_beego

Traffic: http://godoc.org/github.com/MiniProfiler/go/miniprofiler_traffic

gocraft/web: http://godoc.org/github.com/MiniProfiler/go/miniprofiler_gocraftweb

RPCs

Various RPCs have explicit support.

Redis: http://godoc.org/github.com/MiniProfiler/go/redis

SQL: http://godoc.org/github.com/MiniProfiler/go/sql

Index

Package Files

context.go doc.go miniprofiler.go static.go templates.go types.go

Constants

const (
    PATH = "/mini-profiler-resources/"
)

Variables

var (
    // Enable returns true if the request should be profiled.
    Enable func(*http.Request) bool = EnableAll

    // Store stores the Profile by its Id field.
    Store func(*http.Request, *Profile) = StoreMemory

    // Get retrieves a Profile by its Id field.
    Get func(*http.Request, string) *Profile = GetMemory

    // MachineName returns the machine name to display.
    // The default is to use the machine's hostname.
    MachineName func() string = Hostname

    // Valid positions: left, right, bottomleft, bottomright
    Position = "left"

    ShowTrivial         = false
    ShowChildren        = false
    MaxTracesToShow     = 15
    ShowControls        = true
    ToggleShortcut      = "Alt+P"
    StartHidden         = false
    TrivialMilliseconds = 12.0

    Version = "3.0.12"
)

func Dir Uses

func Dir(useLocal bool, name string) http.FileSystem

Dir returns a http.Filesystem for the embedded assets on a given prefix dir. If useLocal is true, the filesystem's contents are instead used.

func EnableAll Uses

func EnableAll(r *http.Request) bool

EnableAll returns true.

func FS Uses

func FS(useLocal bool) http.FileSystem

FS returns a http.Filesystem for the embedded assets. If useLocal is true, the filesystem's contents are instead used.

func FSByte Uses

func FSByte(useLocal bool, name string) ([]byte, error)

FSByte returns the named file from the embedded assets. If useLocal is true, the filesystem's contents are instead used.

func FSMustByte Uses

func FSMustByte(useLocal bool, name string) []byte

FSMustByte is the same as FSByte, but panics if name is not present.

func FSMustString Uses

func FSMustString(useLocal bool, name string) string

FSMustString is the string version of FSMustByte.

func FSString Uses

func FSString(useLocal bool, name string) (string, error)

FSString is the string version of FSByte.

func FuncName Uses

func FuncName(f interface{}) string

FuncName returns the name of the function f, or "" if f is not a function.

func Hostname Uses

func Hostname() string

Hostname returns the os.Hostname() of the current machine, or "" if unavailable.

func MiniProfilerHandler Uses

func MiniProfilerHandler(w http.ResponseWriter, r *http.Request)

miniProfilerHandler serves requests to the /mini-profiler-resources/ path. For use only by miniprofiler helper libraries.

func NewContextHandler Uses

func NewContextHandler(f http.Handler) http.Handler

NewContextHandler creates a ContextHandler to wrap the given http.HandlerFunc. A profiler will be added to the request Context, and can be retreived with miniprofiler.GetTimer(r)

func NewNamedContextHandler Uses

func NewNamedContextHandler(f http.Handler, name string) http.Handler

NewNamedContextHandler creates a ContextHandler to wrap the given http.HandlerFunc. A profiler will be added to the request Context, and can be retreived with miniprofiler.GetTimer(r)

func Since Uses

func Since(t time.Time) float64

Since returns the number of milliseconds since t.

func StoreMemory Uses

func StoreMemory(r *http.Request, p *Profile)

StoreMemory stores a profile in memory (concurrent-safe). Note that profiles do not expire, so memory usage will increase until restart. This function is provided as an example: it is not designed for production use.

type ClientTiming Uses

type ClientTiming struct {
    Name     string
    Start    int64
    Duration int64
}

type ClientTimings Uses

type ClientTimings struct {
    RedirectCount int64
    Timings       []*ClientTiming
}

func (*ClientTimings) Len Uses

func (c *ClientTimings) Len() int

func (*ClientTimings) Less Uses

func (c *ClientTimings) Less(i, j int) bool

func (*ClientTimings) Swap Uses

func (c *ClientTimings) Swap(i, j int)

type ContextHandler Uses

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

ContextHandler is an alternate handler that passes the profiler on the http request's context, rather than as function arguments. This approach is more compatible with standard net/http Handlers.

func (ContextHandler) ServeHTTP Uses

func (h ContextHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type CustomTiming Uses

type CustomTiming struct {
    Id                             string
    ExecuteType                    string
    CommandString                  string
    StackTraceSnippet              string
    StartMilliseconds              float64
    DurationMilliseconds           float64
    FirstFetchDurationMilliseconds float64
}

type Handler Uses

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

func NewHandler Uses

func NewHandler(f func(Timer, http.ResponseWriter, *http.Request)) Handler

NewHandler returns a new profiled handler.

func (Handler) ProfileRequest Uses

func (h Handler) ProfileRequest(w http.ResponseWriter, r *http.Request)

func (Handler) ServeHTTP Uses

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Profile Uses

type Profile struct {
    Id   string
    Name string

    Started              int64
    MachineName          string
    Root                 *Timing
    ClientTimings        *ClientTimings
    DurationMilliseconds float64
    CustomLinks          map[string]string
    // contains filtered or unexported fields
}

func GetMemory Uses

func GetMemory(r *http.Request, id string) *Profile

GetMemory fetches a profile stored by StoreMemory (concurrent-safe).

func NewProfile Uses

func NewProfile(w http.ResponseWriter, r *http.Request, name string) *Profile

NewProfile creates a new Profile with given name. For use only by miniprofiler extensions.

func ProfileFromJson Uses

func ProfileFromJson(b []byte) *Profile

ProfileFromJson returns a Profile from JSON data.

func (p *Profile) AddCustomLink(name, URL string)

func (*Profile) AddCustomTiming Uses

func (p *Profile) AddCustomTiming(callType, executeType string, start, end time.Time, command string)

func (*Profile) Finalize Uses

func (p *Profile) Finalize()

Finalize finalizes a Profile and Store()s it. For use only by miniprofiler extensions.

func (*Profile) Includes Uses

func (p *Profile) Includes() template.HTML

Includes renders the JavaScript includes for this request, if enabled.

func (*Profile) Json Uses

func (p *Profile) Json() []byte

Json converts a profile to JSON.

func (*Profile) SetName Uses

func (p *Profile) SetName(name string)

func (*Profile) Step Uses

func (p *Profile) Step(name string, f func(t Timer))

func (*Profile) StepCustomTiming Uses

func (p *Profile) StepCustomTiming(callType, executeType, command string, f func())

type Timer Uses

type Timer interface {
    AddCustomTiming(callType, executeType string, start, end time.Time, command string)
    Step(name string, f func(t Timer))
    StepCustomTiming(callType, executeType, command string, f func())
    AddCustomLink(name, URL string)
    SetName(string)
    Includes() template.HTML
}

func GetTimer Uses

func GetTimer(r *http.Request) Timer

GetTimer will retreive the timer from the given http request's context. If the request has not been wrapped by a ContextHandler, nil will be returned.

func GetTimerFromContext Uses

func GetTimerFromContext(ctx context.Context) Timer

GetTimerFromContext will retreive the timer from the given context. If the given context has not been wrapped by a ContextHandler, nil will be returned.

type Timing Uses

type Timing struct {
    Id                   string
    Name                 string
    DurationMilliseconds float64
    StartMilliseconds    float64
    Children             []*Timing
    CustomTimings        map[string][]*CustomTiming

    sync.Mutex
    // contains filtered or unexported fields
}
func (T *Timing) AddCustomLink(name, URL string)

func (*Timing) AddCustomTiming Uses

func (t *Timing) AddCustomTiming(callType, executeType string, start, end time.Time, command string)

func (*Timing) Includes Uses

func (T *Timing) Includes() template.HTML

func (*Timing) SetName Uses

func (T *Timing) SetName(name string)

func (*Timing) Step Uses

func (T *Timing) Step(name string, f func(t Timer))

func (*Timing) StepCustomTiming Uses

func (t *Timing) StepCustomTiming(callType, executeType, command string, f func())

Package miniprofiler imports 22 packages (graph) and is imported by 42 packages. Updated 2020-03-20. Refresh now. Tools for package owners.