Documentation
¶
Overview ¶
Package benchmarks implements a benchmark library with mean, median and arbitrary quantiles. It includes a pretty-printing function of duration -- that can be arbitrarily configured.
Example 1: Simple example:
testParams := []int{1, 2, 3, 5, 8}
testFns := make([]benchmarks.NamedFunc, len(testParams))
for ii, param := range testParams {
testFns.Name = fmt.Printf("Param=%d", param)
testFns.Func() = func() {
MyFunc(param)
}
}
benchmarks.New(testFns...).Done()
Example 2: Measuring a CGO call -- we use inner-repeats because the time is very small.
const repeats = 1000
repeatedCGO := func() {
for _ = range repeats {
dummyCGO(unsafe.Pointer(plugin.api))
}
}
benchmarks.New(benchmarks.NamedFunction{"CGOCall", repeatedCGO}).
WithInnerRepeats(repeats).
Done()
Index ¶
- Variables
- func PrettyPrint(d time.Duration) string
- type NamedFunction
- type Options
- func (o *Options) Done()
- func (o *Options) WithColumnSize(columnSize int) *Options
- func (o *Options) WithDuration(duration time.Duration) *Options
- func (o *Options) WithHeader(printHeader bool) *Options
- func (o *Options) WithInnerRepeats(innerRepeats int) *Options
- func (o *Options) WithPrettyPrintFn(fn func(time.Duration) string) *Options
- func (o *Options) WithQuantiles(quantiles ...int) *Options
- func (o *Options) WithTolerance(tolerance float64) *Options
- func (o *Options) WithWarmUps(warmUps int) *Options
Constants ¶
This section is empty.
Variables ¶
var DefaultQuantiles = []int{5, 99}
DefaultQuantiles to use in benchmarking. It can be changed for a particular benchmark using Options.WithQuantiles.
Functions ¶
func PrettyPrint ¶
Types ¶
type NamedFunction ¶
type NamedFunction struct {
Name string
Func func()
}
NamedFunction holds a function to be benchmarked and its name.
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
func New ¶
func New(fns ...NamedFunction) *Options
New sets up a benchmark for the list of named functions fns. You can further configure the returned Options, and call Done() when finished to execute the benchmark.
Example 1: Simple example:
testParams := []int{1, 2, 3, 5, 8}
testFns := make([]benchmarks.NamedFunc, len(testParams))
for ii, param := range testParams {
testFns.Name = fmt.Printf("Param=%d", param)
testFns.Func() = func() {
MyFunc(param)
}
}
benchmarks.New(testFns...).Done()
Example 2: Measuring a CGO call -- we use inner-repeats because the time is very small.
const repeats = 1000
repeatedCGO := func() {
for _ = range repeats {
dummyCGO(unsafe.Pointer(plugin.api))
}
}
benchmarks.New(benchmarks.NamedFunction{"CGOCall", repeatedCGO}).
WithInnerRepeats(repeats).
Done()
func (*Options) WithColumnSize ¶
WithColumnSize sets the size (in number of runes) for each column reported by benchmark. Handy if setting WithPrettyPrint to something that uses more or less space.
Default is 10. Notice that any value smaller than 9 may mis-align the header row.
func (*Options) WithDuration ¶
WithDuration sets the benchmark duration for each function for the options and returns the updated Options instance. When running the benchmark (Options.Done) it will run each function for at least this amount time, collecting statistics.
func (*Options) WithHeader ¶ added in v0.1.1
WithHeader sets whether to display a header. Default is true.
func (*Options) WithInnerRepeats ¶
WithInnerRepeats sets the expected number of inner repeats of the functions given for the benchmark and returns the updated Options instance.
This only informs the inner repetitions inside the functions given, this library won't repeat any calls. If passing a value > 1 here, you must repeat the piece of code you want to benchmark inside the functions given to New.
This is particularly important for things that run below a few microseconds, as it mitigates the time to call the functions passed. Default is 1.
Notice that reported measures are divided by this number. That means changing this number shouldn't affect the reported mean.
func (*Options) WithPrettyPrintFn ¶
WithPrettyPrintFn sets a custom pretty-print function for formatting durations and returns the updated Options instance.
func (*Options) WithQuantiles ¶
WithQuantiles sets the quantiles for the options and returns the updated Options instance. Default is given by DefaultQuantiles ({5, 99})
func (*Options) WithTolerance ¶
WithTolerance sets the tolerance in the approximate quantiles calculations. The smaller the tolerance the larger the amount of memory used in approximating the quantiles -- which may impact the running time due to GC (??)
Default is 0.001 which is good enough for most cases.
func (*Options) WithWarmUps ¶
WithWarmUps sets the number of warm-up iterations (before starting the benchmark) and returns the updated Options instance.