stopwatch

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2014 License: MIT Imports: 4 Imported by: 10

README

Stopwatch GoDoc Build Status

Stopwatch implements a simple stopwatch functionality. Features :

  • Two possible ways to create a Stopwatch. Initialized or uninitialized.
  • Start/Stop at any time or Reset.
  • Take an individual Lap time
  • Stores the list of each Lap
  • Satisfies JSON Marshaler/Unmarshaler interface
  • Handy methods like Print()/Log() to log a function execution time with one step.

Feel free to fork and send a pull request for any changes/improvements. For usage see examples below or click on the godoc badge.

Install

go get github.com/fatih/stopwatch

Examples

Basics
// create a new stopwatch, the timer starts immediately.
s := stopwatch.Start(0)

// get elapsed duration at any time
duration := s.ElapsedTime()
// some work ... another elapsed time
duration2 := s.ElapsedTime()

// create a new stopwatch, but do not start immediately
s := stopwatch.New()
// ... start it later
s.Start()

// start a stopwatch after a certain time
s := stopwatch.Start(2 * time.Second)
d1 := s.ElapsedTime() // d1 is zero here
// after two seconds it works
d2 := s.ElapsedTime()
Resume/Stop
// reset the stopwatch
s.Reset()
// .. or stop the stopwatch
s.Stop()

// resume the timer after a reset/stop
s.Start()
Lap
// create a lap
lap1 := s.Lap()
lap2 := s.Lap()
lap3 := s.Lap()

// get a list of all lap durations
list := s.Laps()

// lap returns zero duration if the timer is stopped/reseted
s.Stop()
lap4 := s.Lap() // lap4 == time.Duration(0)
Helpers
// String representation of stopwatch
fmt.Printf("stopwatch: %s", s)

// find out how long a function lasts
// outputs when the function returns:  myFunction - elapsed: 2.000629842s
defer Start(0).Print("myfunction")

// Marshal to a JSON object.
type API struct {
    Name      string     `json:"name"`
    Stopwatch *Stopwatch `json:"elapsed"`
}

a := API{
    Name:      "Example API Call",
    Stopwatch: Start(0),
}

// do some work ...
time.Sleep(time.Millisecond * 20)

b, err := json.Marshal(a)
if err != nil {
    t.Errorf("error: %s\n", err)
}

// output: {"name":"Example API Call","elapsed":"21.351657ms"}
fmt.Println(string(b))

// Unmarshal from a JSON object.
v := new(API)
err = json.Unmarshal(b, v)
if err != nil {
    t.Errorf("error: %s\n", err)
}

// Get back our elapsed time
duration := v.Stopwatch.ElapsedTime()

Credits

License

The MIT License (MIT) - see LICENSE.md for more details

Documentation

Overview

Package stopwatch provides a timer that implements common stopwatch functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stopwatch

type Stopwatch struct {
	// contains filtered or unexported fields
}

Stopwatch implements the stopwatch functionality. It is not threadsafe by design and should be protected when there is a need for.

func New

func New() *Stopwatch

New creates a new Stopwatch. To start the stopwatch Start() should be invoked.

func Start

func Start(offset time.Duration) *Stopwatch

Start creates a new stopwatch with starting time offset by a user defined value. Negative offsets result in a countdown prior to the start of the stopwatch. A zero offset starts the stopwatch immediately.

func (*Stopwatch) ElapsedTime

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

ElapsedTime returns the duration between the start and current time.

func (*Stopwatch) IsReseted

func (s *Stopwatch) IsReseted() bool

IsReseted shows whether the stopwatch is reseted or not.

func (*Stopwatch) IsStopped

func (s *Stopwatch) IsStopped() bool

IsStopped shows whether the stopwatch is stopped or not.

func (*Stopwatch) Lap

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

Lap takes and stores the current lap time and returns the elapsed time since the latest lap.

func (*Stopwatch) Laps

func (s *Stopwatch) Laps() []time.Duration

Laps returns a slice of all completed laps.

func (*Stopwatch) Log

func (s *Stopwatch) Log(msg string)

Log calls log.Printf() with the given string and the elapsed time attached. Useful to use with a defer statement. Example : defer Start().Log("myFunction") Output: 2014/02/10 00:44:56 myFunction - elapsed: 2.000169591s

func (*Stopwatch) MarshalJSON

func (s *Stopwatch) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. The elapsed time is quoted as a string and is in the form "72h3m0.5s". For more info please refer to time.Duration.String().

func (*Stopwatch) Print

func (s *Stopwatch) Print(msg string)

Print calls fmt.Printf() with the given string and the elapsed time attached. Useful to use with a defer statement. Example : defer Start().Print("myFunction") Output : myFunction - elapsed: 2.000629842s

func (*Stopwatch) Reset

func (s *Stopwatch) Reset()

Reset resets the timer. It needs to be started again with the Start() method.

func (*Stopwatch) Start

func (s *Stopwatch) Start(offset time.Duration)

Start resumes or starts the timer. If a Stop() was invoked it resumes the timer. If a Reset() was invoked it starts a new session with the given offset.

func (*Stopwatch) Stop

func (s *Stopwatch) Stop()

Stop stops the timer. To resume the timer Start() needs to be called again.

func (*Stopwatch) String

func (s *Stopwatch) String() string

String representation of a single Stopwatch instance.

func (*Stopwatch) UnmarshalJSON

func (s *Stopwatch) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface. The elapsed time is expected to be a string that can be successful parsed with time.ParseDuration.

Jump to

Keyboard shortcuts

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