forked from lateefj/slowgrog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
57 lines (48 loc) · 1.15 KB
/
stats.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
package main
var (
// Commands that are documented to be blocking, performance issues or not to be used for production
BadCmdList []string
)
func init() {
BadCmdList = []string{
"KEYS", // KEYS is a blocking command that should NOT be used for production!!! (http://redis.io/commands/keys)
"SMEMBERS", // SMEMBERS is a blocking command that should not be used if possible instead use SCAN!! (http://redis.io/topics/latency)
}
}
type Stats struct {
cmdCounts map[string]int64
}
func NewStats() *Stats {
return &Stats{cmdCounts: make(map[string]int64)}
}
func (s *Stats) IncCmdCount(cmd string) {
v, exists := s.cmdCounts[cmd]
if !exists {
v = 0
}
v++
s.cmdCounts[cmd] = v
}
func contains(s string, l []string) bool {
for _, x := range l {
if s == x {
return true
}
}
return false
}
func matchCmds(cmdList []string, counts map[string]int64) map[string]int64 {
cmds := make(map[string]int64)
for k, v := range counts {
if contains(k, cmdList) {
cmds[k] = v
}
}
return cmds
}
func (s *Stats) Counts() map[string]int64 {
return s.cmdCounts
}
func (s *Stats) BadCmds() map[string]int64 {
return matchCmds(BadCmdList, s.cmdCounts)
}