/
performance_statistics.go
76 lines (66 loc) · 2.17 KB
/
performance_statistics.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
package server
import (
"sync"
"time"
)
// DateFormat is an example to date time string format
const DateFormat = "2006-01-02T15:04:05.000000"
// EndpointStats has statistics on an endpoint
type EndpointStats struct {
CountSuccesses int `json:"count_successes"`
CountFails int `json:"count_fails"`
TotalDuration uint64 `json:"total_duration"`
MaxDuration uint64 `json:"max_duration"`
}
// PerformanceStats has statistics in an interval on selected endpoints
type PerformanceStats struct {
lock sync.Mutex
intervalStartTime time.Time
endpointsStats map[string]*EndpointStats
}
// NewPerformanceStats creates the PerformanceStats
func NewPerformanceStats() PerformanceStats {
return PerformanceStats{
lock: sync.Mutex{},
intervalStartTime: time.Now(),
endpointsStats: make(map[string]*EndpointStats),
}
}
// PerformanceStatsRecord has the bloxroute stats record
type PerformanceStatsRecord struct {
StartTime string `json:"start_timestamp"`
EndTime string `json:"end_timestamp"`
EndpointsStats map[string]*EndpointStats `json:"endpoints_stats"`
}
// CloseInterval closes an interval and return the bloxroute stats record
func (p *PerformanceStats) CloseInterval(endTime time.Time) PerformanceStatsRecord {
p.lock.Lock()
defer p.lock.Unlock()
statsRecord := PerformanceStatsRecord{
StartTime: p.intervalStartTime.Format(DateFormat),
EndTime: endTime.Format(DateFormat),
EndpointsStats: p.endpointsStats,
}
p.endpointsStats = make(map[string]*EndpointStats)
p.intervalStartTime = endTime
return statsRecord
}
// SetEndpointStats updates usage statistics of an endpoint
func (p *PerformanceStats) SetEndpointStats(endpoint string, duration uint64, success bool) {
p.lock.Lock()
defer p.lock.Unlock()
endpointStats, ok := p.endpointsStats[endpoint]
if !ok {
p.endpointsStats[endpoint] = &EndpointStats{}
endpointStats = p.endpointsStats[endpoint]
}
if success {
endpointStats.CountSuccesses++
endpointStats.TotalDuration += duration
if endpointStats.MaxDuration < duration {
endpointStats.MaxDuration = duration
}
} else {
endpointStats.CountFails++
}
}