Repository navigation
Expand file tree
/
Copy pathstats_test.go
More file actions
173 lines (151 loc) · 4.89 KB
/
Copy pathstats_test.go
File metadata and controls
173 lines (151 loc) · 4.89 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
164
165
166
167
168
169
170
171
172
173
package pacecache
import (
"errors"
"sync"
"testing"
"time"
)
func TestStatsSnapshotAggregatesSegments(t *testing.T) {
store := newStorageWithExpirationResolution[string, int](4, 2, time.Nanosecond)
collector := newStatsCollector(2)
cache := &Cache[string, int]{
store: store,
states: make([]cacheState[string, int], 2),
stats: collector,
ttl: time.Minute,
}
for index := range store.segments {
segment := &store.segments[index]
key := string(rune('a' + index))
segment.mu.Lock()
segment.entries[key] = &entry[string, int]{key: key}
counters := collector.segment(index)
counters.hitCount = int64(index + 1)
counters.missCount = int64(index + 3)
counters.evictionCount = int64(index + 4)
counters.expirationCount = int64(index + 5)
segment.mu.Unlock()
counters.loadFoundCount.Store(int64(index + 6))
counters.loadNotFoundCount.Store(int64(index + 7))
counters.loadErrorCount.Store(int64(index + 8))
counters.loadSupersededCount.Store(int64(index + 9))
counters.loadDurationNanos.Store(int64((index + 10) * 100))
counters.sharedCount.Store(int64(index + 11))
counters.deletedEntryCount.Store(int64(index + 12))
}
collector.clearedEntryCount.Store(13)
collector.cleanupCount.Store(14)
collector.cleanupWorkerRunCount.Store(15)
collector.cleanupWorkerPendingCount.Store(16)
collector.cleanupWorkerDurationNanos.Store(17)
want := Stats{
EntryCount: 2,
MaxEntries: 4,
SegmentCount: 2,
HitCount: 3,
MissCount: 7,
EvictionCount: 9,
ExpirationCount: 11,
LoadFoundCount: 13,
LoadNotFoundCount: 15,
LoadErrorCount: 17,
LoadSupersededCount: 19,
LoadDuration: 2_100 * time.Nanosecond,
SharedCount: 23,
DeletedEntryCount: 25,
ClearedEntryCount: 13,
CleanupCount: 14,
CleanupWorkerRunCount: 15,
CleanupWorkerPendingCount: 16,
CleanupWorkerDuration: 17 * time.Nanosecond,
}
if got := cache.Stats(); got != want {
t.Fatalf("Stats() = %+v, want %+v", got, want)
}
}
func TestStatsRecordLoad(t *testing.T) {
collector := newStatsCollector(1)
sentinel := errors.New("load failed")
collector.recordLoad(0, true, nil, 10*time.Nanosecond)
collector.recordLoad(0, false, nil, 20*time.Nanosecond)
collector.recordLoad(0, false, sentinel, 30*time.Nanosecond)
collector.recordLoadSuperseded(0)
counters := collector.segment(0)
if counters.loadFoundCount.Load() != 1 || counters.loadNotFoundCount.Load() != 1 || counters.loadErrorCount.Load() != 1 {
t.Fatalf(
"load counts = found:%d notFound:%d error:%d",
counters.loadFoundCount.Load(),
counters.loadNotFoundCount.Load(),
counters.loadErrorCount.Load(),
)
}
if counters.loadSupersededCount.Load() != 1 {
t.Fatalf("loadSupersededCount = %d, want 1", counters.loadSupersededCount.Load())
}
if counters.loadDurationNanos.Load() != 60 {
t.Fatalf("load duration nanos = %d, want 60", counters.loadDurationNanos.Load())
}
}
func TestStatsRecordHelpers(t *testing.T) {
collector := newStatsCollector(1)
collector.recordShared(0)
collector.recordDelete(0, 0)
collector.recordDelete(0, -1)
collector.recordDelete(0, 3)
collector.recordClear(0)
collector.recordClear(-1)
collector.recordClear(4)
collector.recordCleanup()
collector.recordCleanupWorker(false, 10*time.Nanosecond)
collector.recordCleanupWorker(true, 20*time.Nanosecond)
if collector.segment(0).sharedCount.Load() != 1 {
t.Fatalf("sharedCount = %d, want 1", collector.segment(0).sharedCount.Load())
}
if collector.segment(0).deletedEntryCount.Load() != 3 {
t.Fatalf("deletedEntryCount = %d, want 3", collector.segment(0).deletedEntryCount.Load())
}
if collector.clearedEntryCount.Load() != 4 {
t.Fatalf("clearedEntryCount = %d, want 4", collector.clearedEntryCount.Load())
}
if collector.cleanupCount.Load() != 1 {
t.Fatalf("cleanupCount = %d, want 1", collector.cleanupCount.Load())
}
if collector.cleanupWorkerRunCount.Load() != 2 || collector.cleanupWorkerPendingCount.Load() != 1 || collector.cleanupWorkerDurationNanos.Load() != 30 {
t.Fatalf(
"worker stats = runs:%d pending:%d duration:%d",
collector.cleanupWorkerRunCount.Load(),
collector.cleanupWorkerPendingCount.Load(),
collector.cleanupWorkerDurationNanos.Load(),
)
}
}
func TestStatsConcurrentWithCacheOperations(t *testing.T) {
cache := newTestCache[int](WithMaxEntries(128), WithSegmentCount(8))
var group sync.WaitGroup
for worker := range 8 {
group.Add(1)
go func(worker int) {
defer group.Done()
for iteration := range 1_000 {
key := string(rune('a' + (worker+iteration)%16))
cache.Set(key, iteration, NoExpiration)
cache.Get(key)
if iteration%11 == 0 {
cache.Delete(key)
}
}
}(worker)
}
group.Add(1)
go func() {
defer group.Done()
for range 1_000 {
_ = cache.Stats()
}
}()
waitTestGroup(t, &group)
stats := cache.Stats()
if stats.MaxEntries != 128 || stats.SegmentCount != 8 {
t.Fatalf("Stats configuration = %+v", stats)
}
}