-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathhotkeys_commands_test.go
More file actions
214 lines (176 loc) · 6.58 KB
/
hotkeys_commands_test.go
File metadata and controls
214 lines (176 loc) · 6.58 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
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
package redis_test
import (
"context"
"time"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
"github.com/redis/go-redis/v9"
)
var _ = Describe("HotKeys Commands", func() {
ctx := context.TODO()
var client *redis.Client
BeforeEach(func() {
client = redis.NewClient(redisOptions())
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
Describe("HOTKEYS", func() {
It("should start, get, stop, and reset hotkeys tracking", func() {
SkipBeforeRedisVersion(8.6, "HOTKEYS commands require Redis >= 8.6")
startArgs := &redis.HotKeysStartArgs{
Metrics: []redis.HotKeysMetric{redis.HotKeysMetricCPU, redis.HotKeysMetricNET},
Count: 10,
Duration: 0,
Sample: 1,
}
start := client.HotKeysStart(ctx, startArgs)
Expect(start.Err()).NotTo(HaveOccurred())
Expect(start.Val()).To(Equal("OK"))
for i := 0; i < 100; i++ {
client.Set(ctx, "hotkey1", "value1", 0)
client.Get(ctx, "hotkey1")
}
for i := 0; i < 50; i++ {
client.Set(ctx, "hotkey2", "value2", 0)
client.Get(ctx, "hotkey2")
}
for i := 0; i < 25; i++ {
client.Set(ctx, "hotkey3", "value3", 0)
client.Get(ctx, "hotkey3")
}
time.Sleep(100 * time.Millisecond)
get := client.HotKeysGet(ctx)
Expect(get.Err()).NotTo(HaveOccurred())
result := get.Val()
Expect(result).NotTo(BeNil())
Expect(result.TrackingActive).To(BeTrue())
Expect(result.SampleRatio).To(Equal(uint8(1)))
// Verify that collection start time is set (not zero)
Expect(result.CollectionStartTime.IsZero()).To(BeFalse())
// Verify that we have some CPU time data after running commands
Expect(result.AllCommandsAllSlots).To(BeNumerically(">", 0))
// Verify that we have hot keys data (we ran 350 commands on 3 keys)
Expect(len(result.ByCPUTime)).To(BeNumerically(">", 0))
Expect(len(result.ByNetBytes)).To(BeNumerically(">", 0))
// Verify that the first hot key entry has a non-empty key
if len(result.ByCPUTime) > 0 {
Expect(result.ByCPUTime[0].Key).NotTo(BeEmpty())
Expect(result.ByCPUTime[0].Value).NotTo(BeNil())
}
stop := client.HotKeysStop(ctx)
Expect(stop.Err()).NotTo(HaveOccurred())
Expect(stop.Val()).To(Equal("OK"))
get2 := client.HotKeysGet(ctx)
Expect(get2.Err()).NotTo(HaveOccurred())
result2 := get2.Val()
Expect(result2).NotTo(BeNil())
Expect(result2.TrackingActive).To(BeFalse())
// After stopping, collection duration should be set
Expect(result2.CollectionDuration).To(BeNumerically(">", 0))
reset := client.HotKeysReset(ctx)
Expect(reset.Err()).NotTo(HaveOccurred())
Expect(reset.Val()).To(Equal("OK"))
})
It("should start hotkeys tracking with CPU metric only", func() {
SkipBeforeRedisVersion(8.6, "HOTKEYS commands require Redis >= 8.6")
startArgs := &redis.HotKeysStartArgs{
Metrics: []redis.HotKeysMetric{redis.HotKeysMetricCPU},
Count: 5,
}
start := client.HotKeysStart(ctx, startArgs)
Expect(start.Err()).NotTo(HaveOccurred())
Expect(start.Val()).To(Equal("OK"))
stop := client.HotKeysStop(ctx)
Expect(stop.Err()).NotTo(HaveOccurred())
})
It("should start hotkeys tracking with NET metric only", func() {
SkipBeforeRedisVersion(8.6, "HOTKEYS commands require Redis >= 8.6")
startArgs := &redis.HotKeysStartArgs{
Metrics: []redis.HotKeysMetric{redis.HotKeysMetricNET},
Count: 5,
}
start := client.HotKeysStart(ctx, startArgs)
Expect(start.Err()).NotTo(HaveOccurred())
Expect(start.Val()).To(Equal("OK"))
stop := client.HotKeysStop(ctx)
Expect(stop.Err()).NotTo(HaveOccurred())
})
It("should start hotkeys tracking with duration", func() {
SkipBeforeRedisVersion(8.6, "HOTKEYS commands require Redis >= 8.6")
startArgs := &redis.HotKeysStartArgs{
Metrics: []redis.HotKeysMetric{redis.HotKeysMetricCPU, redis.HotKeysMetricNET},
Count: 10,
Duration: 2,
}
start := client.HotKeysStart(ctx, startArgs)
Expect(start.Err()).NotTo(HaveOccurred())
Expect(start.Val()).To(Equal("OK"))
time.Sleep(3 * time.Second)
get := client.HotKeysGet(ctx)
Expect(get.Err()).NotTo(HaveOccurred())
result := get.Val()
Expect(result).NotTo(BeNil())
Expect(result.TrackingActive).To(BeFalse())
})
It("should start hotkeys tracking with sampling", func() {
SkipBeforeRedisVersion(8.6, "HOTKEYS commands require Redis >= 8.6")
startArgs := &redis.HotKeysStartArgs{
Metrics: []redis.HotKeysMetric{redis.HotKeysMetricCPU, redis.HotKeysMetricNET},
Count: 10,
Sample: 10,
}
start := client.HotKeysStart(ctx, startArgs)
Expect(start.Err()).NotTo(HaveOccurred())
Expect(start.Val()).To(Equal("OK"))
stop := client.HotKeysStop(ctx)
Expect(stop.Err()).NotTo(HaveOccurred())
})
It("should error when using slots in non-cluster mode", func() {
SkipBeforeRedisVersion(8.6, "HOTKEYS commands require Redis >= 8.6")
startArgs := &redis.HotKeysStartArgs{
Metrics: []redis.HotKeysMetric{redis.HotKeysMetricCPU, redis.HotKeysMetricNET},
Count: 10,
Slots: []uint16{0, 1, 2, 100, 200},
}
start := client.HotKeysStart(ctx, startArgs)
Expect(start.Err()).To(HaveOccurred())
Expect(start.Err().Error()).To(ContainSubstring("SLOTS parameter cannot be used in non-cluster mode"))
})
It("should error when no metrics are specified", func() {
startArgs := &redis.HotKeysStartArgs{
Count: 10,
}
start := client.HotKeysStart(ctx, startArgs)
Expect(start.Err()).To(HaveOccurred())
Expect(start.Err()).To(Equal(redis.ErrHotKeysNoMetrics))
})
It("should error when starting tracking while already active", func() {
SkipBeforeRedisVersion(8.6, "HOTKEYS commands require Redis >= 8.6")
startArgs := &redis.HotKeysStartArgs{
Metrics: []redis.HotKeysMetric{redis.HotKeysMetricCPU},
Count: 10,
}
start1 := client.HotKeysStart(ctx, startArgs)
Expect(start1.Err()).NotTo(HaveOccurred())
start2 := client.HotKeysStart(ctx, startArgs)
Expect(start2.Err()).To(HaveOccurred())
stop := client.HotKeysStop(ctx)
Expect(stop.Err()).NotTo(HaveOccurred())
})
It("should error when resetting while tracking is active", func() {
SkipBeforeRedisVersion(8.6, "HOTKEYS commands require Redis >= 8.6")
startArgs := &redis.HotKeysStartArgs{
Metrics: []redis.HotKeysMetric{redis.HotKeysMetricCPU},
Count: 10,
}
start := client.HotKeysStart(ctx, startArgs)
Expect(start.Err()).NotTo(HaveOccurred())
reset := client.HotKeysReset(ctx)
Expect(reset.Err()).To(HaveOccurred())
stop := client.HotKeysStop(ctx)
Expect(stop.Err()).NotTo(HaveOccurred())
})
})
})