profiletimer

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2021 License: MIT Imports: 8 Imported by: 1

README

Profile Timer

A simple utility tool to profile go code.

Profile timer works like a stop watch, you start the timer and call the step function everytime you want to add a new data point. At the end you can print a summary of all the steps and the amount of time each step took.

Installation

go get -u github.com/ejoffe/profiletimer

Usage

func main() {
    timer := profiletimer.StartProfileTimer()

    doSomeStuff()
    timer.Step("doSomeStuff")
    doSomeMoreStuff()
    timer.Step("doSomeMoreStuff")
    doEvenMoreStuff()
    timer.Step("doEvenMoreStuff")

    timer.ShowResults()
}

The code above will print out a summary to stdout with the amount of time each step took.

doSomeStuff     : 2165.694534ms
doSomeMoreStuff : 11.568080ms
doEvenMoreStuff : 541.535541ms
-------------------------------
total           : 2718.798155ms

Middleware

func main() {
    router := chi.NewRouter()
    router.Use(profiletimer.TimerMiddleware)
    router.Get("/", get)
    http.ListenAndServe(":80", router)
}

func get(w http.ResponseWriter, r *http.Request) {
    timer := profiletimer.TimerFromContext(r.Context())
    doSomeStuff()
    timer.Step("doSomeStuff")
    doSomeMoreStuff()
    timer.Step("doSomeMoreStuff")
    doEvenMoreStuff()
    timer.Step("doEvenMoreStuff")
}
Noop Timer

Often, you'll want to leave the profile step functions in the code and only run them in debug or profile mode. In this case you can use StartProfileTimer() when in debug mode, and otherwise instantiate the timer using StartNoopTimer().

License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartNoopTimer

func StartNoopTimer() *noopTimer

StartNoopTimer creates a timer that does nothing. This is useful in cases

where you want to keep the timer step call in production code, but don't
want them to have any logical or performance effects.

func StartProfileTimer

func StartProfileTimer() *profileTimer

StartProfileTimer creates a profile timer and starts it.

func TimerMiddleware added in v0.2.0

func TimerMiddleware(next http.Handler) http.Handler

Types

type Timer

type Timer interface {
	Step(label string)
	WriteResults(w io.Writer) error
	ShowResults() error
}

Timer is the interface for profile or stopwatch timer.

func TimerFromContext added in v0.2.0

func TimerFromContext(ctx context.Context) Timer

Jump to

Keyboard shortcuts

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