github.com/haraldrudell/parl@v0.4.176/timer.go (about) 1 /* 2 © 2020–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package parl 7 8 import ( 9 "fmt" 10 "time" 11 ) 12 13 const ( 14 tms = time.Millisecond 15 ) 16 17 // Timer is a simple request timer 18 type Timer struct { 19 Label string 20 t0 time.Time 21 d time.Duration 22 } 23 24 // NewTimer gets a simple timer with duration or string output 25 func NewTimer(label string) (t *Timer) { 26 t = &Timer{Label: label, t0: time.Now()} 27 return 28 } 29 30 // End gets duration 31 func (t *Timer) End() (d time.Duration) { 32 d = time.Since(t.t0) 33 t.d = d 34 return 35 } 36 37 // Endms gets tring with duration in ms 38 func (t *Timer) Endms() (ms string) { 39 d := t.End() 40 ms = fmt.Sprintf("%s: %s", t.Label, d.Round(tms).String()) 41 return 42 }