-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmetrics.go
More file actions
163 lines (145 loc) · 3.63 KB
/
metrics.go
File metadata and controls
163 lines (145 loc) · 3.63 KB
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
package mcache
import "sync/atomic"
// Metrics holds cache statistics.
type Metrics struct {
hits atomic.Int64 // Cache hits
misses atomic.Int64 // Cache misses
sets atomic.Int64 // Successful sets
deletes atomic.Int64 // Successful deletes
evictions atomic.Int64 // Evictions due to size/cost limit
expirations atomic.Int64 // Expirations due to TTL
rejections atomic.Int64 // Rejections by TinyLFU admission policy
costAdded atomic.Int64 // Total cost added
costEvicted atomic.Int64 // Total cost evicted
bufferDrops atomic.Int64 // Buffer saturation drops (sync fallback used)
}
// MetricsSnapshot is a point-in-time snapshot of cache metrics.
type MetricsSnapshot struct {
Hits int64 // Total cache hits
Misses int64 // Total cache misses
Sets int64 // Total successful sets
Deletes int64 // Total successful deletes
Evictions int64 // Total evictions due to size/cost limit
Expirations int64 // Total expirations due to TTL
Rejections int64 // Total rejections by admission policy
CostAdded int64 // Total cost added over time
CostEvicted int64 // Total cost evicted over time
BufferDrops int64 // Times buffer was full and sync fallback was used
HitRatio float64 // Hit ratio (hits / (hits + misses))
}
// newMetrics creates a new Metrics instance.
func newMetrics() *Metrics {
return &Metrics{}
}
// incHit increments the hit counter.
func (m *Metrics) incHit() {
if m == nil {
return
}
m.hits.Add(1)
}
// incMiss increments the miss counter.
func (m *Metrics) incMiss() {
if m == nil {
return
}
m.misses.Add(1)
}
// incSet increments the set counter.
func (m *Metrics) incSet() {
if m == nil {
return
}
m.sets.Add(1)
}
// incDelete increments the delete counter.
func (m *Metrics) incDelete() {
if m == nil {
return
}
m.deletes.Add(1)
}
// incEviction increments the eviction counter.
func (m *Metrics) incEviction() {
if m == nil {
return
}
m.evictions.Add(1)
}
// incExpiration increments the expiration counter.
func (m *Metrics) incExpiration() {
if m == nil {
return
}
m.expirations.Add(1)
}
// incRejection increments the rejection counter.
func (m *Metrics) incRejection() {
if m == nil {
return
}
m.rejections.Add(1)
}
// addCost adds to the cost added counter.
func (m *Metrics) addCost(cost int64) {
if m == nil {
return
}
m.costAdded.Add(cost)
}
// addEvictedCost adds to the cost evicted counter.
func (m *Metrics) addEvictedCost(cost int64) {
if m == nil {
return
}
m.costEvicted.Add(cost)
}
// incBufferDrop increments the buffer drop counter.
func (m *Metrics) incBufferDrop() {
if m == nil {
return
}
m.bufferDrops.Add(1)
}
// Snapshot returns a point-in-time snapshot of the metrics.
func (m *Metrics) Snapshot() MetricsSnapshot {
if m == nil {
return MetricsSnapshot{}
}
hits := m.hits.Load()
misses := m.misses.Load()
total := hits + misses
var hitRatio float64
if total > 0 {
hitRatio = float64(hits) / float64(total)
}
return MetricsSnapshot{
Hits: hits,
Misses: misses,
Sets: m.sets.Load(),
Deletes: m.deletes.Load(),
Evictions: m.evictions.Load(),
Expirations: m.expirations.Load(),
Rejections: m.rejections.Load(),
CostAdded: m.costAdded.Load(),
CostEvicted: m.costEvicted.Load(),
BufferDrops: m.bufferDrops.Load(),
HitRatio: hitRatio,
}
}
// Reset resets all metrics to zero.
func (m *Metrics) Reset() {
if m == nil {
return
}
m.hits.Store(0)
m.misses.Store(0)
m.sets.Store(0)
m.deletes.Store(0)
m.evictions.Store(0)
m.expirations.Store(0)
m.rejections.Store(0)
m.costAdded.Store(0)
m.costEvicted.Store(0)
m.bufferDrops.Store(0)
}