stopwatch

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2021 License: BSD-2-Clause Imports: 3 Imported by: 60

README

stopwatch

stopwatch - a simple package to provide timer functionality

While there are other stopwatch implementations none of them seemed to exactly match what I was looking for. This is very simple and intended for collecting run times inside an application.

Installation

Install the package with: go get github.com/sjmudd/stopwatch

Contributing

Patches and improvements to this package are welcome.

Licensing

BSD 2-Clause License

Feedback

Feedback and patches welcome.

Simon J Mudd sjmudd@pobox.com

Code Documentation

godoc.org/github.com/sjmudd/stopwatch

Documentation

Overview

Package stopwatch implements simple stopwatch functionality

Package stopwatch implements simple stopwatch functionality This package is only thread safe for the AddElapsed() call. Other usage assumes that usage is single threaded.

Index

Constants

This section is empty.

Variables

View Source
var DefaultFormat = func(t time.Duration) string { return t.String() }

DefaultFormat allows the Stopwatch.String() function to be configured differently to time.Duration if needed. This is done at the global package level to avoid having to do on each Stopwatch instance.

Functions

This section is empty.

Types

type NamedStopwatch

type NamedStopwatch struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

NamedStopwatch holds a map of string named stopwatches. Intended to be used when several Stopwatches are being used at once, and easy to use as they are name based.

func NewNamedStopwatch

func NewNamedStopwatch() *NamedStopwatch

NewNamedStopwatch creates an empty Stopwatch list

func (*NamedStopwatch) Add

func (ns *NamedStopwatch) Add(name string) error

Add adds a single Stopwatch name with the given name.

func (*NamedStopwatch) AddElapsedSince

func (ns *NamedStopwatch) AddElapsedSince(name string, t time.Time)

AddElapsedSince adds the duration since the reference time to the given named stopwatch.

func (*NamedStopwatch) AddMany

func (ns *NamedStopwatch) AddMany(names []string) error

AddMany adds several named stopwatches in one go

func (*NamedStopwatch) Delete

func (ns *NamedStopwatch) Delete(name string)

Delete removes a Stopwatch with the given name (if it exists)

func (*NamedStopwatch) Elapsed

func (ns *NamedStopwatch) Elapsed(name string) time.Duration

Elapsed returns the elapsed time.Duration of the named stopwatch if it exists or 0

func (*NamedStopwatch) ElapsedMilliSeconds

func (ns *NamedStopwatch) ElapsedMilliSeconds(name string) float64

ElapsedMilliSeconds returns the elapsed time in milliseconds of the named stopwatch if it exists or 0.

func (*NamedStopwatch) ElapsedSeconds

func (ns *NamedStopwatch) ElapsedSeconds(name string) float64

ElapsedSeconds returns the elapsed time in seconds of the named stopwatch if it exists or 0.

func (*NamedStopwatch) Exists

func (ns *NamedStopwatch) Exists(name string) bool

Exists returns true if the NamedStopwatch exists

func (*NamedStopwatch) Keys

func (ns *NamedStopwatch) Keys() []string

Keys returns the known names of Stopwatches

func (*NamedStopwatch) Reset

func (ns *NamedStopwatch) Reset(name string)

Reset resets a NamedStopwatch if it exists

func (*NamedStopwatch) SetLogger added in v0.1.1

func (ns *NamedStopwatch) SetLogger(logger logger)

SetLogger is used to set the logger interface

func (*NamedStopwatch) Start

func (ns *NamedStopwatch) Start(name string)

Start starts a NamedStopwatch if it exists

func (*NamedStopwatch) StartMany

func (ns *NamedStopwatch) StartMany(names []string)

StartMany allows you to start several stopwatches in one go

func (*NamedStopwatch) Stop

func (ns *NamedStopwatch) Stop(name string)

Stop stops a NamedStopwatch if it exists

func (*NamedStopwatch) StopMany

func (ns *NamedStopwatch) StopMany(names []string)

StopMany allows you to stop several stopwatches in one go

type Stopwatch

type Stopwatch struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Stopwatch is a structure to hold information about a stopwatch

func New

func New(f func(time.Duration) string) *Stopwatch

New returns a pointer to a new Stopwatch struct.

func Start

func Start(f func(time.Duration) string) *Stopwatch

Start returns a pointer to a new Stopwatch struct and indicates that the stopwatch has started.

func (*Stopwatch) AddElapsedSince

func (s *Stopwatch) AddElapsedSince(t time.Time)

AddElapsed just adds an elapsed time to the value that's been stored.

func (*Stopwatch) Elapsed

func (s *Stopwatch) Elapsed() time.Duration

Elapsed returns the elapsed time since starting (in time.Duration).

func (*Stopwatch) ElapsedMilliSeconds

func (s *Stopwatch) ElapsedMilliSeconds() float64

ElapsedMilliSeconds is a helper function returns the number of milliseconds since starting.

func (*Stopwatch) ElapsedSeconds

func (s *Stopwatch) ElapsedSeconds() float64

ElapsedSeconds is a helper function returns the number of seconds since starting.

func (*Stopwatch) IsRunning

func (s *Stopwatch) IsRunning() bool

IsRunning is a helper function to indicate if in theory the stopwatch is working.

func (*Stopwatch) Reset

func (s *Stopwatch) Reset()

Reset resets the counters.

func (*Stopwatch) SetStringFormat

func (s *Stopwatch) SetStringFormat(f func(time.Duration) string)

SetStringFormat allows the String() function to be configured differently to time.Duration for the specific Stopwatch.

func (*Stopwatch) Start

func (s *Stopwatch) Start()

Start records that we are now running. If called previously this is a no-op and the existing refTime is not touched.

func (*Stopwatch) Stop

func (s *Stopwatch) Stop()

Stop collects the elapsed time if running and remembers we are not running.

func (*Stopwatch) String

func (s *Stopwatch) String() string

String gives the string representation of the duration.

Jump to

Keyboard shortcuts

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