-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistic.go
115 lines (99 loc) · 2.52 KB
/
statistic.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
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2024, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.
package bus
import (
"strings"
"time"
"go.uber.org/atomic"
)
type Statistic struct {
min *atomic.Duration
max *atomic.Duration
avg *atomic.Duration
rps *RPSCounter
}
func NewStatistic() *Statistic {
return &Statistic{
min: atomic.NewDuration(0),
max: atomic.NewDuration(0),
avg: atomic.NewDuration(0),
rps: startRPSCounter(),
}
}
func (s *Statistic) setTime(t time.Duration) {
if s.min.Load() == 0 {
s.min.Store(t)
}
if s.min.Load() > t {
s.min.Store(t)
}
if s.max.Load() == 0 {
s.max.Store(t)
}
if t > s.max.Load() {
s.max.Store(t)
}
s.avg.Store((s.max.Load() + s.min.Load()) / 2)
}
// StatItem ...
type StatItem struct {
Topic string
Subscribers int
Min time.Duration
Max time.Duration
Avg time.Duration
Rps float64
}
// Stats ...
type Stats []*StatItem
func (s Stats) Len() int { return len(s) }
func (s Stats) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s Stats) Less(i, j int) bool { return strings.Compare(s[i].Topic, s[j].Topic) == -1 }
type RPSCounter struct {
count *atomic.Int64
value *atomic.Float64
isRunning *atomic.Bool
}
func startRPSCounter() *RPSCounter {
counter := &RPSCounter{
count: atomic.NewInt64(0),
value: atomic.NewFloat64(0),
isRunning: atomic.NewBool(true),
}
go func() {
ticker := time.NewTicker(time.Second * 5)
defer ticker.Stop()
for counter.isRunning.Load() {
select {
case <-ticker.C:
counter.value.Store(float64(counter.count.Load()) / 5)
counter.count.Store(0)
}
}
}()
return counter
}
func (c *RPSCounter) Inc() {
c.count.Inc()
}
func (c *RPSCounter) Value() float64 {
return c.value.Load()
}
func (c *RPSCounter) Stop() {
c.isRunning.Store(false)
}