This is a module allowing to measure run time of functions and methods in Go.
go get github.com/avegner/fperf
There are 2 options to measure run time:
- defer special helper function
func FuncToMeasure() {
defer MeasureRunTime(reportRunTime)()
// some code
}
- embed measurement
func FuncToMeasure() {
// some code
}
func main() {
ef := EmbedRunTimeMeasurement(FuncToMeasure, reportRunTime).(func())
ef()
}
Both options use a callback to report function's (method's) name and run duration:
type ReportCallback func(name string, duration time.Duration)
An example of the callback:
func reportRunTime(name string, duration time.Duration) {
fmt.Fprintf(os.Stderr, "%q func took %v\n", name, duration)
}
Thus an app can decide how to print measurements.