-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistic.go
300 lines (252 loc) · 7.08 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package log
import (
"encoding/json"
"fmt"
"runtime/debug"
"strings"
"sync"
"time"
)
/*
统计: 每个文件的方法被调用的总次数、总调用时间、平均执行时间(微秒)
statistic: every function called total counts, executed total time, average execute time on micro seconds
*/
const (
EXPIRE_TIME_MICRO_SECONDS = 24 * 3600 * 1e6
)
var (
FUNCNAME_ALL = "all"
FUNCNAME_NIL = ""
enableStats = true
)
type statistic struct {
callers sync.Map
results sync.Map
mutex sync.RWMutex
}
type caller struct {
KeyName string `json:"key_name"`
FileName string `json:"file_name"`
LineNo int `json:"line_no"`
FuncName string `json:"func_name"`
EnterTime int64 `json:"enter_time"`
LeaveTime int64 `json:"leave_time"`
SpendTime int64 `json:"spend_time"`
ExpireTime int64 `json:"expire_time"`
CallOk bool `json:"call_ok"`
}
type result struct {
FileName string `json:"file_name"` //code file of function [代码文件名]
LineNo int `json:"line_no"` //line no of function [行号]
FuncName string `json:"func_name"` //function name [方法名称]
CallCount int64 `json:"call_count"` //call total times [调用次数]
ErrorCount int64 `json:"error_count"` //call error times [错误次数]
TotalTime int64 `json:"total_time"` //micro seconds [执行总时间]
TotalTimeStr string `json:"total_time_str"` //time string format [执行总时间-日期字符串格式]
AvgTime int64 `json:"avg_time"` //micro seconds [平均执行时间]
AvgTimeStr string `json:"avg_time_str"` //time string format [平均执行时间-日期字符串格式]
MaxTime int64 `json:"max_time"` //max time elapse once [单次最大执行时间]
MaxTimeStr string `json:"max_time_str"` //max time elapse once [单次最大执行时间-日期字符串格式]
CreateTime int64 `json:"create_time"` //unix timestamp on seconds [计时开始时间]
UpdateTime int64 `json:"update_time"` //unix timestamp on seconds [计时更新时间]
}
type summary struct {
TimeUnit string `json:"time_unit"`
Results []*result `json:"statistics"`
}
var stic *statistic //数据统计对象
func init() {
stic = newStatistic()
go checkExpire(stic)
}
//create a new statistic object
func newStatistic() *statistic {
return &statistic{}
}
func getUnixSecond() int64 {
return time.Now().Unix()
}
func getDatetime() string {
return time.Now().Format("2006-01-02 15:04:05")
}
func getMilliSec() int64 {
return time.Now().UnixNano() / 1e6 //milliseconds
}
func getMicroSec() int64 {
return time.Now().UnixNano() / 1e3 //microseconds
}
func getSpendTime(microseconds int64) (h, m, s int, ms float32) {
if microseconds > 0 {
nSpend := microseconds / 1e6
if nSpend > 0 {
h = int(nSpend / 3600)
m = int((nSpend % 3600) / 60)
s = int((nSpend % 3600) % 60)
rem := microseconds - (nSpend * 1e6)
if rem > 0 {
ms = float32(rem) / 1000
}
} else {
ms = float32(microseconds) / 1000
}
}
return
}
func getCallerStoreKey(strFile, strFunc string) string {
return fmt.Sprintf("%v %v %v", getRoutineId(), strFile, strFunc)
}
func makeCallerStoreKey(strRoutineId string, strFile, strFunc string) string {
return fmt.Sprintf("%v %v %v", strRoutineId, strFile, strFunc)
}
func getResultStoreKey(strFile, strFunc string) string {
return fmt.Sprintf("%v:%v", strFile, strFunc)
}
func getRoutineId() (strRoutine string) {
strStack := string(debug.Stack())
nIdx := strings.IndexAny(strStack, ":\r\n")
if nIdx > 0 {
strRoutine = strStack[:nIdx]
strings.TrimSpace(strRoutine)
nIdx = strings.Index(strRoutine, "[")
if nIdx > 0 {
strRoutine = strRoutine[:nIdx-1]
}
return
}
return "<unknown routine>"
}
//进入方法(enter function)
func (s *statistic) enter(strFile, strFunc string, nLineNo int) {
if !enableStats {
Warnf("log statistics is disabled")
return
}
now64 := getMicroSec()
c := caller{
FileName: strFile,
LineNo: nLineNo,
FuncName: strFunc,
EnterTime: now64,
LeaveTime: 0,
SpendTime: 0,
ExpireTime: now64 + EXPIRE_TIME_MICRO_SECONDS,
CallOk: true,
}
c.KeyName = getCallerStoreKey(strFile, strFunc)
s.callers.Store(c.KeyName, &c)
//Debug("caller store ok")
strResultKey := getResultStoreKey(strFile, strFunc)
var r *result
if _, ok := s.results.Load(strResultKey); !ok {
r = &result{
FileName: strFile,
LineNo: nLineNo,
FuncName: strFunc,
CallCount: 0,
ErrorCount: 0,
TotalTime: 0,
AvgTime: 0,
CreateTime: getUnixSecond(),
UpdateTime: getUnixSecond(),
}
s.results.Store(strResultKey, r)
}
return
}
//退出方法(leave function)
func (s *statistic) leave(strFile, strFunc string, nLineNo int) (int64, bool) {
if !enableStats {
return 0, true
}
now64 := getMicroSec()
strCallerKey := getCallerStoreKey(strFile, strFunc)
strResultKey := getResultStoreKey(strFile, strFunc)
if v, ok := s.callers.Load(strCallerKey); ok {
s.callers.Delete(strCallerKey)
c := v.(*caller)
s.mutex.Lock() //lock
c.LeaveTime = now64
c.SpendTime = c.LeaveTime - c.EnterTime
var r *result
if v2, ok2 := s.results.Load(strResultKey); ok2 {
r = v2.(*result)
r.CallCount++
if c.SpendTime > 0 {
r.TotalTime += c.SpendTime
r.AvgTime = r.TotalTime / r.CallCount
if c.SpendTime > r.MaxTime {
r.MaxTime = c.SpendTime //单次调用最大耗时
}
}
if !c.CallOk {
r.ErrorCount++
}
h, m, s, ms := getSpendTime(r.TotalTime)
r.TotalTimeStr = fmt.Sprintf("%vh %vm %vs %.3fms", h, m, s, ms)
h, m, s, ms = getSpendTime(r.AvgTime)
r.AvgTimeStr = fmt.Sprintf("%vh %vm %vs %.3fms", h, m, s, ms)
h, m, s, ms = getSpendTime(r.MaxTime)
r.MaxTimeStr = fmt.Sprintf("%vh %vm %vs %.3fms", h, m, s, ms)
r.UpdateTime = getUnixSecond()
}
s.mutex.Unlock() //unlock
//Json("result: ", r)
return c.SpendTime, ok
}
return 0, false
}
//统计error次数(incr error counts)
func (s *statistic) error(strFile, strFunc string, nLineNo int) {
if !enableStats {
return
}
strKey := getCallerStoreKey(strFile, strFunc)
if v, ok := s.callers.Load(strKey); ok {
c := v.(*caller)
c.CallOk = false
}
}
//统计信息汇总(statistic summary)
func (s *statistic) report(args ...interface{}) string {
var strFuncName string
if len(args) == 0 {
strFuncName = FUNCNAME_NIL
} else {
strFuncName = args[0].(string)
}
var summ = summary{
TimeUnit: "micro seconds",
}
s.mutex.RLock()
s.results.Range(
func(k, v interface{}) bool {
r := v.(*result)
if strFuncName == FUNCNAME_ALL || strFuncName == FUNCNAME_NIL || strings.Contains(r.FuncName, strFuncName) {
summ.Results = append(summ.Results, r)
}
return true
},
)
data, _ := json.MarshalIndent(summ, "", "\t")
s.mutex.RUnlock()
return string(data)
}
func checkExpire(stic *statistic) {
if !enableStats {
return
}
for {
stic.callers.Range(
func(k, v interface{}) bool {
now64 := getMicroSec()
c := v.(*caller)
if now64 > c.ExpireTime {
Warn("caller key [%v] expired at [%v]", k.(string), getDatetime())
stic.callers.Delete(k)
}
return true
},
)
time.Sleep(time.Hour)
}
}