/
stats.go
148 lines (131 loc) · 3.97 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"encoding/xml"
"fmt"
"sort"
"time"
)
// Duration allows automatic unmarshaling of a duration from XML.
type Duration struct {
Duration time.Duration
State string
}
// SetState sets the state of each percentile based on the input parameters.
func (dur *Duration) SetState(critical time.Duration, warning time.Duration) {
if dur.Duration >= critical {
dur.State = "critical"
} else if dur.Duration >= warning {
dur.State = "warning"
} else {
dur.State = "nominal"
}
}
// UnmarshalXMLAttr unmarshals a duration.
func (dur *Duration) UnmarshalXMLAttr(attr xml.Attr) (err error) {
parsed, err := time.ParseDuration(attr.Value)
if err != nil {
return
}
*dur = Duration{Duration: parsed}
return nil
}
func (dur *Duration) String() string {
return dur.Duration.String()
}
// MarshalXMLAttr implements the xml.MarshalerAttr interface.
func (dur *Duration) MarshalXMLAttr(name xml.Name) (attr xml.Attr, err error) {
attr.Name = name
attr.Value = dur.String()
return
}
// MarshalXML is a custom marshaller for Duration.
func (dur *Duration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Attr = []xml.Attr{xml.Attr{Name: xml.Name{Local: "duration"}, Value: dur.Duration.String()},
xml.Attr{Name: xml.Name{Local: "state"}, Value: dur.State},
}
e.EncodeToken(start)
e.EncodeToken(xml.EndElement{Name: start.Name})
return nil
}
// Percentages stores some Percentagess.
type Percentages struct {
MeanValue Duration `xml:"mean"`
vals []*Duration
length int
}
// MarshalXML handles the serializing into XML of Percentages.
func (p *Percentages) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
e.EncodeToken(start)
p.MeanValue.MarshalXML(e, xml.StartElement{Name: xml.Name{Local: "mean"}})
var elName string
for _, perc := range []int{0, 10, 25, 50, 66, 75, 80, 90, 95, 98, 99, 100} {
if perc == 0 {
elName = "shortest"
} else if perc == 100 {
elName = "longest"
} else {
elName = fmt.Sprintf("p%d", perc)
}
p.Percentage(perc).MarshalXML(e, xml.StartElement{Name: xml.Name{Local: elName}})
}
e.EncodeToken(xml.EndElement{Name: start.Name})
return nil
}
// Len is for the Sorter interface.
func (p *Percentages) Len() int {
return p.length
}
// Less is for the Sorter interface.
func (p *Percentages) Less(i, j int) bool {
return p.vals[i].Duration < p.vals[j].Duration
}
// Swap is for the Sorter interface.
func (p *Percentages) Swap(i, j int) {
p.vals[i], p.vals[j] = p.vals[j], p.vals[i]
}
// Percentage returns the items in position X.
func (p *Percentages) Percentage(v int) *Duration {
if v < 0 || v > 100 {
panic(fmt.Errorf("incorrect value requested %d", v))
}
if v == 0 {
return p.vals[0]
}
if v == 100 {
return p.vals[p.length-1]
}
return p.vals[int(float64(p.length)*float64(v)/100)]
}
// Mean returns the mean of this set of values.
func (p *Percentages) Mean() time.Duration {
if p.MeanValue.Duration == 0 && p.length > 0 {
sum := int64(0)
for i := 0; i < p.length; i++ {
sum += p.vals[i].Duration.Nanoseconds()
}
p.MeanValue.Duration = time.Duration(sum / int64(p.length))
}
return p.MeanValue.Duration
}
// SetState sets the state of each percentile based on the input parameters.
func (p *Percentages) SetState(critical time.Duration, warning time.Duration) {
for i := 0; i < p.length; i++ {
p.vals[i].SetState(critical, warning)
}
p.MeanValue.SetState(critical, warning)
}
//String implements the Stringer interface.
func (p *Percentages) String() string {
return fmt.Sprintf("Shortest: %s Median: %s Q3: %s P95: %s Longest: %s", p.Percentage(1).String(),
p.Percentage(50).String(), p.Percentage(75).String(), p.Percentage(95).String(), p.Percentage(100).String())
}
// NewPercentages returns a stuct which helps in serializing request results.
func NewPercentages(vals []time.Duration) *Percentages {
dVals := make([]*Duration, len(vals))
for i, v := range vals {
dVals[i] = &Duration{Duration: v}
}
p := Percentages{vals: dVals, length: len(vals)}
sort.Sort(&p)
return &p
}