Skip to content

Commit 63fbaaf

Browse files
authored
feat: support for latency command (#3584)
* support for latency command * add NonRedisEnterprise label for latency test
1 parent 60b748b commit 63fbaaf

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

command.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3794,6 +3794,83 @@ func (cmd *SlowLogCmd) readReply(rd *proto.Reader) error {
37943794

37953795
//-----------------------------------------------------------------------
37963796

3797+
type Latency struct {
3798+
Name string
3799+
Time time.Time
3800+
Latest time.Duration
3801+
Max time.Duration
3802+
}
3803+
3804+
type LatencyCmd struct {
3805+
baseCmd
3806+
val []Latency
3807+
}
3808+
3809+
var _ Cmder = (*LatencyCmd)(nil)
3810+
3811+
func NewLatencyCmd(ctx context.Context, args ...interface{}) *LatencyCmd {
3812+
return &LatencyCmd{
3813+
baseCmd: baseCmd{
3814+
ctx: ctx,
3815+
args: args,
3816+
},
3817+
}
3818+
}
3819+
3820+
func (cmd *LatencyCmd) SetVal(val []Latency) {
3821+
cmd.val = val
3822+
}
3823+
3824+
func (cmd *LatencyCmd) Val() []Latency {
3825+
return cmd.val
3826+
}
3827+
3828+
func (cmd *LatencyCmd) Result() ([]Latency, error) {
3829+
return cmd.val, cmd.err
3830+
}
3831+
3832+
func (cmd *LatencyCmd) String() string {
3833+
return cmdString(cmd, cmd.val)
3834+
}
3835+
3836+
func (cmd *LatencyCmd) readReply(rd *proto.Reader) error {
3837+
n, err := rd.ReadArrayLen()
3838+
if err != nil {
3839+
return err
3840+
}
3841+
cmd.val = make([]Latency, n)
3842+
for i := 0; i < len(cmd.val); i++ {
3843+
nn, err := rd.ReadArrayLen()
3844+
if err != nil {
3845+
return err
3846+
}
3847+
if nn < 3 {
3848+
return fmt.Errorf("redis: got %d elements in latency get, expected at least 3", nn)
3849+
}
3850+
if cmd.val[i].Name, err = rd.ReadString(); err != nil {
3851+
return err
3852+
}
3853+
createdAt, err := rd.ReadInt()
3854+
if err != nil {
3855+
return err
3856+
}
3857+
cmd.val[i].Time = time.Unix(createdAt, 0)
3858+
latest, err := rd.ReadInt()
3859+
if err != nil {
3860+
return err
3861+
}
3862+
cmd.val[i].Latest = time.Duration(latest) * time.Millisecond
3863+
maximum, err := rd.ReadInt()
3864+
if err != nil {
3865+
return err
3866+
}
3867+
cmd.val[i].Max = time.Duration(maximum) * time.Millisecond
3868+
}
3869+
return nil
3870+
}
3871+
3872+
//-----------------------------------------------------------------------
3873+
37973874
type MapStringInterfaceCmd struct {
37983875
baseCmd
37993876

commands.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ type Cmdable interface {
214214
Time(ctx context.Context) *TimeCmd
215215
DebugObject(ctx context.Context, key string) *StringCmd
216216
MemoryUsage(ctx context.Context, key string, samples ...int) *IntCmd
217+
Latency(ctx context.Context) *LatencyCmd
218+
LatencyReset(ctx context.Context, events ...interface{}) *StatusCmd
217219

218220
ModuleLoadex(ctx context.Context, conf *ModuleLoadexConfig) *StringCmd
219221

@@ -673,6 +675,22 @@ func (c cmdable) SlowLogGet(ctx context.Context, num int64) *SlowLogCmd {
673675
return cmd
674676
}
675677

678+
func (c cmdable) Latency(ctx context.Context) *LatencyCmd {
679+
cmd := NewLatencyCmd(ctx, "latency", "latest")
680+
_ = c(ctx, cmd)
681+
return cmd
682+
}
683+
684+
func (c cmdable) LatencyReset(ctx context.Context, events ...interface{}) *StatusCmd {
685+
args := make([]interface{}, 2+len(events))
686+
args[0] = "latency"
687+
args[1] = "reset"
688+
copy(args[2:], events)
689+
cmd := NewStatusCmd(ctx, args...)
690+
_ = c(ctx, cmd)
691+
return cmd
692+
}
693+
676694
func (c cmdable) Sync(_ context.Context) {
677695
panic("not implemented")
678696
}

commands_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8312,6 +8312,86 @@ var _ = Describe("Commands", func() {
83128312
Expect(len(result)).NotTo(BeZero())
83138313
})
83148314
})
8315+
8316+
Describe("Latency", Label("NonRedisEnterprise"), func() {
8317+
It("returns latencies", func() {
8318+
const key = "latency-monitor-threshold"
8319+
8320+
old := client.ConfigGet(ctx, key).Val()
8321+
client.ConfigSet(ctx, key, "1")
8322+
defer client.ConfigSet(ctx, key, old[key])
8323+
8324+
err := client.Do(ctx, "DEBUG", "SLEEP", 0.01).Err()
8325+
Expect(err).NotTo(HaveOccurred())
8326+
8327+
result, err := client.Latency(ctx).Result()
8328+
Expect(err).NotTo(HaveOccurred())
8329+
Expect(len(result)).NotTo(BeZero())
8330+
})
8331+
8332+
It("reset all latencies", func() {
8333+
const key = "latency-monitor-threshold"
8334+
8335+
result, err := client.Latency(ctx).Result()
8336+
// reset all latencies
8337+
err = client.LatencyReset(ctx).Err()
8338+
Expect(err).NotTo(HaveOccurred())
8339+
8340+
old := client.ConfigGet(ctx, key).Val()
8341+
client.ConfigSet(ctx, key, "1")
8342+
defer client.ConfigSet(ctx, key, old[key])
8343+
8344+
// get latency after reset
8345+
result, err = client.Latency(ctx).Result()
8346+
Expect(err).NotTo(HaveOccurred())
8347+
Expect(len(result)).Should(Equal(0))
8348+
8349+
// create a new latency
8350+
err = client.Do(ctx, "DEBUG", "SLEEP", 0.01).Err()
8351+
Expect(err).NotTo(HaveOccurred())
8352+
8353+
// get latency after create a new latency
8354+
result, err = client.Latency(ctx).Result()
8355+
Expect(err).NotTo(HaveOccurred())
8356+
Expect(len(result)).Should(Equal(1))
8357+
8358+
// reset all latencies again
8359+
err = client.LatencyReset(ctx).Err()
8360+
Expect(err).NotTo(HaveOccurred())
8361+
8362+
// get latency after reset again
8363+
result, err = client.Latency(ctx).Result()
8364+
Expect(err).NotTo(HaveOccurred())
8365+
Expect(len(result)).Should(Equal(0))
8366+
})
8367+
8368+
It("reset latencies by add event name args", func() {
8369+
const key = "latency-monitor-threshold"
8370+
8371+
old := client.ConfigGet(ctx, key).Val()
8372+
client.ConfigSet(ctx, key, "1")
8373+
defer client.ConfigSet(ctx, key, old[key])
8374+
8375+
result, err := client.Latency(ctx).Result()
8376+
Expect(err).NotTo(HaveOccurred())
8377+
Expect(len(result)).Should(Equal(0))
8378+
8379+
err = client.Do(ctx, "DEBUG", "SLEEP", 0.01).Err()
8380+
Expect(err).NotTo(HaveOccurred())
8381+
8382+
result, err = client.Latency(ctx).Result()
8383+
Expect(err).NotTo(HaveOccurred())
8384+
Expect(len(result)).Should(Equal(1))
8385+
8386+
// reset latency by event name
8387+
err = client.LatencyReset(ctx, result[0].Name).Err()
8388+
Expect(err).NotTo(HaveOccurred())
8389+
8390+
result, err = client.Latency(ctx).Result()
8391+
Expect(err).NotTo(HaveOccurred())
8392+
Expect(len(result)).Should(Equal(0))
8393+
})
8394+
})
83158395
})
83168396

83178397
type numberStruct struct {

0 commit comments

Comments
 (0)