Skip to content
/ fperf Public

Go func and method performance measurement with ease

License

Notifications You must be signed in to change notification settings

avegner/fperf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fperf

This is a module allowing to measure run time of functions and methods in Go.

Installation

go get github.com/avegner/fperf

How to Measure

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.