aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/tests/unit/latency-monitor.tcl
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/tests/unit/latency-monitor.tcl')
-rw-r--r--examples/redis-unstable/tests/unit/latency-monitor.tcl189
1 files changed, 189 insertions, 0 deletions
diff --git a/examples/redis-unstable/tests/unit/latency-monitor.tcl b/examples/redis-unstable/tests/unit/latency-monitor.tcl
new file mode 100644
index 0000000..9e714c1
--- /dev/null
+++ b/examples/redis-unstable/tests/unit/latency-monitor.tcl
@@ -0,0 +1,189 @@
1start_server {tags {"latency-monitor needs:latency"}} {
2 # Set a threshold high enough to avoid spurious latency events.
3 r config set latency-monitor-threshold 200
4 r latency reset
5
6 test {LATENCY HISTOGRAM with empty histogram} {
7 r config resetstat
8 set histo [dict create {*}[r latency histogram]]
9 # Config resetstat is recorded
10 assert_equal [dict size $histo] 1
11 assert_match {*config|resetstat*} $histo
12 }
13
14 test {LATENCY HISTOGRAM all commands} {
15 r config resetstat
16 r set a b
17 r set c d
18 set histo [dict create {*}[r latency histogram]]
19 assert_match {calls 2 histogram_usec *} [dict get $histo set]
20 assert_match {calls 1 histogram_usec *} [dict get $histo "config|resetstat"]
21 }
22
23 test {LATENCY HISTOGRAM sub commands} {
24 r config resetstat
25 r client id
26 r client list
27 # parent command reply with its sub commands
28 set histo [dict create {*}[r latency histogram client]]
29 assert {[dict size $histo] == 2}
30 assert_match {calls 1 histogram_usec *} [dict get $histo "client|id"]
31 assert_match {calls 1 histogram_usec *} [dict get $histo "client|list"]
32
33 # explicitly ask for one sub-command
34 set histo [dict create {*}[r latency histogram "client|id"]]
35 assert {[dict size $histo] == 1}
36 assert_match {calls 1 histogram_usec *} [dict get $histo "client|id"]
37 }
38
39 test {LATENCY HISTOGRAM with a subset of commands} {
40 r config resetstat
41 r set a b
42 r set c d
43 r get a
44 r hset f k v
45 r hgetall f
46 set histo [dict create {*}[r latency histogram set hset]]
47 assert_match {calls 2 histogram_usec *} [dict get $histo set]
48 assert_match {calls 1 histogram_usec *} [dict get $histo hset]
49 assert_equal [dict size $histo] 2
50 set histo [dict create {*}[r latency histogram hgetall get zadd]]
51 assert_match {calls 1 histogram_usec *} [dict get $histo hgetall]
52 assert_match {calls 1 histogram_usec *} [dict get $histo get]
53 assert_equal [dict size $histo] 2
54 }
55
56 test {LATENCY HISTOGRAM command} {
57 r config resetstat
58 r set a b
59 r get a
60 assert {[llength [r latency histogram set get]] == 4}
61 }
62
63 test {LATENCY HISTOGRAM with wrong command name skips the invalid one} {
64 r config resetstat
65 assert {[llength [r latency histogram blabla]] == 0}
66 assert {[llength [r latency histogram blabla blabla2 set get]] == 0}
67 r set a b
68 r get a
69 assert_match {calls 1 histogram_usec *} [lindex [r latency histogram blabla blabla2 set get] 1]
70 assert_match {calls 1 histogram_usec *} [lindex [r latency histogram blabla blabla2 set get] 3]
71 assert {[string length [r latency histogram blabla set get]] > 0}
72 }
73
74tags {"needs:debug"} {
75 set old_threshold_value [lindex [r config get latency-monitor-threshold] 1]
76
77 test {Test latency events logging} {
78 r config set latency-monitor-threshold 200
79 r latency reset
80 r debug sleep 0.3
81 after 1100
82 r debug sleep 0.4
83 after 1100
84 r debug sleep 0.5
85 r config set latency-monitor-threshold 0
86 assert {[r latency history command] >= 3}
87 }
88
89 test {LATENCY HISTORY output is ok} {
90 set res [r latency history command]
91 if {$::verbose} {
92 puts "LATENCY HISTORY data:"
93 puts $res
94 }
95
96 set min 250
97 set max 450
98 foreach event $res {
99 lassign $event time latency
100 if {!$::no_latency} {
101 assert {$latency >= $min && $latency <= $max}
102 }
103 incr min 100
104 incr max 100
105 set last_time $time ; # Used in the next test
106 }
107 }
108
109 test {LATENCY LATEST output is ok} {
110 set res [r latency latest]
111 if {$::verbose} {
112 puts "LATENCY LATEST data:"
113 puts $res
114 }
115
116 foreach event $res {
117 lassign $event eventname time latency max
118 assert {$eventname eq "command"}
119 if {!$::no_latency} {
120 assert {$max >= 450 & $max <= 650}
121 assert {$time == $last_time}
122 }
123 break
124 }
125 }
126
127 test {LATENCY GRAPH can output the event graph} {
128 set res [r latency graph command]
129 if {$::verbose} {
130 puts "LATENCY GRAPH data:"
131 puts $res
132 }
133 assert_match {*command*high*low*} $res
134
135 # These numbers are taken from the "Test latency events logging" test.
136 # (debug sleep 0.3) and (debug sleep 0.5), using range to prevent timing issue.
137 regexp "command - high (.*?) ms, low (.*?) ms" $res -> high low
138 assert_morethan_equal $high 500
139 assert_morethan_equal $low 300
140 }
141
142 r config set latency-monitor-threshold $old_threshold_value
143} ;# tag
144
145 test {LATENCY of expire events are correctly collected} {
146 r config set latency-monitor-threshold 20
147 r flushdb
148 if {$::valgrind} {set count 100000} else {set count 1000000}
149 r eval {
150 local i = 0
151 while (i < tonumber(ARGV[1])) do
152 redis.call('sadd',KEYS[1],i)
153 i = i+1
154 end
155 } 1 mybigkey $count
156 r pexpire mybigkey 50
157 wait_for_condition 5 100 {
158 [r dbsize] == 0
159 } else {
160 fail "key wasn't expired"
161 }
162 assert_match {*expire-cycle*} [r latency latest]
163
164 test {LATENCY GRAPH can output the expire event graph} {
165 assert_match {*expire-cycle*high*low*} [r latency graph expire-cycle]
166 }
167
168 r config set latency-monitor-threshold 200
169 }
170
171 test {LATENCY HISTORY / RESET with wrong event name is fine} {
172 assert {[llength [r latency history blabla]] == 0}
173 assert {[r latency reset blabla] == 0}
174 }
175
176 test {LATENCY DOCTOR produces some output} {
177 assert {[string length [r latency doctor]] > 0}
178 }
179
180 test {LATENCY RESET is able to reset events} {
181 assert {[r latency reset] > 0}
182 assert {[r latency latest] eq {}}
183 }
184
185 test {LATENCY HELP should not have unexpected options} {
186 catch {r LATENCY help xxx} e
187 assert_match "*wrong number of arguments for 'latency|help' command" $e
188 }
189}