event.go 505 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package perf
  2. import (
  3. "math"
  4. "sync"
  5. "time"
  6. )
  7. type Event struct {
  8. Mu sync.RWMutex
  9. Count int64
  10. Total time.Duration
  11. Min time.Duration
  12. Max time.Duration
  13. }
  14. func (e *Event) Add(t time.Duration) {
  15. e.Mu.Lock()
  16. defer e.Mu.Unlock()
  17. if t > e.Max {
  18. e.Max = t
  19. }
  20. if t < e.Min {
  21. e.Min = t
  22. }
  23. e.Count++
  24. e.Total += t
  25. }
  26. func (e *Event) MeanTime() time.Duration {
  27. e.Mu.RLock()
  28. defer e.Mu.RUnlock()
  29. return e.Total / time.Duration(e.Count)
  30. }
  31. func (e *Event) Init() {
  32. e.Min = math.MaxInt64
  33. }