timer

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2018 License: Apache-2.0 Imports: 1 Imported by: 22

Documentation

Overview

Package timer is a small convenience package for timing blocks of code.

Example

Example of combining a timer with a defer to make it easy to put all your timing code at the top of a function.

defer func(t Timer) {
	dur := t.Finish()
	fmt.Printf("log my duration as %g\n", dur)
}(Start())
Output:

Example (Block)

Example_block when timing non-funciton based locks, separate the start and finish

// do some work
t := Start()
// do some more work
dur := t.Finish()
fmt.Printf("log my duration as %g\n", dur)
Output:

Example (OtherTime)

Example_othertime for when starting from Now isn't quite right

actualStart := time.Unix(1525150486, 0)
t := New(actualStart)
// do some work
dur := t.Finish()
fmt.Printf("log my duration as %g\n", dur)
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Timer

type Timer interface {
	// Finish calculates the time since the timer was started and returns its
	// representation in milliseconds
	Finish() float64
}

Timer is the thing that you pass around to time blocks of code. Represented as an interface so that other implementations can do fancy things with the values in addition to just returning them, such as submit them to instrumentation frameworks.

func New

func New(t time.Time) Timer

New creates a new timer with an arbitrary starting time

func Start

func Start() Timer

Start creates a new timer using `time.Now()` as the starting time

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL