-
Notifications
You must be signed in to change notification settings - Fork 30
/
analysis.go
217 lines (198 loc) · 6.59 KB
/
analysis.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"fmt"
"log"
"time"
)
// collect ping result and divided them into a group by each min
type Group1Min struct {
Result []PingResult
TimeStamp int64
}
// statistic of ping result take-time
type TimeStatistic struct {
Min int64
Mean float64
Max int64
Stddev float64
Sum int64
}
// statistic of ping result
type PingSatistic struct {
Submitted float64
Confirmed float64
Loss float64
Count int
TimeMeasure TakeTime
TimeStatistic
Errors []string
TimeStamp int64
}
// statistic without a group
type GlobalStatistic struct {
Submitted float64
Confirmed float64
Loss float64
Count int
TimeStatistic
}
// All statistic Data
type GroupsAllStatistic struct {
PingStatisticList []PingSatistic
RawPingStaticList []PingSatistic
GlobalErrorStatistic map[string]int
GlobalStatistic
}
func (g *GroupsAllStatistic) GetGroupsAllStatistic(raw bool) GlobalStatistic {
groupStat := GlobalStatistic{}
var sumOfSubmitted, sumOfConfirmed float64
var sumOfCount int
var sumTimeMeasure TakeTime
if !raw {
for _, pg := range g.PingStatisticList {
sumOfSubmitted += pg.Submitted
sumOfConfirmed += pg.Confirmed
sumOfCount += pg.Count
sumTimeMeasure.Times = append(sumTimeMeasure.Times, pg.TimeMeasure.Times...)
}
} else { // raw data (without exception)
for _, pg := range g.RawPingStaticList {
sumOfSubmitted += pg.Submitted
sumOfConfirmed += pg.Confirmed
sumOfCount += pg.Count
sumTimeMeasure.Times = append(sumTimeMeasure.Times, pg.TimeMeasure.Times...)
}
}
groupStat.Submitted = sumOfSubmitted
groupStat.Confirmed = sumOfConfirmed
groupStat.Loss = 1
if groupStat.Submitted > 0 {
groupStat.Loss = (groupStat.Submitted - groupStat.Confirmed) / groupStat.Submitted
} else if len(g.GlobalErrorStatistic) == 0 { // No Data
groupStat.Loss = 0
}
tMax, tMean, tMin, tStddev, tSum := sumTimeMeasure.Statistic()
groupStat.TimeStatistic = TimeStatistic{
Min: tMin,
Mean: tMean,
Max: tMax,
Stddev: tStddev,
Sum: tSum,
}
return groupStat
}
// grouping1Min: group []PingResult into 1 min group.
func grouping1Min(pr []PingResult, startTime int64, endTime int64) []Group1Min {
window := int64(60)
groups := []Group1Min{}
for periodend := endTime; periodend > startTime; periodend = periodend - window {
minGroup := Group1Min{}
for _, pResult := range pr {
if pResult.TimeStamp <= periodend && pResult.TimeStamp > periodend-window {
minGroup.Result = append(minGroup.Result, pResult)
}
}
minGroup.TimeStamp = periodend
// debug: printPingResultGroup(minGroup.Result, periodend, periodend-window)
groups = append(groups, minGroup)
}
return groups
}
func statisticCompute(cConf ClusterConfig, groups []Group1Min) *GroupsAllStatistic {
stat := GroupsAllStatistic{}
stat.PingStatisticList = []PingSatistic{}
stat.RawPingStaticList = []PingSatistic{}
stat.GlobalErrorStatistic = make(map[string]int)
for _, group := range groups {
filterGroupStat := PingSatistic{}
rawGroupStat := PingSatistic{}
for _, singlePing := range group.Result {
errorException := false
errorCount := len(singlePing.Error)
if errorCount > 0 {
for _, e := range singlePing.Error {
stat.GlobalErrorStatistic[string(e)] = stat.GlobalErrorStatistic[string(e)] + 1
if PingResultError(e).IsInErrorList(StatisticErrorExceptionList) {
errorException = true
} else {
filterGroupStat.Errors = append(filterGroupStat.Errors, string(e))
}
rawGroupStat.Errors = append(rawGroupStat.Errors, string(e))
}
}
// Raw Data Statistic
rawGroupStat.TimeStamp = group.TimeStamp
rawGroupStat.Submitted += float64(singlePing.Submitted)
rawGroupStat.Confirmed += float64(singlePing.Confirmed)
rawGroupStat.Count += 1
rawGroupStat.TimeMeasure.AddTime(singlePing.TakeTime)
// Data Statistic (Filtered by error filter)
filterGroupStat.TimeStamp = group.TimeStamp // Need a ts to present the group
if !errorException {
filterGroupStat.Submitted += float64(singlePing.Submitted)
filterGroupStat.Confirmed += float64(singlePing.Confirmed)
filterGroupStat.Count += 1
if errorCount <= 0 {
filterGroupStat.TimeMeasure.AddTime(singlePing.TakeTime)
} else if (errorCount > 0) && !errorException { // general error is considered as a timeout
t := time.Duration(cConf.PingConfig.TxTimeout) * time.Second
filterGroupStat.TimeMeasure.AddTime(t.Milliseconds())
} // if StatisticErrorExceptionList , do not count as a satistic
}
}
// raw data
if rawGroupStat.Submitted == 0 { // no data
rawGroupStat.Loss = 1
} else {
rawGroupStat.Loss = (rawGroupStat.Submitted - rawGroupStat.Confirmed) / rawGroupStat.Submitted
}
tMax, tMean, tMin, tStddev, tSum := rawGroupStat.TimeMeasure.Statistic()
rawGroupStat.TimeStatistic = TimeStatistic{
Min: tMin,
Mean: tMean,
Max: tMax,
Stddev: tStddev,
Sum: tSum,
}
stat.RawPingStaticList = append(stat.RawPingStaticList, rawGroupStat)
// data with filter
if filterGroupStat.Submitted == 0 { // no data
filterGroupStat.Loss = 1
} else {
filterGroupStat.Loss = (filterGroupStat.Submitted - filterGroupStat.Confirmed) / filterGroupStat.Submitted
}
tMax, tMean, tMin, tStddev, tSum = filterGroupStat.TimeMeasure.Statistic()
filterGroupStat.TimeStatistic = TimeStatistic{
Min: tMin,
Mean: tMean,
Max: tMax,
Stddev: tStddev,
Sum: tSum,
}
stat.PingStatisticList = append(stat.PingStatisticList, filterGroupStat)
}
return &stat
}
func printPingResultGroup(pr []PingResult, from int64, to int64) {
for i, v := range pr {
fmt.Println("group ", i, "", from, "~", to, ":", v.TimeStamp)
}
}
func printStatistic(cConf ClusterConfig, stat *GroupsAllStatistic) {
for i, g := range stat.PingStatisticList {
statisticTime := fmt.Sprintf("min/mean/max/stddev ms = %d/%3.0f/%d/%3.0f",
g.TimeStatistic.Min, g.TimeStatistic.Mean, g.TimeStatistic.Max, g.TimeStatistic.Stddev)
log.Println(fmt.Sprintf("%d->{ hostname: %s, submitted: %3.0f,confirmed:%3.0f, loss: %3.1f%s, count:%d %s}",
i, cConf.HostName, g.Submitted, g.Confirmed, g.Loss*100, "%", g.Count, statisticTime))
}
for i, g := range stat.RawPingStaticList {
statisticTime := fmt.Sprintf("min/mean/max/stddev ms = %d/%3.0f/%d/%3.0f",
g.TimeStatistic.Min, g.TimeStatistic.Mean, g.TimeStatistic.Max, g.TimeStatistic.Stddev)
errString := ""
for _, v := range g.Errors {
errString = errString + v + "\n"
}
log.Println(fmt.Sprintf("RAW %d->{ hostname: %s, submitted: %3.0f,confirmed:%3.0f, loss: %3.3f%s, count:%d %s}",
i, cConf.HostName, g.Submitted, g.Confirmed, g.Loss, "%", g.Count, statisticTime))
}
}