Skip to content

Commit 53cc4b4

Browse files
authored
test: TestRingSetAddrsContention changed to Benchmark (#2316)
1 parent 603e972 commit 53cc4b4

File tree

3 files changed

+76
-88
lines changed

3 files changed

+76
-88
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
*.rdb
2-
testdata/*/
2+
testdata/*
33
.idea/

bench_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,78 @@ func BenchmarkClusterSetString(b *testing.B) {
365365
}
366366
})
367367
}
368+
369+
func BenchmarkExecRingSetAddrsCmd(b *testing.B) {
370+
const (
371+
ringShard1Name = "ringShardOne"
372+
ringShard2Name = "ringShardTwo"
373+
)
374+
375+
for _, port := range []string{ringShard1Port, ringShard2Port} {
376+
if _, err := startRedis(port); err != nil {
377+
b.Fatal(err)
378+
}
379+
}
380+
381+
b.Cleanup(func() {
382+
for _, p := range processes {
383+
if err := p.Close(); err != nil {
384+
b.Errorf("Failed to stop redis process: %v", err)
385+
}
386+
}
387+
processes = nil
388+
})
389+
390+
ring := redis.NewRing(&redis.RingOptions{
391+
Addrs: map[string]string{
392+
"ringShardOne": ":" + ringShard1Port,
393+
},
394+
NewClient: func(opt *redis.Options) *redis.Client {
395+
// Simulate slow shard creation
396+
time.Sleep(100 * time.Millisecond)
397+
return redis.NewClient(opt)
398+
},
399+
})
400+
defer ring.Close()
401+
402+
if _, err := ring.Ping(context.Background()).Result(); err != nil {
403+
b.Fatal(err)
404+
}
405+
406+
// Continuously update addresses by adding and removing one address
407+
updatesDone := make(chan struct{})
408+
defer func() { close(updatesDone) }()
409+
go func() {
410+
ticker := time.NewTicker(10 * time.Millisecond)
411+
defer ticker.Stop()
412+
for i := 0; ; i++ {
413+
select {
414+
case <-ticker.C:
415+
if i%2 == 0 {
416+
ring.SetAddrs(map[string]string{
417+
ringShard1Name: ":" + ringShard1Port,
418+
})
419+
} else {
420+
ring.SetAddrs(map[string]string{
421+
ringShard1Name: ":" + ringShard1Port,
422+
ringShard2Name: ":" + ringShard2Port,
423+
})
424+
}
425+
case <-updatesDone:
426+
return
427+
}
428+
}
429+
}()
430+
431+
b.ResetTimer()
432+
for i := 0; i < b.N; i++ {
433+
if _, err := ring.Ping(context.Background()).Result(); err != nil {
434+
if err == redis.ErrClosed {
435+
// The shard client could be closed while ping command is in progress
436+
continue
437+
} else {
438+
b.Fatal(err)
439+
}
440+
}
441+
}
442+
}

ring_test.go

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net"
88
"strconv"
99
"sync"
10-
"testing"
1110
"time"
1211

1312
. "github.com/onsi/ginkgo"
@@ -740,89 +739,3 @@ var _ = Describe("Ring Tx timeout", func() {
740739
testTimeout()
741740
})
742741
})
743-
744-
func TestRingSetAddrsContention(t *testing.T) {
745-
const (
746-
ringShard1Name = "ringShardOne"
747-
ringShard2Name = "ringShardTwo"
748-
)
749-
750-
for _, port := range []string{ringShard1Port, ringShard2Port} {
751-
if _, err := startRedis(port); err != nil {
752-
t.Fatal(err)
753-
}
754-
}
755-
756-
t.Cleanup(func() {
757-
for _, p := range processes {
758-
if err := p.Close(); err != nil {
759-
t.Errorf("Failed to stop redis process: %v", err)
760-
}
761-
}
762-
processes = nil
763-
})
764-
765-
ring := redis.NewRing(&redis.RingOptions{
766-
Addrs: map[string]string{
767-
"ringShardOne": ":" + ringShard1Port,
768-
},
769-
NewClient: func(opt *redis.Options) *redis.Client {
770-
// Simulate slow shard creation
771-
time.Sleep(100 * time.Millisecond)
772-
return redis.NewClient(opt)
773-
},
774-
})
775-
defer ring.Close()
776-
777-
if _, err := ring.Ping(context.Background()).Result(); err != nil {
778-
t.Fatal(err)
779-
}
780-
781-
// Continuously update addresses by adding and removing one address
782-
updatesDone := make(chan struct{})
783-
defer func() { close(updatesDone) }()
784-
go func() {
785-
ticker := time.NewTicker(10 * time.Millisecond)
786-
defer ticker.Stop()
787-
for i := 0; ; i++ {
788-
select {
789-
case <-ticker.C:
790-
if i%2 == 0 {
791-
ring.SetAddrs(map[string]string{
792-
ringShard1Name: ":" + ringShard1Port,
793-
})
794-
} else {
795-
ring.SetAddrs(map[string]string{
796-
ringShard1Name: ":" + ringShard1Port,
797-
ringShard2Name: ":" + ringShard2Port,
798-
})
799-
}
800-
case <-updatesDone:
801-
return
802-
}
803-
}
804-
}()
805-
806-
var pings, errClosed int
807-
timer := time.NewTimer(1 * time.Second)
808-
for running := true; running; pings++ {
809-
select {
810-
case <-timer.C:
811-
running = false
812-
default:
813-
if _, err := ring.Ping(context.Background()).Result(); err != nil {
814-
if err == redis.ErrClosed {
815-
// The shard client could be closed while ping command is in progress
816-
errClosed++
817-
} else {
818-
t.Fatal(err)
819-
}
820-
}
821-
}
822-
}
823-
824-
t.Logf("Number of pings: %d, errClosed: %d", pings, errClosed)
825-
if pings < 10_000 {
826-
t.Errorf("Expected at least 10k pings, got: %d", pings)
827-
}
828-
}

0 commit comments

Comments
 (0)