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 ¶
- type Stopwatch
- func (s Stopwatch) Elapsed(unit time.Duration) time.Duration
- func (s Stopwatch) ElapsedTime(unit time.Duration) int64
- func (s Stopwatch) IsRunning() bool
- func (s *Stopwatch) Reset() *Stopwatch
- func (s *Stopwatch) Start() (*Stopwatch, error)
- func (s *Stopwatch) Stop() (*Stopwatch, error)
- func (s Stopwatch) String() string
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 ¶
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 ¶
CreateUnstartedClock creates (but does not start) a new stopwatch, using the specified time source.
func (Stopwatch) Elapsed ¶
Elapsed returns the current elapsed time shown on this stopwatch as a Duration.
func (Stopwatch) ElapsedTime ¶
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 ¶
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 ¶
Reset sets the elapsed time for this stopwatch to zero, and places it in a stopped state.