NewThreadSafeMovingAverage
Vladimir Bauer
6 years ago
| 22 | 22 |
}
|
| 23 | 23 |
|
| 24 | 24 |
// 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.
|
| 28 | 28 |
func EwmaETA(style TimeStyle, age float64, wcc ...WC) Decorator {
|
| 29 | 29 |
var average ewma.MovingAverage
|
| 30 | 30 |
if age == 0 {
|
|
| 32 | 32 |
} else {
|
| 33 | 33 |
average = ewma.NewMovingAverage(age)
|
| 34 | 34 |
}
|
| 35 | |
average = &ThreadSafeMovingAverage{MovingAverage: average}
|
| 36 | |
return MovingAverageETA(style, average, nil, wcc...)
|
|
35 |
return MovingAverageETA(style, NewThreadSafeMovingAverage(average), nil, wcc...)
|
| 37 | 36 |
}
|
| 38 | 37 |
|
| 39 | 38 |
// MovingAverageETA decorator relies on MovingAverage implementation to calculate its average.
|
| 6 | 6 |
"github.com/VividCortex/ewma"
|
| 7 | 7 |
)
|
| 8 | 8 |
|
| 9 | |
// ThreadSafeMovingAverage is a thread safe wrapper for ewma.MovingAverage.
|
| 10 | |
type ThreadSafeMovingAverage struct {
|
|
9 |
type threadSafeMovingAverage struct {
|
| 11 | 10 |
ewma.MovingAverage
|
| 12 | 11 |
mu sync.Mutex
|
| 13 | 12 |
}
|
| 14 | 13 |
|
| 15 | |
func (s *ThreadSafeMovingAverage) Add(value float64) {
|
|
14 |
func (s *threadSafeMovingAverage) Add(value float64) {
|
| 16 | 15 |
s.mu.Lock()
|
| 17 | 16 |
s.MovingAverage.Add(value)
|
| 18 | 17 |
s.mu.Unlock()
|
| 19 | 18 |
}
|
| 20 | 19 |
|
| 21 | |
func (s *ThreadSafeMovingAverage) Value() float64 {
|
|
20 |
func (s *threadSafeMovingAverage) Value() float64 {
|
| 22 | 21 |
s.mu.Lock()
|
| 23 | 22 |
defer s.mu.Unlock()
|
| 24 | 23 |
return s.MovingAverage.Value()
|
| 25 | 24 |
}
|
| 26 | 25 |
|
| 27 | |
func (s *ThreadSafeMovingAverage) Set(value float64) {
|
|
26 |
func (s *threadSafeMovingAverage) Set(value float64) {
|
| 28 | 27 |
s.mu.Lock()
|
| 29 | 28 |
s.MovingAverage.Set(value)
|
| 30 | 29 |
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}
|
| 31 | 39 |
}
|
| 32 | 40 |
|
| 33 | 41 |
type medianWindow [3]float64
|
|
| 55 | 63 |
|
| 56 | 64 |
// NewMedian is fixed last 3 samples median MovingAverage.
|
| 57 | 65 |
func NewMedian() ewma.MovingAverage {
|
| 58 | |
return &ThreadSafeMovingAverage{MovingAverage: new(medianWindow)}
|
|
66 |
return NewThreadSafeMovingAverage(new(medianWindow))
|
| 59 | 67 |
}
|
| 27 | 27 |
}
|
| 28 | 28 |
|
| 29 | 29 |
// 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.
|
| 33 | 33 |
func EwmaSpeed(unit int, format string, age float64, wcc ...WC) Decorator {
|
| 34 | 34 |
var average ewma.MovingAverage
|
| 35 | 35 |
if age == 0 {
|
|
| 37 | 37 |
} else {
|
| 38 | 38 |
average = ewma.NewMovingAverage(age)
|
| 39 | 39 |
}
|
| 40 | |
average = &ThreadSafeMovingAverage{MovingAverage: average}
|
| 41 | |
return MovingAverageSpeed(unit, format, average, wcc...)
|
|
40 |
return MovingAverageSpeed(unit, format, NewThreadSafeMovingAverage(average), wcc...)
|
| 42 | 41 |
}
|
| 43 | 42 |
|
| 44 | 43 |
// MovingAverageSpeed decorator relies on MovingAverage implementation
|