miniprofiler

package
v0.0.0-...-d539043 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2015 License: ISC Imports: 20 Imported by: 0

Documentation

Overview

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

Constants

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

Variables

View Source
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.11"
)

Functions

func EnableAll

func EnableAll(r *http.Request) bool

EnableAll returns true.

func FS

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 FuncName

func FuncName(f interface{}) string

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

func Hostname

func Hostname() string

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

func MiniProfilerHandler

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 Since

func Since(t time.Time) float64

Since returns the number of milliseconds since t.

func StoreMemory

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.

Types

type ClientTiming

type ClientTiming struct {
	Name     string
	Start    int64
	Duration int64
}

type ClientTimings

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

func (*ClientTimings) Len

func (c *ClientTimings) Len() int

func (*ClientTimings) Less

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

func (*ClientTimings) Swap

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

type CustomTiming

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

type Handler

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

func NewHandler

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

NewHandler returns a new profiled handler.

func (Handler) ProfileRequest

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

func (Handler) ServeHTTP

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

type Profile

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

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

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

func NewProfile

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

func ProfileFromJson(b []byte) *Profile

ProfileFromJson returns a Profile from JSON data.

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

func (*Profile) AddCustomTiming

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

func (*Profile) Finalize

func (p *Profile) Finalize()

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

func (*Profile) Includes

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

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

func (*Profile) Json

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

Json converts a profile to JSON.

func (*Profile) SetName

func (p *Profile) SetName(name string)

func (*Profile) Step

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

func (*Profile) StepCustomTiming

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

type Timer

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
}

type Timing

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

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

func (*Timing) Includes

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

func (*Timing) SetName

func (T *Timing) SetName(name string)

func (*Timing) Step

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

func (*Timing) StepCustomTiming

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

Jump to

Keyboard shortcuts

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