Profile stopwatch that works like a stop watch, you start the stopwatch and call the step function every time 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.
go get -u github.com/inigolabs/stopwatch
func main() {
sw := stopwatch.Start()
doSomeStuff()
sw.Step("doSomeStuff")
doSomeMoreStuff()
sw.Step("doSomeMoreStuff")
doEvenMoreStuff()
sw.Step("doEvenMoreStuff")
sw.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
func main() {
router := chi.NewRouter()
router.Use(stopwatch.StopWatchMiddleware)
router.Get("/", get)
http.ListenAndServe(":80", router)
}
func get(w http.ResponseWriter, r *http.Request) {
sw := stopwatch.StopWatchFromContext(r.Context())
doSomeStuff()
sw.Step("doSomeStuff")
doSomeMoreStuff()
sw.Step("doSomeMoreStuff")
doEvenMoreStuff()
sw.Step("doEvenMoreStuff")
}
Often, you'll want to leave the profile step function calls in the code and only run them in debug or profile mode. In this case you can use Start()
when in debug mode, and otherwise instantiate the stopwatch using StartNoopStopWatch()
.