| 1 |
package memmetrics |
| 2 |
|
| 3 |
import "time" |
| 4 |
|
| 5 |
// RatioCounter calculates a ratio of a/a+b over a rolling window of predefined buckets. |
| 6 |
type RatioCounter struct { |
| 7 |
a *RollingCounter |
| 8 |
b *RollingCounter |
| 9 |
} |
| 10 |
|
| 11 |
// NewRatioCounter creates a new RatioCounter. |
| 12 |
func NewRatioCounter(buckets int, resolution time.Duration, options ...RatioOption) (*RatioCounter, error) { |
| 13 |
rc := &RatioCounter{} |
| 14 |
|
| 15 |
for _, o := range options { |
| 16 |
if err := o(rc); err != nil { |
| 17 |
return nil, err |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
a, err := NewCounter(buckets, resolution) |
| 22 |
if err != nil { |
| 23 |
return nil, err |
| 24 |
} |
| 25 |
|
| 26 |
b, err := NewCounter(buckets, resolution) |
| 27 |
if err != nil { |
| 28 |
return nil, err |
| 29 |
} |
| 30 |
|
| 31 |
rc.a = a |
| 32 |
rc.b = b |
| 33 |
return rc, nil |
| 34 |
} |
| 35 |
|
| 36 |
// Reset resets the counter. |
| 37 |
func (r *RatioCounter) Reset() { |
| 38 |
r.a.Reset() |
| 39 |
r.b.Reset() |
| 40 |
} |
| 41 |
|
| 42 |
// IsReady returns true if the counter is ready. |
| 43 |
func (r *RatioCounter) IsReady() bool { |
| 44 |
return r.a.countedBuckets+r.b.countedBuckets >= len(r.a.values) |
| 45 |
} |
| 46 |
|
| 47 |
// CountA gets count A. |
| 48 |
func (r *RatioCounter) CountA() int64 { |
| 49 |
return r.a.Count() |
| 50 |
} |
| 51 |
|
| 52 |
// CountB gets count B. |
| 53 |
func (r *RatioCounter) CountB() int64 { |
| 54 |
return r.b.Count() |
| 55 |
} |
| 56 |
|
| 57 |
// Resolution gets resolution. |
| 58 |
func (r *RatioCounter) Resolution() time.Duration { |
| 59 |
return r.a.Resolution() |
| 60 |
} |
| 61 |
|
| 62 |
// Buckets gets buckets. |
| 63 |
func (r *RatioCounter) Buckets() int { |
| 64 |
return r.a.Buckets() |
| 65 |
} |
| 66 |
|
| 67 |
// WindowSize gets windows size. |
| 68 |
func (r *RatioCounter) WindowSize() time.Duration { |
| 69 |
return r.a.WindowSize() |
| 70 |
} |
| 71 |
|
| 72 |
// ProcessedCount gets processed count. |
| 73 |
func (r *RatioCounter) ProcessedCount() int64 { |
| 74 |
return r.CountA() + r.CountB() |
| 75 |
} |
| 76 |
|
| 77 |
// Ratio gets ratio. |
| 78 |
func (r *RatioCounter) Ratio() float64 { |
| 79 |
a := r.a.Count() |
| 80 |
b := r.b.Count() |
| 81 |
// No data, return ok |
| 82 |
if a+b == 0 { |
| 83 |
return 0 |
| 84 |
} |
| 85 |
return float64(a) / float64(a+b) |
| 86 |
} |
| 87 |
|
| 88 |
// IncA increments counter A. |
| 89 |
func (r *RatioCounter) IncA(v int) { |
| 90 |
r.a.Inc(v) |
| 91 |
} |
| 92 |
|
| 93 |
// IncB increments counter B. |
| 94 |
func (r *RatioCounter) IncB(v int) { |
| 95 |
r.b.Inc(v) |
| 96 |
} |
| 97 |
|
| 98 |
// TestMeter a test meter. |
| 99 |
type TestMeter struct { |
| 100 |
Rate float64 |
| 101 |
NotReady bool |
| 102 |
WindowSize time.Duration |
| 103 |
} |
| 104 |
|
| 105 |
// GetWindowSize gets windows size. |
| 106 |
func (tm *TestMeter) GetWindowSize() time.Duration { |
| 107 |
return tm.WindowSize |
| 108 |
} |
| 109 |
|
| 110 |
// IsReady returns true if the meter is ready. |
| 111 |
func (tm *TestMeter) IsReady() bool { |
| 112 |
return !tm.NotReady |
| 113 |
} |
| 114 |
|
| 115 |
// GetRate gets rate. |
| 116 |
func (tm *TestMeter) GetRate() float64 { |
| 117 |
return tm.Rate |
| 118 |
} |