stopwatch

package
v0.0.0-...-7272a43 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package stopwatch provides means to measures elapsed time in nanoseconds.

Example
package main

import (
	"fmt"
	"time"

	"github.com/abc-inc/goava/base/stopwatch"
	"github.com/jonboulle/clockwork"
)

func main() {
	clock := clockwork.NewFakeClock()
	s := stopwatch.CreateStartedClock(clock)

	// doSomething()
	clock.Advance(12300 * time.Microsecond) // for the sake of the example, we turn the watch hand

	_, _ = s.Stop() // optional
	duration := s.ElapsedTime(time.Microsecond)
	fmt.Println(duration)
	fmt.Println("time:", s.String())
}
Output:

12300
time: 12.30 ms

Index

Examples

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 measures elapsed time in nanoseconds.

It is useful to measure elapsed time using this type instead of direct calls to time.Now() for a few reasons:

• An alternate time source can be substituted, for testing or performance reasons.

• As documented by the time package, the value returned by time.Now() has no absolute meaning, and can only be interpreted as relative to another timestamp returned by time.Now() at a different time. Stopwatch is a more effective abstraction because it exposes only these relative values, not the absolute ones.

Stopwatch methods are not idempotent; it is an error to start or stop a stopwatch that is already in the desired state.

When testing code that uses Stopwatch, use CreateUnstarted(Clock) or CreateStarted(Clock) to supply a fake or mock clock. This allows you to simulate any valid behavior of the Stopwatch.

Note: This struct is not thread-safe.

func CreateStarted

func CreateStarted() *Stopwatch

CreateStarted creates (and starts) a new stopwatch using the wall clock as its time source.

func CreateStartedClock

func CreateStartedClock(clock clockwork.Clock) *Stopwatch

CreateStartedClock creates (and starts) a new stopwatch, using the specified time source.

func CreateUnstarted

func CreateUnstarted() *Stopwatch

CreateUnstarted creates (but does not start) a new stopwatch using the wall clock as its time source.

func CreateUnstartedClock

func CreateUnstartedClock(clock clockwork.Clock) *Stopwatch

CreateUnstartedClock creates (but does not start) a new stopwatch, using the specified time source.

func (Stopwatch) Elapsed

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

Elapsed returns the current elapsed time shown on this stopwatch as a Duration.

func (Stopwatch) ElapsedTime

func (s Stopwatch) ElapsedTime(unit time.Duration) int64

ElapsedTime returns the current elapsed time shown on this stopwatch, expressed in the desired time unit, with any fraction rounded down.

Note: the overhead of measurement can be more than a microsecond, so it is generally not useful to specify time.Nanosecond precision here.

It is generally not a good idea to use an ambiguous, unitless int64 to represent elapsed time. Therefore, we recommend using Elapsed(Duration) instead, which returns a strongly-typed Duration instance.

func (Stopwatch) IsRunning

func (s Stopwatch) IsRunning() bool

IsRunning returns true if Start has been called on this stopwatch, and Stop has not been called since the last call to Start.

func (*Stopwatch) Reset

func (s *Stopwatch) Reset() *Stopwatch

Reset sets the elapsed time for this stopwatch to zero, and places it in a stopped state.

func (*Stopwatch) Start

func (s *Stopwatch) Start() (*Stopwatch, error)

Start starts the stopwatch.

func (*Stopwatch) Stop

func (s *Stopwatch) Stop() (*Stopwatch, error)

Stop stops the stopwatch. Future reads will return the fixed duration that had elapsed up to this point.

func (Stopwatch) String

func (s Stopwatch) String() string

String returns a string representation of the current elapsed time.

Jump to

Keyboard shortcuts

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