Codebase list golang-github-vbauerster-mpb / 46e1846
NewThreadSafeMovingAverage Vladimir Bauer 6 years ago
3 changed file(s) with 22 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
2222 }
2323
2424 // EwmaETA exponential-weighted-moving-average based ETA decorator.
25 // Note that it's necessary to supply bar.Incr* methods with incremental
26 // work duration as second argument, in order for this decorator to
27 // work correctly. This decorator is a wrapper of MovingAverageETA.
25 // For this decorator to work correctly you have to measure each
26 // iteration's duration and pass it to the
27 // *Bar.DecoratorEwmaUpdate(time.Duration) method after each increment.
2828 func EwmaETA(style TimeStyle, age float64, wcc ...WC) Decorator {
2929 var average ewma.MovingAverage
3030 if age == 0 {
3232 } else {
3333 average = ewma.NewMovingAverage(age)
3434 }
35 average = &ThreadSafeMovingAverage{MovingAverage: average}
36 return MovingAverageETA(style, average, nil, wcc...)
35 return MovingAverageETA(style, NewThreadSafeMovingAverage(average), nil, wcc...)
3736 }
3837
3938 // MovingAverageETA decorator relies on MovingAverage implementation to calculate its average.
66 "github.com/VividCortex/ewma"
77 )
88
9 // ThreadSafeMovingAverage is a thread safe wrapper for ewma.MovingAverage.
10 type ThreadSafeMovingAverage struct {
9 type threadSafeMovingAverage struct {
1110 ewma.MovingAverage
1211 mu sync.Mutex
1312 }
1413
15 func (s *ThreadSafeMovingAverage) Add(value float64) {
14 func (s *threadSafeMovingAverage) Add(value float64) {
1615 s.mu.Lock()
1716 s.MovingAverage.Add(value)
1817 s.mu.Unlock()
1918 }
2019
21 func (s *ThreadSafeMovingAverage) Value() float64 {
20 func (s *threadSafeMovingAverage) Value() float64 {
2221 s.mu.Lock()
2322 defer s.mu.Unlock()
2423 return s.MovingAverage.Value()
2524 }
2625
27 func (s *ThreadSafeMovingAverage) Set(value float64) {
26 func (s *threadSafeMovingAverage) Set(value float64) {
2827 s.mu.Lock()
2928 s.MovingAverage.Set(value)
3029 s.mu.Unlock()
30 }
31
32 // NewThreadSafeMovingAverage converts provided ewma.MovingAverage
33 // into thread safe ewma.MovingAverage.
34 func NewThreadSafeMovingAverage(average ewma.MovingAverage) ewma.MovingAverage {
35 if tsma, ok := average.(*threadSafeMovingAverage); ok {
36 return tsma
37 }
38 return &threadSafeMovingAverage{MovingAverage: average}
3139 }
3240
3341 type medianWindow [3]float64
5563
5664 // NewMedian is fixed last 3 samples median MovingAverage.
5765 func NewMedian() ewma.MovingAverage {
58 return &ThreadSafeMovingAverage{MovingAverage: new(medianWindow)}
66 return NewThreadSafeMovingAverage(new(medianWindow))
5967 }
2727 }
2828
2929 // EwmaSpeed exponential-weighted-moving-average based speed decorator.
30 // Note that it's necessary to supply bar.Incr* methods with incremental
31 // work duration as second argument, in order for this decorator to
32 // work correctly. This decorator is a wrapper of MovingAverageSpeed.
30 // For this decorator to work correctly you have to measure each
31 // iteration's duration and pass it to the
32 // *Bar.DecoratorEwmaUpdate(time.Duration) method after each increment.
3333 func EwmaSpeed(unit int, format string, age float64, wcc ...WC) Decorator {
3434 var average ewma.MovingAverage
3535 if age == 0 {
3737 } else {
3838 average = ewma.NewMovingAverage(age)
3939 }
40 average = &ThreadSafeMovingAverage{MovingAverage: average}
41 return MovingAverageSpeed(unit, format, average, wcc...)
40 return MovingAverageSpeed(unit, format, NewThreadSafeMovingAverage(average), wcc...)
4241 }
4342
4443 // MovingAverageSpeed decorator relies on MovingAverage implementation