-
Notifications
You must be signed in to change notification settings - Fork 29
/
stats.go
51 lines (44 loc) · 1.21 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
package stats
import (
"sync"
"sync/atomic"
"unsafe"
"github.com/orca-zhang/ecache"
)
var m sync.Map
type StatsNode struct {
// don't reorder them or add field between them
Evicted, Updated, Added, GetMiss, GetHit, DelMiss, DelHit uint64
}
// HitRate
func (s *StatsNode) HitRate() float64 {
if s.GetHit == 0 && s.GetMiss == 0 {
return 0.0
}
return float64(s.GetHit) / float64(s.GetHit+s.GetMiss)
}
// Bind - to stats a cache
// `pool` can be used to classify instances that store same items
// `caches` is cache instances to be binded
func Bind(pool string, caches ...*ecache.Cache) error {
v, _ := m.LoadOrStore(pool, &StatsNode{})
for _, c := range caches {
c.Inspect(func(action int, _ string, _ *interface{}, _ []byte, status int) {
// very, very, very low-cost for stats
atomic.AddUint64((*uint64)(unsafe.Pointer(uintptr(unsafe.Pointer(v.(*StatsNode)))+uintptr(status+action*2-1)*unsafe.Sizeof(&status))), 1)
})
}
return nil
}
// Stats - get the result like follows
//
// `k` is categoy, type is string
// `v` is node, type is `*stats.StatsNode`
//
// stats.Stats().Range(func(k, v interface{}) bool {
// fmt.Println("stats:", k, v)
// return true
// })
func Stats() *sync.Map {
return &m
}