-
Notifications
You must be signed in to change notification settings - Fork 15
/
CountTime.go
138 lines (125 loc) · 3.37 KB
/
CountTime.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
package s
import (
"fmt"
"github.com/ssgo/discover"
"github.com/ssgo/log"
"strings"
"sync"
"time"
)
type TimeStatistician struct {
startTime time.Time
endTime time.Time
stepNames []string
stepTotals []float64
stepTimes []int
stepMinimums []float64
stepMaxes []float64
total float64
times int
totalMinimum float64
totalMax float64
lock sync.Mutex
logger *log.Logger
}
type TimeCounter struct {
start int64
last int64
stepNames []string
stepTimes []float64
total float64
}
func StartTimeCounter() *TimeCounter {
now := time.Now().UnixNano()
return &TimeCounter{start: now, last: now}
}
func (t *TimeCounter) Add(name string) float64 {
now := time.Now().UnixNano()
used := float64(now-t.last) / float64(time.Millisecond)
t.last = now
t.stepNames = append(t.stepNames, name)
t.stepTimes = append(t.stepTimes, used)
return used
}
func (t *TimeCounter) Total() float64 {
if t.total == 0 {
t.total = float64(t.last-t.start) / float64(time.Millisecond)
}
return t.total
}
func (t *TimeCounter) Sprint() string {
a := make([]string, 0)
for i, name := range t.stepNames {
a = append(a, fmt.Sprintf("%s: %.4f", name, t.stepTimes[i]))
}
a = append(a, fmt.Sprintf("Total: %.4f", t.Total()))
return strings.Join(a, "\n")
}
func (t *TimeCounter) Print() {
fmt.Println(t.Sprint())
}
func NewTimeStatistic(logger *log.Logger) *TimeStatistician {
return &TimeStatistician{logger: logger, lock: sync.Mutex{}, startTime: time.Now(), stepNames: make([]string, 0), stepMinimums: make([]float64, 0), stepMaxes: make([]float64, 0), stepTotals: make([]float64, 0), stepTimes: make([]int, 0)}
}
func (t *TimeStatistician) Push(c *TimeCounter) string {
out := ""
t.lock.Lock()
if len(c.stepNames) >= len(t.stepNames) {
for i := len(t.stepNames); i < len(c.stepNames); i++ {
t.stepNames = append(t.stepNames, c.stepNames[i])
t.stepTimes = append(t.stepTimes, 0)
t.stepTotals = append(t.stepTotals, 0)
t.stepMinimums = append(t.stepMinimums, 0)
t.stepMaxes = append(t.stepMaxes, 0)
}
//fmt.Println(" #######", u.JsonP(t.stepNames))
}
cTotal := c.Total()
t.total += float64(cTotal)
if cTotal < t.totalMinimum || t.totalMinimum == 0 {
t.totalMinimum = cTotal
}
if cTotal > t.totalMax || t.totalMax == 0 {
t.totalMax = cTotal
}
t.times++
for i, tm := range c.stepTimes {
t.stepTotals[i] += float64(tm)
t.stepTimes[i]++
if tm < t.stepMinimums[i] || t.stepMinimums[i] == 0 {
t.stepMinimums[i] = tm
}
if tm > t.stepMaxes[i] || t.stepMaxes[i] == 0 {
t.stepMaxes[i] = tm
}
}
// 进行统计
n := Config.StatisticTimeInterval
if n == 0 {
n = 10000
}
if t.times >= n {
t.endTime = time.Now()
t.Log()
t.startTime = t.endTime
t.total = 0
t.totalMinimum = 0
t.totalMax = 0
t.times = 0
for i := range t.stepTimes {
t.stepTotals[i] = 0
t.stepTimes[i] = 0
t.stepMinimums[i] = 0
t.stepMaxes[i] = 0
}
}
t.lock.Unlock()
return out
}
func (t *TimeStatistician) Log() {
for i, name := range t.stepNames {
avg := t.stepTotals[i] / float64(t.stepTimes[i])
t.logger.Statistic(serverId, discover.Config.App, "s-request-"+name, t.startTime, t.endTime, uint(t.times), 0, avg, t.stepMinimums[i], t.stepMaxes[i])
}
t.logger.Statistic(serverId, discover.Config.App, "s-request-Total", t.startTime, t.endTime, uint(t.times), 0, t.total/float64(t.times), t.totalMinimum, t.totalMax)
}