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.
Index ¶
- Constants
- Variables
- func EnableAll(r *http.Request) bool
- func FS(useLocal bool) http.FileSystem
- func FuncName(f interface{}) string
- func Hostname() string
- func MiniProfilerHandler(w http.ResponseWriter, r *http.Request)
- func Since(t time.Time) float64
- func StoreMemory(r *http.Request, p *Profile)
- type ClientTiming
- type ClientTimings
- type CustomTiming
- type Handler
- type Profile
- func (p *Profile) AddCustomLink(name, URL string)
- func (p *Profile) AddCustomTiming(callType, executeType string, start, end time.Time, command string)
- func (p *Profile) Finalize()
- func (p *Profile) Includes() template.HTML
- func (p *Profile) Json() []byte
- func (p *Profile) SetName(name string)
- func (p *Profile) Step(name string, f func(t Timer))
- func (p *Profile) StepCustomTiming(callType, executeType, command string, f func())
- type Timer
- type Timing
- func (T *Timing) AddCustomLink(name, URL string)
- func (t *Timing) AddCustomTiming(callType, executeType string, start, end time.Time, command string)
- func (T *Timing) Includes() template.HTML
- func (T *Timing) SetName(name string)
- func (T *Timing) Step(name string, f func(t Timer))
- func (t *Timing) StepCustomTiming(callType, executeType, command string, f func())
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.11" )
Functions ¶
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 StoreMemory ¶
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 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 Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func NewHandler ¶
NewHandler returns a new profiled handler.
func (Handler) ProfileRequest ¶
func (h Handler) ProfileRequest(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 NewProfile ¶
NewProfile creates a new Profile with given name. For use only by miniprofiler extensions.
func ProfileFromJson ¶
ProfileFromJson returns a Profile from JSON data.
func (*Profile) AddCustomLink ¶
func (*Profile) AddCustomTiming ¶
func (*Profile) Finalize ¶
func (p *Profile) Finalize()
Finalize finalizes a Profile and Store()s it. For use only by miniprofiler extensions.
func (*Profile) StepCustomTiming ¶
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 }