Documentation
¶
Index ¶
- Constants
- type FormattingMode
- type Lap
- type Stopwatch
- func (s *Stopwatch) ElapsedTime() time.Duration
- func (s *Stopwatch) ElapsedTimeFrom(now time.Time) time.Duration
- func (s *Stopwatch) Lap(state string) Lap
- func (s *Stopwatch) LapTime() time.Duration
- func (s *Stopwatch) LapWithData(state string, data map[string]interface{}) Lap
- func (s *Stopwatch) LapWithDataAndTime(now time.Time, state string, data map[string]interface{}) Lap
- func (s *Stopwatch) Laps() []Lap
- func (s *Stopwatch) MarshalJSON() ([]byte, error)
- func (s *Stopwatch) Reset(offset time.Duration, active bool)
- func (s *Stopwatch) SetFormatter(formatter func(time.Duration) string)
- func (s *Stopwatch) SetFormattingMode(newMode FormattingMode)
- func (s *Stopwatch) Start()
- func (s *Stopwatch) Stop()
- func (s *Stopwatch) String() string
Examples ¶
Constants ¶
const VERSION = "1.0.0"
VERSION represents the version of this library
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FormattingMode ¶
type FormattingMode string
const ( // FormattingModeJsonArray formats Stopwatch to the form of an array of laps [{},{},...] FormattingModeJsonArray FormattingMode = "JSON_ARRAY" // FormattingModeJsonSimpleObject formats Stopwatch to the form object with property-per-lap {"Lap1":"10ms", "Lap2":"20ms"} // It's compatitable with ELK. Does not support additional lap data FormattingModeJsonSimpleObject FormattingMode = "JSON_OBJECT" // FormattingModeJsonMsObject formats Stopwatch to the form object with property-per-lap, where values are numbers {"Lap1":10.1, "Lap2":20.2} // It's compatitable with ELK. Does not support additional lap data FormattingModeJsonMsObject FormattingMode = "JSON_OBJECT_MS" )
type Lap ¶
type Lap struct {
// contains filtered or unexported fields
}
Lap represents a split time from the stopwatch
type Stopwatch ¶
Stopwatch is a non high-resolution timer for recording elapsed time deltas to give you some insight into how long things take for your app
Example (MultiThread) ¶
// Create a new StopWatch that starts off counting
sw := New(0, true)
// Optionally, format that time.Duration how you need it
sw.SetFormatter(func(duration time.Duration) string {
return fmt.Sprintf("%.0f", duration.Seconds())
})
// Take measurement of various states
sw.Lap("Create File")
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
for i := 0; i < 2; i++ {
task := fmt.Sprintf("task %d", i)
sw.Lap(task)
}
}()
go func() {
defer wg.Done()
task := "task A"
sw.LapWithData(task, map[string]interface{}{
"filename": "word.doc",
})
}()
// Simulate some time by sleeping
sw.Lap("Upload File")
// Stop the timer
wg.Wait()
sw.Stop()
laps := sw.Laps()
sorted := make([]string, 0, len(laps))
for _, lap := range laps {
sorted = append(sorted, lap.String())
}
sort.Strings(sorted)
for _, lap := range sorted {
fmt.Println(lap)
}
Output: {"state":"Create File", "time":"0"} {"state":"Upload File", "time":"0"} {"state":"task 0", "time":"0"} {"state":"task 1", "time":"0"} {"state":"task A", "time":"0", "filename":"word.doc"}
Example (SingleThread) ¶
// Create a new StopWatch that starts off counting
sw := New(0, true)
// Optionally, format that time.Duration how you need it
sw.SetFormatter(func(duration time.Duration) string {
return fmt.Sprintf("%.0f", duration.Seconds())
})
// Take measurement of various states
sw.Lap("Create File")
sw.Lap("Edit File")
sw.Lap("Upload File")
// Take a measurement with some additional metadata
sw.LapWithData("Delete File", map[string]interface{}{
"filename": "word.doc",
})
// Stop the timer
sw.Stop()
// Marshal to json
if b, err := json.Marshal(sw); err == nil {
fmt.Println(string(b))
}
Output: [{"state":"Create File","time":"0"},{"state":"Edit File","time":"0"},{"state":"Upload File","time":"0"},{"state":"Delete File","time":"0","filename":"word.doc"}]
func New ¶
New 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.
func (*Stopwatch) ElapsedTime ¶
ElapsedTime is the time the stopwatch has been active
func (*Stopwatch) ElapsedTimeFrom ¶ added in v1.0.2
ElapsedTimeFrom is the time the stopwatch has been active till 'now'
func (*Stopwatch) LapWithData ¶
LapWithData starts a new lap, and returns the length of the previous one allowing the user to pass in additional metadata to be recorded.
func (*Stopwatch) LapWithDataAndTime ¶ added in v1.0.2
func (s *Stopwatch) LapWithDataAndTime(now time.Time, state string, data map[string]interface{}) Lap
LapWithDataAndTime starts a new lap from 'now' timestamp, and returns the length of the previous one allowing the user to pass in additional metadata to be recorded.
func (*Stopwatch) MarshalJSON ¶
MarshalJSON converts into a slice of bytes
func (*Stopwatch) SetFormatter ¶
SetFormatter takes a function that converts time.Duration into a string
func (*Stopwatch) SetFormattingMode ¶
func (s *Stopwatch) SetFormattingMode(newMode FormattingMode)
Laps returns a slice of completed lap times