]> git.saurik.com Git - redis.git/blob - tests/support/util.tcl
better random dataset creation function in test. master-slave replication test now...
[redis.git] / tests / support / util.tcl
1 proc randstring {min max {type binary}} {
2 set len [expr {$min+int(rand()*($max-$min+1))}]
3 set output {}
4 if {$type eq {binary}} {
5 set minval 0
6 set maxval 255
7 } elseif {$type eq {alpha}} {
8 set minval 48
9 set maxval 122
10 } elseif {$type eq {compr}} {
11 set minval 48
12 set maxval 52
13 }
14 while {$len} {
15 append output [format "%c" [expr {$minval+int(rand()*($maxval-$minval+1))}]]
16 incr len -1
17 }
18 return $output
19 }
20
21 # Useful for some test
22 proc zlistAlikeSort {a b} {
23 if {[lindex $a 0] > [lindex $b 0]} {return 1}
24 if {[lindex $a 0] < [lindex $b 0]} {return -1}
25 string compare [lindex $a 1] [lindex $b 1]
26 }
27
28 # Return all log lines starting with the first line that contains a warning.
29 # Generally, this will be an assertion error with a stack trace.
30 proc warnings_from_file {filename} {
31 set lines [split [exec cat $filename] "\n"]
32 set matched 0
33 set result {}
34 foreach line $lines {
35 if {[regexp {^\[\d+\]\s+\d+\s+\w+\s+\d{2}:\d{2}:\d{2} \#} $line]} {
36 set matched 1
37 }
38 if {$matched} {
39 lappend result $line
40 }
41 }
42 join $result "\n"
43 }
44
45 # Return value for INFO property
46 proc status {r property} {
47 if {[regexp "\r\n$property:(.*?)\r\n" [{*}$r info] _ value]} {
48 set _ $value
49 }
50 }
51
52 proc waitForBgsave r {
53 while 1 {
54 if {[status r bgsave_in_progress] eq 1} {
55 puts -nonewline "\nWaiting for background save to finish... "
56 flush stdout
57 after 1000
58 } else {
59 break
60 }
61 }
62 }
63
64 proc waitForBgrewriteaof r {
65 while 1 {
66 if {[status r bgrewriteaof_in_progress] eq 1} {
67 puts -nonewline "\nWaiting for background AOF rewrite to finish... "
68 flush stdout
69 after 1000
70 } else {
71 break
72 }
73 }
74 }
75
76 proc wait_for_sync r {
77 while 1 {
78 if {[status r master_link_status] eq "down"} {
79 after 10
80 } else {
81 break
82 }
83 }
84 }
85
86 proc randomInt {max} {
87 expr {int(rand()*$max)}
88 }
89
90 proc randpath args {
91 set path [expr {int(rand()*[llength $args])}]
92 uplevel 1 [lindex $args $path]
93 }
94
95 proc randomValue {} {
96 randpath {
97 # Small enough to likely collide
98 randomInt 1000
99 } {
100 # 32 bit compressible signed/unsigned
101 randpath {randomInt 2000000000} {randomInt 4000000000}
102 } {
103 # 64 bit
104 randpath {randomInt 1000000000000}
105 } {
106 # Random string
107 randpath {randstring 0 256 alpha} \
108 {randstring 0 256 compr} \
109 {randstring 0 256 binary}
110 }
111 }
112
113 proc randomKey {} {
114 randpath {
115 # Small enough to likely collide
116 randomInt 1000
117 } {
118 # 32 bit compressible signed/unsigned
119 randpath {randomInt 2000000000} {randomInt 4000000000}
120 } {
121 # 64 bit
122 randpath {randomInt 1000000000000}
123 } {
124 # Random string
125 randpath {randstring 1 256 alpha} \
126 {randstring 1 256 compr}
127 }
128 }
129
130 proc findKeyWithType {r type} {
131 for {set j 0} {$j < 20} {incr j} {
132 set k [{*}$r randomkey]
133 if {$k eq {}} {
134 return {}
135 }
136 if {[{*}$r type $k] eq $type} {
137 return $k
138 }
139 }
140 return {}
141 }
142
143 proc createComplexDataset {r ops} {
144 for {set j 0} {$j < $ops} {incr j} {
145 set k [randomKey]
146 set k2 [randomKey]
147 set f [randomValue]
148 set v [randomValue]
149 randpath {
150 set d [expr {rand()}]
151 } {
152 set d [expr {rand()}]
153 } {
154 set d [expr {rand()}]
155 } {
156 set d [expr {rand()}]
157 } {
158 set d [expr {rand()}]
159 } {
160 randpath {set d +inf} {set d -inf}
161 }
162 set t [{*}$r type $k]
163
164 if {$t eq {none}} {
165 randpath {
166 {*}$r set $k $v
167 } {
168 {*}$r lpush $k $v
169 } {
170 {*}$r sadd $k $v
171 } {
172 {*}$r zadd $k $d $v
173 } {
174 {*}$r hset $k $f $v
175 } {
176 {*}$r del $k
177 }
178 set t [{*}$r type $k]
179 }
180
181 switch $t {
182 {string} {
183 # Nothing to do
184 }
185 {list} {
186 randpath {{*}$r lpush $k $v} \
187 {{*}$r rpush $k $v} \
188 {{*}$r lrem $k 0 $v} \
189 {{*}$r rpop $k} \
190 {{*}$r lpop $k}
191 }
192 {set} {
193 randpath {{*}$r sadd $k $v} \
194 {{*}$r srem $k $v} \
195 {
196 set otherset [findKeyWithType r set]
197 if {$otherset ne {}} {
198 randpath {
199 {*}$r sunionstore $k2 $k $otherset
200 } {
201 {*}$r sinterstore $k2 $k $otherset
202 } {
203 {*}$r sdiffstore $k2 $k $otherset
204 }
205 }
206 }
207 }
208 {zset} {
209 randpath {{*}$r zadd $k $d $v} \
210 {{*}$r zrem $k $v} \
211 {
212 set otherzset [findKeyWithType r zset]
213 if {$otherzset ne {}} {
214 randpath {
215 {*}$r zunionstore $k2 2 $k $otherzset
216 } {
217 {*}$r zinterstore $k2 2 $k $otherzset
218 }
219 }
220 }
221 }
222 {hash} {
223 randpath {{*}$r hset $k $f $v} \
224 {{*}$r hdel $k $f}
225 }
226 }
227 }
228 }
229
230 proc formatCommand {args} {
231 set cmd "*[llength $args]\r\n"
232 foreach a $args {
233 append cmd "$[string length $a]\r\n$a\r\n"
234 }
235 set _ $cmd
236 }
237
238 proc csvdump r {
239 set o {}
240 foreach k [lsort [{*}$r keys *]] {
241 set type [{*}$r type $k]
242 append o [csvstring $k] , [csvstring $type] ,
243 switch $type {
244 string {
245 append o [csvstring [{*}$r get $k]] "\n"
246 }
247 list {
248 foreach e [{*}$r lrange $k 0 -1] {
249 append o [csvstring $e] ,
250 }
251 append o "\n"
252 }
253 set {
254 foreach e [lsort [{*}$r smembers $k]] {
255 append o [csvstring $e] ,
256 }
257 append o "\n"
258 }
259 zset {
260 foreach e [{*}$r zrange $k 0 -1 withscores] {
261 append o [csvstring $e] ,
262 }
263 append o "\n"
264 }
265 hash {
266 set fields [{*}$r hgetall $k]
267 set newfields {}
268 foreach {k v} $fields {
269 lappend newfields [list $k $v]
270 }
271 set fields [lsort -index 0 $newfields]
272 foreach kv $fields {
273 append o [csvstring [lindex $kv 0]] ,
274 append o [csvstring [lindex $kv 1]] ,
275 }
276 append o "\n"
277 }
278 }
279 }
280 return $o
281 }
282
283 proc csvstring s {
284 return "\"$s\""
285 }