Documentation
¶
Overview ¶
Package hrtime implements High-Resolution Timing functions for benchmarking.
`hrtime` relies on using the best timing mechanism on a particular system. At the moment, for Windows it is using Performance Counters and on other platforms standard `time.Now` (since it's good enough).
Package also supports using hardware time stamp counters (TSC). They offer better accuracy and on some platforms correspond to the processor cycles. However, they are not supported on all platforms.
The basic usage of this package looks like:
package main import ( "fmt" "github.com/loov/hrtime" ) func main() { const numberOfExperiments = 4096 bench := hrtime.NewBenchmark(numberOfExperiments) for bench.Next() { time.Sleep(10) } fmt.Println(bench.Histogram(10)) }
To see more complex examples refer to the _example folder. (https://github.com/loov/hrtime/tree/master/_example)
Index ¶
- func Now() time.Duration
- func NowPrecision() float64
- func Overhead() time.Duration
- func RDTSC() uint64
- func RDTSCP() uint64
- func Since(start time.Duration) time.Duration
- func TSCSupported() bool
- type Benchmark
- func (bench *Benchmark) Float64s() []float64
- func (bench *Benchmark) Histogram(binCount int) *Histogram
- func (bench *Benchmark) HistogramClamp(binCount int, min, max time.Duration) *Histogram
- func (bench *Benchmark) Laps() []time.Duration
- func (bench *Benchmark) Name() string
- func (bench *Benchmark) Next() bool
- func (bench *Benchmark) Unit() string
- type BenchmarkTSC
- func (bench *BenchmarkTSC) Counts() []Count
- func (bench *BenchmarkTSC) Float64s() []float64
- func (bench *BenchmarkTSC) Histogram(binCount int) *Histogram
- func (bench *BenchmarkTSC) HistogramClamp(binCount int, min, max time.Duration) *Histogram
- func (bench *BenchmarkTSC) Laps() []time.Duration
- func (bench *BenchmarkTSC) Name() string
- func (bench *BenchmarkTSC) Next() bool
- func (bench *BenchmarkTSC) Unit() string
- type Count
- type Histogram
- type HistogramBin
- type HistogramOptions
- type Span
- type SpanTSC
- type Stopwatch
- func (bench *Stopwatch) Durations() []time.Duration
- func (bench *Stopwatch) Float64s() []float64
- func (bench *Stopwatch) Histogram(binCount int) *Histogram
- func (bench *Stopwatch) HistogramClamp(binCount int, min, max time.Duration) *Histogram
- func (bench *Stopwatch) Name() string
- func (bench *Stopwatch) Spans() []Span
- func (bench *Stopwatch) Start() int32
- func (bench *Stopwatch) Stop(lap int32)
- func (bench *Stopwatch) Unit() string
- func (bench *Stopwatch) Wait()
- type StopwatchTSC
- func (bench *StopwatchTSC) ApproxDurations() []time.Duration
- func (bench *StopwatchTSC) Float64s() []float64
- func (bench *StopwatchTSC) Histogram(binCount int) *Histogram
- func (bench *StopwatchTSC) HistogramClamp(binCount int, min, max time.Duration) *Histogram
- func (bench *StopwatchTSC) Name() string
- func (bench *StopwatchTSC) Spans() []SpanTSC
- func (bench *StopwatchTSC) Start() int32
- func (bench *StopwatchTSC) Stop(lap int32)
- func (bench *StopwatchTSC) Unit() string
- func (bench *StopwatchTSC) Wait()
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Now ¶
Now returns current time.Duration with best possible precision.
Now returns time offset from a specific time. The values aren't comparable between computer restarts or between computers.
func NowPrecision ¶
func NowPrecision() float64
NowPrecision returns maximum possible precision for Nanos in nanoseconds.
func RDTSC ¶
func RDTSC() uint64
RDTSC returns Read Time-Stamp Counter value using RDTSC asm instruction.
If a platform doesn't support the instruction it returns 0. Use TSCSupported to check.
func RDTSCP ¶
func RDTSCP() uint64
RDTSCP returns Read Time-Stamp Counter value using RDTSCP asm instruction.
If a platform doesn't support the instruction it returns 0. Use TSCSupported to check.
func TSCSupported ¶
func TSCSupported() bool
TSCSupported returns whether processor supports giving invariant time stamp counter values
Types ¶
type Benchmark ¶
type Benchmark struct {
// contains filtered or unexported fields
}
Benchmark helps benchmarking using time.
Example ¶
package main import ( "fmt" "time" "github.com/loov/hrtime" ) func main() { const numberOfExperiments = 4096 bench := hrtime.NewBenchmark(numberOfExperiments) for bench.Next() { time.Sleep(1000 * time.Nanosecond) } fmt.Println(bench.Histogram(10)) }
Output:
func NewBenchmark ¶
NewBenchmark creates a new benchmark using time. Count defines the number of samples to measure.
func (*Benchmark) Histogram ¶
Histogram creates an histogram of all the laps.
It creates binCount bins to distribute the data and uses the 99.9 percentile as the last bucket range. However, for a nicer output it might choose a larger value.
func (*Benchmark) HistogramClamp ¶
HistogramClamp creates an historgram of all the laps clamping minimum and maximum time.
It creates binCount bins to distribute the data and uses the maximum as the last bucket.
type BenchmarkTSC ¶
type BenchmarkTSC struct {
// contains filtered or unexported fields
}
BenchmarkTSC helps benchmarking using CPU counters.
Example ¶
package main import ( "fmt" "time" "github.com/loov/hrtime" ) func main() { const numberOfExperiments = 4096 bench := hrtime.NewBenchmarkTSC(numberOfExperiments) for bench.Next() { time.Sleep(1000 * time.Nanosecond) } fmt.Println(bench.Histogram(10)) }
Output:
func NewBenchmarkTSC ¶
func NewBenchmarkTSC(count int) *BenchmarkTSC
NewBenchmarkTSC creates a new benchmark using CPU counters. Count defines the number of samples to measure.
func (*BenchmarkTSC) Counts ¶
func (bench *BenchmarkTSC) Counts() []Count
Counts returns counts for each lap.
func (*BenchmarkTSC) Float64s ¶
func (bench *BenchmarkTSC) Float64s() []float64
Float64s returns all measurements as float64s
func (*BenchmarkTSC) Histogram ¶
func (bench *BenchmarkTSC) Histogram(binCount int) *Histogram
Histogram creates an histogram of all the laps.
It creates binCount bins to distribute the data and uses the 99.9 percentile as the last bucket range. However, for a nicer output it might choose a larger value.
func (*BenchmarkTSC) HistogramClamp ¶
func (bench *BenchmarkTSC) HistogramClamp(binCount int, min, max time.Duration) *Histogram
HistogramClamp creates an historgram of all the laps clamping minimum and maximum time.
It creates binCount bins to distribute the data and uses the maximum as the last bucket.
func (*BenchmarkTSC) Laps ¶
func (bench *BenchmarkTSC) Laps() []time.Duration
Laps returns timing for each lap using the approximate conversion of Count.
func (*BenchmarkTSC) Name ¶
func (bench *BenchmarkTSC) Name() string
Name returns name of the benchmark.
func (*BenchmarkTSC) Next ¶
func (bench *BenchmarkTSC) Next() bool
Next starts measuring the next lap. It will return false, when all measurements have been made.
func (*BenchmarkTSC) Unit ¶
func (bench *BenchmarkTSC) Unit() string
Unit returns units it measures.
type Count ¶
type Count int64
Count represents represents Time Stamp Counter value, when available.
Count doesn't depend on power throttling making it useful for benchmarking. However it is not reliably convertible to a reasonable time-value.
func TSC ¶
func TSC() Count
TSC reads the current Time Stamp Counter value.
Reminder: Time Stamp Count are processor specific and need to be converted to time.Duration with Count.ApproxDuration.
func TSCSince ¶
TSCSince returns count since start.
Reminder: Count is processor specific and need to be converted to time.Duration with Count.ApproxDuration.
func (Count) ApproxDuration ¶
ApproxDuration returns approximate conversion into a Duration.
First call to this function will do calibration and can take several milliseconds.
type Histogram ¶
type Histogram struct { Minimum float64 Average float64 Maximum float64 P50, P90, P99, P999, P9999 float64 Bins []HistogramBin // for pretty printing Width int }
Histogram is a binned historgram with different statistics.
func NewDurationHistogram ¶
func NewDurationHistogram(durations []time.Duration, opts *HistogramOptions) *Histogram
NewDurationHistogram creates a histogram from time.Duration-s.
func NewHistogram ¶
func NewHistogram(nanoseconds []float64, opts *HistogramOptions) *Histogram
NewHistogram creates a new histogram from the specified nanosecond values.
func (*Histogram) StringStats ¶
StringStats returns a string representation of the histogram stats.
func (*Histogram) WriteStatsTo ¶
WriteStatsTo writes formatted statistics to w.
type HistogramBin ¶
type HistogramBin struct { Start float64 Count int Width float64 // contains filtered or unexported fields }
HistogramBin is a single bin in histogram
type HistogramOptions ¶
type HistogramOptions struct { BinCount int // NiceRange will try to round the bucket sizes to have a nicer output. NiceRange bool // Clamp values to either percentile or to a specific ns value. ClampMaximum float64 ClampPercentile float64 }
HistogramOptions is configuration.
type SpanTSC ¶
SpanTSC defines a Count span
func (*SpanTSC) ApproxDuration ¶
ApproxDuration returns the approximate duration of the span.
type Stopwatch ¶
type Stopwatch struct {
// contains filtered or unexported fields
}
Stopwatch allows concurrent benchmarking using Now
Example ¶
package main import ( "fmt" "time" "github.com/loov/hrtime" ) func main() { const numberOfExperiments = 4096 bench := hrtime.NewStopwatch(numberOfExperiments) for i := 0; i < numberOfExperiments; i++ { go func() { lap := bench.Start() defer bench.Stop(lap) time.Sleep(1000 * time.Nanosecond) }() } bench.Wait() fmt.Println(bench.Histogram(10)) }
Output:
func NewStopwatch ¶
NewStopwatch creates a new concurrent benchmark using Now
func (*Stopwatch) Histogram ¶
Histogram creates an histogram of all the durations.
It creates binCount bins to distribute the data and uses the 99.9 percentile as the last bucket range. However, for a nicer output it might choose a larger value.
func (*Stopwatch) HistogramClamp ¶
HistogramClamp creates an historgram of all the durations clamping minimum and maximum time.
It creates binCount bins to distribute the data and uses the maximum as the last bucket.
func (*Stopwatch) Start ¶
Start starts measuring a new lap. It returns the lap number to pass in for Stop. It will return -1, when all measurements have been made.
Call to Stop with -1 is ignored.
type StopwatchTSC ¶
type StopwatchTSC struct {
// contains filtered or unexported fields
}
StopwatchTSC allows concurrent benchmarking using TSC
Example ¶
package main import ( "fmt" "time" "github.com/loov/hrtime" ) func main() { const numberOfExperiments = 4096 bench := hrtime.NewStopwatchTSC(numberOfExperiments) for i := 0; i < numberOfExperiments; i++ { go func() { lap := bench.Start() defer bench.Stop(lap) time.Sleep(1000 * time.Nanosecond) }() } bench.Wait() fmt.Println(bench.Histogram(10)) }
Output:
func NewStopwatchTSC ¶
func NewStopwatchTSC(count int) *StopwatchTSC
NewStopwatchTSC creates a new concurrent benchmark using TSC
func (*StopwatchTSC) ApproxDurations ¶
func (bench *StopwatchTSC) ApproxDurations() []time.Duration
ApproxDurations returns measured durations.
func (*StopwatchTSC) Float64s ¶
func (bench *StopwatchTSC) Float64s() []float64
Float64s returns all measurements.
func (*StopwatchTSC) Histogram ¶
func (bench *StopwatchTSC) Histogram(binCount int) *Histogram
Histogram creates an histogram of all the durations.
It creates binCount bins to distribute the data and uses the 99.9 percentile as the last bucket range. However, for a nicer output it might choose a larger value.
func (*StopwatchTSC) HistogramClamp ¶
func (bench *StopwatchTSC) HistogramClamp(binCount int, min, max time.Duration) *Histogram
HistogramClamp creates an historgram of all the durations clamping minimum and maximum time.
It creates binCount bins to distribute the data and uses the maximum as the last bucket.
func (*StopwatchTSC) Name ¶
func (bench *StopwatchTSC) Name() string
Name returns name of the benchmark.
func (*StopwatchTSC) Spans ¶
func (bench *StopwatchTSC) Spans() []SpanTSC
Spans returns measured time-spans.
func (*StopwatchTSC) Start ¶
func (bench *StopwatchTSC) Start() int32
Start starts measuring a new lap. It returns the lap number to pass in for Stop. It will return -1, when all measurements have been made.
Call to Stop with -1 is ignored.
func (*StopwatchTSC) Stop ¶
func (bench *StopwatchTSC) Stop(lap int32)
Stop stops measuring the specified lap.
Call to Stop with -1 is ignored.
func (*StopwatchTSC) Unit ¶
func (bench *StopwatchTSC) Unit() string
Unit returns units it measures.
func (*StopwatchTSC) Wait ¶
func (bench *StopwatchTSC) Wait()
Wait waits for all measurements to be completed.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
_example
|
|
basic
This program demonstrates the basic usage of the package.
|
This program demonstrates the basic usage of the package. |
benchtime
benchtime implements measuring and plotting of timing overheads.
|
benchtime implements measuring and plotting of timing overheads. |
plotting
plotting demonstrates how to combine hrtime with plot package.
|
plotting demonstrates how to combine hrtime with plot package. |
hrplot
module
|
|
Package hrtesting implements wrappers for testing.B that allow to output more detailed information in standard go benchmarks.
|
Package hrtesting implements wrappers for testing.B that allow to output more detailed information in standard go benchmarks. |