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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
proc randstring {min max {type binary}} {
set len [expr {$min+int(rand()*($max-$min+1))}]
set output {}
if {$type eq {binary}} {
set minval 0
set maxval 255
} elseif {$type eq {alpha}} {
set minval 48
set maxval 122
} elseif {$type eq {compr}} {
set minval 48
set maxval 52
}
while {$len} {
append output [format "%c" [expr {$minval+int(rand()*($maxval-$minval+1))}]]
incr len -1
}
return $output
}
# Useful for some test
proc zlistAlikeSort {a b} {
if {[lindex $a 0] > [lindex $b 0]} {return 1}
if {[lindex $a 0] < [lindex $b 0]} {return -1}
string compare [lindex $a 1] [lindex $b 1]
}
# Return all log lines starting with the first line that contains a warning.
# Generally, this will be an assertion error with a stack trace.
proc warnings_from_file {filename} {
set lines [split [exec cat $filename] "\n"]
set matched 0
set logall 0
set result {}
foreach line $lines {
if {[string match {*REDIS BUG REPORT START*} $line]} {
set logall 1
}
if {[regexp {^\[\d+\]\s+\d+\s+\w+\s+\d{2}:\d{2}:\d{2} \#} $line]} {
set matched 1
}
if {$logall || $matched} {
lappend result $line
}
}
join $result "\n"
}
# Return value for INFO property
proc status {r property} {
if {[regexp "\r\n$property:(.*?)\r\n" [{*}$r info] _ value]} {
set _ $value
}
}
proc waitForBgsave r {
while 1 {
if {[status r rdb_bgsave_in_progress] eq 1} {
if {$::verbose} {
puts -nonewline "\nWaiting for background save to finish... "
flush stdout
}
after 1000
} else {
break
}
}
}
proc waitForBgrewriteaof r {
while 1 {
if {[status r aof_rewrite_in_progress] eq 1} {
if {$::verbose} {
puts -nonewline "\nWaiting for background AOF rewrite to finish... "
flush stdout
}
after 1000
} else {
break
}
}
}
proc wait_for_sync r {
while 1 {
if {[status $r master_link_status] eq "down"} {
after 10
} else {
break
}
}
}
# Random integer between 0 and max (excluded).
proc randomInt {max} {
expr {int(rand()*$max)}
}
# Random signed integer between -max and max (both extremes excluded).
proc randomSignedInt {max} {
set i [randomInt $max]
if {rand() > 0.5} {
set i -$i
}
return $i
}
proc randpath args {
set path [expr {int(rand()*[llength $args])}]
uplevel 1 [lindex $args $path]
}
proc randomValue {} {
randpath {
# Small enough to likely collide
randomSignedInt 1000
} {
# 32 bit compressible signed/unsigned
randpath {randomSignedInt 2000000000} {randomSignedInt 4000000000}
} {
# 64 bit
randpath {randomSignedInt 1000000000000}
} {
# Random string
randpath {randstring 0 256 alpha} \
{randstring 0 256 compr} \
{randstring 0 256 binary}
}
}
proc randomKey {} {
randpath {
# Small enough to likely collide
randomInt 1000
} {
# 32 bit compressible signed/unsigned
randpath {randomInt 2000000000} {randomInt 4000000000}
} {
# 64 bit
randpath {randomInt 1000000000000}
} {
# Random string
randpath {randstring 1 256 alpha} \
{randstring 1 256 compr}
}
}
proc findKeyWithType {r type} {
for {set j 0} {$j < 20} {incr j} {
set k [{*}$r randomkey]
if {$k eq {}} {
return {}
}
if {[{*}$r type $k] eq $type} {
return $k
}
}
return {}
}
proc createComplexDataset {r ops {opt {}}} {
for {set j 0} {$j < $ops} {incr j} {
set k [randomKey]
set k2 [randomKey]
set f [randomValue]
set v [randomValue]
if {[lsearch -exact $opt useexpire] != -1} {
if {rand() < 0.1} {
{*}$r expire [randomKey] [randomInt 2]
}
}
randpath {
set d [expr {rand()}]
} {
set d [expr {rand()}]
} {
set d [expr {rand()}]
} {
set d [expr {rand()}]
} {
set d [expr {rand()}]
} {
randpath {set d +inf} {set d -inf}
}
set t [{*}$r type $k]
if {$t eq {none}} {
randpath {
{*}$r set $k $v
} {
{*}$r lpush $k $v
} {
{*}$r sadd $k $v
} {
{*}$r zadd $k $d $v
} {
{*}$r hset $k $f $v
} {
{*}$r del $k
}
set t [{*}$r type $k]
}
switch $t {
{string} {
# Nothing to do
}
{list} {
randpath {{*}$r lpush $k $v} \
{{*}$r rpush $k $v} \
{{*}$r lrem $k 0 $v} \
{{*}$r rpop $k} \
{{*}$r lpop $k}
}
{set} {
randpath {{*}$r sadd $k $v} \
{{*}$r srem $k $v} \
{
set otherset [findKeyWithType {*}$r set]
if {$otherset ne {}} {
randpath {
{*}$r sunionstore $k2 $k $otherset
} {
{*}$r sinterstore $k2 $k $otherset
} {
{*}$r sdiffstore $k2 $k $otherset
}
}
}
}
{zset} {
randpath {{*}$r zadd $k $d $v} \
{{*}$r zrem $k $v} \
{
set otherzset [findKeyWithType {*}$r zset]
if {$otherzset ne {}} {
randpath {
{*}$r zunionstore $k2 2 $k $otherzset
} {
{*}$r zinterstore $k2 2 $k $otherzset
}
}
}
}
{hash} {
randpath {{*}$r hset $k $f $v} \
{{*}$r hdel $k $f}
}
}
}
}
proc formatCommand {args} {
set cmd "*[llength $args]\r\n"
foreach a $args {
append cmd "$[string length $a]\r\n$a\r\n"
}
set _ $cmd
}
proc csvdump r {
set o {}
foreach k [lsort [{*}$r keys *]] {
set type [{*}$r type $k]
append o [csvstring $k] , [csvstring $type] ,
switch $type {
string {
append o [csvstring [{*}$r get $k]] "\n"
}
list {
foreach e [{*}$r lrange $k 0 -1] {
append o [csvstring $e] ,
}
append o "\n"
}
set {
foreach e [lsort [{*}$r smembers $k]] {
append o [csvstring $e] ,
}
append o "\n"
}
zset {
foreach e [{*}$r zrange $k 0 -1 withscores] {
append o [csvstring $e] ,
}
append o "\n"
}
hash {
set fields [{*}$r hgetall $k]
set newfields {}
foreach {k v} $fields {
lappend newfields [list $k $v]
}
set fields [lsort -index 0 $newfields]
foreach kv $fields {
append o [csvstring [lindex $kv 0]] ,
append o [csvstring [lindex $kv 1]] ,
}
append o "\n"
}
}
}
return $o
}
proc csvstring s {
return "\"$s\""
}
proc roundFloat f {
format "%.10g" $f
}
proc find_available_port start {
for {set j $start} {$j < $start+1024} {incr j} {
if {[catch {
set fd [socket 127.0.0.1 $j]
}]} {
return $j
} else {
close $fd
}
}
if {$j == $start+1024} {
error "Can't find a non busy port in the $start-[expr {$start+1023}] range."
}
}
# Test if TERM looks like to support colors
proc color_term {} {
expr {[info exists ::env(TERM)] && [string match *xterm* $::env(TERM)]}
}
proc colorstr {color str} {
if {[color_term]} {
set b 0
if {[string range $color 0 4] eq {bold-}} {
set b 1
set color [string range $color 5 end]
}
switch $color {
red {set colorcode {31}}
green {set colorcode {32}}
yellow {set colorcode {33}}
blue {set colorcode {34}}
magenta {set colorcode {35}}
cyan {set colorcode {36}}
white {set colorcode {37}}
default {set colorcode {37}}
}
if {$colorcode ne {}} {
return "\033\[$b;${colorcode};49m$str\033\[0m"
}
} else {
return $str
}
}
# Execute a background process writing random data for the specified number
# of seconds to the specified Redis instance.
proc start_write_load {host port seconds} {
set tclsh [info nameofexecutable]
exec $tclsh tests/helpers/gen_write_load.tcl $host $port $seconds &
}
# Stop a process generating write load executed with start_write_load.
proc stop_write_load {handle} {
catch {exec /bin/kill -9 $handle}
}
|