forked from buraksezer/olric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_test.go
279 lines (236 loc) · 8.29 KB
/
stats_test.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2018-2024 Burak Sezer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package olric
import (
"context"
"fmt"
"testing"
"time"
"github.com/buraksezer/olric/internal/dmap"
"github.com/buraksezer/olric/internal/protocol"
"github.com/buraksezer/olric/internal/pubsub"
"github.com/buraksezer/olric/internal/testutil"
"github.com/buraksezer/olric/stats"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
)
func resetPubSubStats() {
pubsub.SubscribersTotal.Reset()
pubsub.CurrentPSubscribers.Reset()
pubsub.CurrentSubscribers.Reset()
pubsub.PSubscribersTotal.Reset()
pubsub.PublishedTotal.Reset()
}
func TestOlric_Stats(t *testing.T) {
cluster := newTestOlricCluster(t)
db := cluster.addMember(t)
c := db.NewEmbeddedClient()
dm, err := c.NewDMap("mymap")
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for i := 0; i < 100; i++ {
err = dm.Put(ctx, testutil.ToKey(i), testutil.ToVal(i))
require.NoError(t, err)
}
s, err := c.Stats(ctx, db.rt.This().String())
require.NoError(t, err)
if s.ClusterCoordinator.ID != db.rt.This().ID {
t.Fatalf("Expected cluster coordinator: %v. Got: %v", db.rt.This(), s.ClusterCoordinator)
}
require.Equal(t, s.Member.Name, db.rt.This().Name)
require.Equal(t, s.Member.ID, db.rt.This().ID)
require.Equal(t, s.Member.Birthdate, db.rt.This().Birthdate)
if s.Runtime != nil {
t.Error("Runtime stats must not be collected by default:", s.Runtime)
}
var total int
for partID, part := range s.Partitions {
total += part.Length
if _, ok := part.DMaps["mymap"]; !ok {
t.Fatalf("Expected dmap check result is true. Got false")
}
if len(part.PreviousOwners) != 0 {
t.Fatalf("Expected PreviosOwners list is empty. "+
"Got: %v for PartID: %d", part.PreviousOwners, partID)
}
if part.Length <= 0 {
t.Fatalf("Unexpected Length: %d", part.Length)
}
}
if total != 100 {
t.Fatalf("Expected total length of partition in stats is 100. Got: %d", total)
}
_, ok := s.ClusterMembers[stats.MemberID(db.rt.This().ID)]
if !ok {
t.Fatalf("Expected member ID: %d could not be found in ClusterMembers", db.rt.This().ID)
}
}
func TestOlric_Stats_CollectRuntime(t *testing.T) {
cluster := newTestOlricCluster(t)
db := cluster.addMember(t)
e := db.NewEmbeddedClient()
s, err := e.Stats(context.Background(), db.rt.This().String(), CollectRuntime())
require.NoError(t, err)
if s.Runtime == nil {
t.Fatal("Runtime stats must be collected by default:", s.Runtime)
}
}
func TestOlric_Stats_Cluster(t *testing.T) {
cluster := newTestOlricCluster(t)
db := cluster.addMember(t)
db2 := cluster.addMember(t)
e := db.NewEmbeddedClient()
s, err := e.Stats(context.Background(), db2.rt.This().String())
require.NoError(t, err)
require.Nil(t, s.Runtime)
require.Equal(t, s.Member.String(), db2.rt.This().String())
}
func TestStats_PubSub(t *testing.T) {
resetPubSubStats()
cluster := newTestOlricCluster(t)
db := cluster.addMember(t)
rc := redis.NewClient(&redis.Options{Addr: db.rt.This().String()})
ctx := context.Background()
t.Run("Subscribe", func(t *testing.T) {
defer func() {
resetPubSubStats()
}()
var subscribers []*redis.PubSub
for i := 0; i < 5; i++ {
ps := rc.Subscribe(ctx, "my-channel")
// Wait for confirmation that subscription is created before publishing anything.
_, err := ps.Receive(ctx)
require.NoError(t, err)
subscribers = append(subscribers, ps)
}
for i := 0; i < 10; i++ {
cmd := rc.Publish(ctx, "my-channel", fmt.Sprintf("message-%d", i))
res, err := cmd.Result()
require.Equal(t, int64(5), res)
require.NoError(t, err)
}
require.Equal(t, int64(50), pubsub.PublishedTotal.Read())
require.Equal(t, int64(5), pubsub.SubscribersTotal.Read())
require.Equal(t, int64(5), pubsub.CurrentSubscribers.Read())
require.Equal(t, int64(0), pubsub.PSubscribersTotal.Read())
require.Equal(t, int64(0), pubsub.CurrentPSubscribers.Read())
// Unsubscribe
for _, s := range subscribers {
err := s.Unsubscribe(ctx, "my-channel")
require.NoError(t, err)
<-time.After(100 * time.Millisecond)
}
require.Equal(t, int64(5), pubsub.SubscribersTotal.Read())
require.Equal(t, int64(0), pubsub.CurrentSubscribers.Read())
require.Equal(t, int64(0), pubsub.PSubscribersTotal.Read())
require.Equal(t, int64(0), pubsub.CurrentPSubscribers.Read())
})
t.Run("PSubscribe", func(t *testing.T) {
defer func() {
resetPubSubStats()
}()
ps := rc.PSubscribe(ctx, "h?llo")
// Wait for confirmation that subscription is created before publishing anything.
_, err := ps.Receive(ctx)
require.NoError(t, err)
cmd := rc.Publish(ctx, "hxllo", "message")
res, err := cmd.Result()
require.Equal(t, int64(1), res)
require.NoError(t, err)
require.Equal(t, int64(1), pubsub.PublishedTotal.Read())
require.Equal(t, int64(1), pubsub.PSubscribersTotal.Read())
require.Equal(t, int64(1), pubsub.CurrentPSubscribers.Read())
require.Equal(t, int64(0), pubsub.SubscribersTotal.Read())
require.Equal(t, int64(0), pubsub.CurrentSubscribers.Read())
err = ps.PUnsubscribe(ctx, "h?llo")
require.NoError(t, err)
<-time.After(100 * time.Millisecond)
require.Equal(t, int64(1), pubsub.PSubscribersTotal.Read())
require.Equal(t, int64(0), pubsub.CurrentPSubscribers.Read())
require.Equal(t, int64(0), pubsub.SubscribersTotal.Read())
require.Equal(t, int64(0), pubsub.CurrentSubscribers.Read())
})
}
func TestStats_DMap(t *testing.T) {
cluster := newTestOlricCluster(t)
db := cluster.addMember(t)
rc := redis.NewClient(&redis.Options{Addr: db.rt.This().String()})
ctx := context.Background()
t.Run("DMap stats without eviction", func(t *testing.T) {
// EntriesTotal
for i := 0; i < 10; i++ {
cmd := protocol.NewPut("mydmap", fmt.Sprintf("mykey-%d", i), []byte("myvalue")).Command(ctx)
err := rc.Process(ctx, cmd)
require.NoError(t, err)
require.NoError(t, cmd.Err())
}
// GetHits
for i := 0; i < 10; i++ {
cmd := protocol.NewGet("mydmap", fmt.Sprintf("mykey-%d", i)).Command(ctx)
err := rc.Process(ctx, cmd)
require.NoError(t, err)
require.NoError(t, cmd.Err())
}
// DeleteHits
for i := 0; i < 10; i++ {
cmd := protocol.NewDel("mydmap", fmt.Sprintf("mykey-%d", i)).Command(ctx)
err := rc.Process(ctx, cmd)
require.NoError(t, err)
require.NoError(t, cmd.Err())
}
// GetMisses
for i := 0; i < 10; i++ {
cmd := protocol.NewGet("mydmap", fmt.Sprintf("mykey-%d", i)).Command(ctx)
err := rc.Process(ctx, cmd)
err = protocol.ConvertError(err)
require.ErrorIs(t, err, dmap.ErrKeyNotFound)
}
// DeleteMisses
for i := 0; i < 10; i++ {
cmd := protocol.NewDel("mydmap", fmt.Sprintf("mykey-%d", i)).Command(ctx)
err := rc.Process(ctx, cmd)
require.NoError(t, err)
require.NoError(t, cmd.Err())
}
require.GreaterOrEqual(t, dmap.EntriesTotal.Read(), int64(10))
require.GreaterOrEqual(t, dmap.GetMisses.Read(), int64(10))
require.GreaterOrEqual(t, dmap.GetHits.Read(), int64(10))
require.GreaterOrEqual(t, dmap.DeleteHits.Read(), int64(10))
require.GreaterOrEqual(t, dmap.DeleteMisses.Read(), int64(10))
})
t.Run("DMap eviction stats", func(t *testing.T) {
// EntriesTotal, EvictedTotal
for i := 0; i < 10; i++ {
cmd := protocol.
NewPut("mydmap", fmt.Sprintf("mykey-%d", i), []byte("myvalue")).
SetPX(time.Millisecond.Milliseconds()).
Command(ctx)
err := rc.Process(ctx, cmd)
require.NoError(t, err)
require.NoError(t, cmd.Err())
}
<-time.After(100 * time.Millisecond)
// GetMisses
for i := 0; i < 10; i++ {
cmd := protocol.NewGet("mydmap", "mykey").Command(ctx)
err := rc.Process(ctx, cmd)
err = protocol.ConvertError(err)
require.ErrorIs(t, err, dmap.ErrKeyNotFound)
}
require.Greater(t, dmap.DeleteHits.Read(), int64(0))
require.Greater(t, dmap.EvictedTotal.Read(), int64(0))
require.GreaterOrEqual(t, dmap.EntriesTotal.Read(), int64(10))
})
}