avegner / fperf

Go func and method performance measurement with ease

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

Go func and method performance measurement with ease

License:MIT License


Languages

Language:Go 100.0%