util.tcl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. # Useful for some test
  21. proc zlistAlikeSort {a b} {
  22. if {[lindex $a 0] > [lindex $b 0]} {return 1}
  23. if {[lindex $a 0] < [lindex $b 0]} {return -1}
  24. string compare [lindex $a 1] [lindex $b 1]
  25. }
  26. # Return all log lines starting with the first line that contains a warning.
  27. # Generally, this will be an assertion error with a stack trace.
  28. proc warnings_from_file {filename} {
  29. set lines [split [exec cat $filename] "\n"]
  30. set matched 0
  31. set logall 0
  32. set result {}
  33. foreach line $lines {
  34. if {[string match {*REDIS BUG REPORT START*} $line]} {
  35. set logall 1
  36. }
  37. if {[regexp {^\[\d+\]\s+\d+\s+\w+\s+\d{2}:\d{2}:\d{2} \#} $line]} {
  38. set matched 1
  39. }
  40. if {$logall || $matched} {
  41. lappend result $line
  42. }
  43. }
  44. join $result "\n"
  45. }
  46. # Return value for INFO property
  47. proc status {r property} {
  48. if {[regexp "\r\n$property:(.*?)\r\n" [{*}$r info] _ value]} {
  49. set _ $value
  50. }
  51. }
  52. proc waitForBgsave r {
  53. while 1 {
  54. if {[status r rdb_bgsave_in_progress] eq 1} {
  55. if {$::verbose} {
  56. puts -nonewline "\nWaiting for background save to finish... "
  57. flush stdout
  58. }
  59. after 1000
  60. } else {
  61. break
  62. }
  63. }
  64. }
  65. proc waitForBgrewriteaof r {
  66. while 1 {
  67. if {[status r aof_rewrite_in_progress] eq 1} {
  68. if {$::verbose} {
  69. puts -nonewline "\nWaiting for background AOF rewrite to finish... "
  70. flush stdout
  71. }
  72. after 1000
  73. } else {
  74. break
  75. }
  76. }
  77. }
  78. proc wait_for_sync r {
  79. while 1 {
  80. if {[status $r master_link_status] eq "down"} {
  81. after 10
  82. } else {
  83. break
  84. }
  85. }
  86. }
  87. # Random integer between 0 and max (excluded).
  88. proc randomInt {max} {
  89. expr {int(rand()*$max)}
  90. }
  91. # Random signed integer between -max and max (both extremes excluded).
  92. proc randomSignedInt {max} {
  93. set i [randomInt $max]
  94. if {rand() > 0.5} {
  95. set i -$i
  96. }
  97. return $i
  98. }
  99. proc randpath args {
  100. set path [expr {int(rand()*[llength $args])}]
  101. uplevel 1 [lindex $args $path]
  102. }
  103. proc randomValue {} {
  104. randpath {
  105. # Small enough to likely collide
  106. randomSignedInt 1000
  107. } {
  108. # 32 bit compressible signed/unsigned
  109. randpath {randomSignedInt 2000000000} {randomSignedInt 4000000000}
  110. } {
  111. # 64 bit
  112. randpath {randomSignedInt 1000000000000}
  113. } {
  114. # Random string
  115. randpath {randstring 0 256 alpha} \
  116. {randstring 0 256 compr} \
  117. {randstring 0 256 binary}
  118. }
  119. }
  120. proc randomKey {} {
  121. randpath {
  122. # Small enough to likely collide
  123. randomInt 1000
  124. } {
  125. # 32 bit compressible signed/unsigned
  126. randpath {randomInt 2000000000} {randomInt 4000000000}
  127. } {
  128. # 64 bit
  129. randpath {randomInt 1000000000000}
  130. } {
  131. # Random string
  132. randpath {randstring 1 256 alpha} \
  133. {randstring 1 256 compr}
  134. }
  135. }
  136. proc findKeyWithType {r type} {
  137. for {set j 0} {$j < 20} {incr j} {
  138. set k [{*}$r randomkey]
  139. if {$k eq {}} {
  140. return {}
  141. }
  142. if {[{*}$r type $k] eq $type} {
  143. return $k
  144. }
  145. }
  146. return {}
  147. }
  148. proc createComplexDataset {r ops {opt {}}} {
  149. for {set j 0} {$j < $ops} {incr j} {
  150. set k [randomKey]
  151. set k2 [randomKey]
  152. set f [randomValue]
  153. set v [randomValue]
  154. if {[lsearch -exact $opt useexpire] != -1} {
  155. if {rand() < 0.1} {
  156. {*}$r expire [randomKey] [randomInt 2]
  157. }
  158. }
  159. randpath {
  160. set d [expr {rand()}]
  161. } {
  162. set d [expr {rand()}]
  163. } {
  164. set d [expr {rand()}]
  165. } {
  166. set d [expr {rand()}]
  167. } {
  168. set d [expr {rand()}]
  169. } {
  170. randpath {set d +inf} {set d -inf}
  171. }
  172. set t [{*}$r type $k]
  173. if {$t eq {none}} {
  174. randpath {
  175. {*}$r set $k $v
  176. } {
  177. {*}$r lpush $k $v
  178. } {
  179. {*}$r sadd $k $v
  180. } {
  181. {*}$r zadd $k $d $v
  182. } {
  183. {*}$r hset $k $f $v
  184. } {
  185. {*}$r del $k
  186. }
  187. set t [{*}$r type $k]
  188. }
  189. switch $t {
  190. {string} {
  191. # Nothing to do
  192. }
  193. {list} {
  194. randpath {{*}$r lpush $k $v} \
  195. {{*}$r rpush $k $v} \
  196. {{*}$r lrem $k 0 $v} \
  197. {{*}$r rpop $k} \
  198. {{*}$r lpop $k}
  199. }
  200. {set} {
  201. randpath {{*}$r sadd $k $v} \
  202. {{*}$r srem $k $v} \
  203. {
  204. set otherset [findKeyWithType {*}$r set]
  205. if {$otherset ne {}} {
  206. randpath {
  207. {*}$r sunionstore $k2 $k $otherset
  208. } {
  209. {*}$r sinterstore $k2 $k $otherset
  210. } {
  211. {*}$r sdiffstore $k2 $k $otherset
  212. }
  213. }
  214. }
  215. }
  216. {zset} {
  217. randpath {{*}$r zadd $k $d $v} \
  218. {{*}$r zrem $k $v} \
  219. {
  220. set otherzset [findKeyWithType {*}$r zset]
  221. if {$otherzset ne {}} {
  222. randpath {
  223. {*}$r zunionstore $k2 2 $k $otherzset
  224. } {
  225. {*}$r zinterstore $k2 2 $k $otherzset
  226. }
  227. }
  228. }
  229. }
  230. {hash} {
  231. randpath {{*}$r hset $k $f $v} \
  232. {{*}$r hdel $k $f}
  233. }
  234. }
  235. }
  236. }
  237. proc formatCommand {args} {
  238. set cmd "*[llength $args]\r\n"
  239. foreach a $args {
  240. append cmd "$[string length $a]\r\n$a\r\n"
  241. }
  242. set _ $cmd
  243. }
  244. proc csvdump r {
  245. set o {}
  246. for {set db 0} {$db < 16} {incr db} {
  247. {*}$r select $db
  248. foreach k [lsort [{*}$r keys *]] {
  249. set type [{*}$r type $k]
  250. append o [csvstring $db] , [csvstring $k] , [csvstring $type] ,
  251. switch $type {
  252. string {
  253. append o [csvstring [{*}$r get $k]] "\n"
  254. }
  255. list {
  256. foreach e [{*}$r lrange $k 0 -1] {
  257. append o [csvstring $e] ,
  258. }
  259. append o "\n"
  260. }
  261. set {
  262. foreach e [lsort [{*}$r smembers $k]] {
  263. append o [csvstring $e] ,
  264. }
  265. append o "\n"
  266. }
  267. zset {
  268. foreach e [{*}$r zrange $k 0 -1 withscores] {
  269. append o [csvstring $e] ,
  270. }
  271. append o "\n"
  272. }
  273. hash {
  274. set fields [{*}$r hgetall $k]
  275. set newfields {}
  276. foreach {k v} $fields {
  277. lappend newfields [list $k $v]
  278. }
  279. set fields [lsort -index 0 $newfields]
  280. foreach kv $fields {
  281. append o [csvstring [lindex $kv 0]] ,
  282. append o [csvstring [lindex $kv 1]] ,
  283. }
  284. append o "\n"
  285. }
  286. }
  287. }
  288. }
  289. {*}$r select 9
  290. return $o
  291. }
  292. proc csvstring s {
  293. return "\"$s\""
  294. }
  295. proc roundFloat f {
  296. format "%.10g" $f
  297. }
  298. proc find_available_port start {
  299. for {set j $start} {$j < $start+1024} {incr j} {
  300. if {[catch {set fd1 [socket 127.0.0.1 $j]}] &&
  301. [catch {set fd2 [socket 127.0.0.1 [expr $j+10000]]}]} {
  302. return $j
  303. } else {
  304. catch {
  305. close $fd1
  306. close $fd2
  307. }
  308. }
  309. }
  310. if {$j == $start+1024} {
  311. error "Can't find a non busy port in the $start-[expr {$start+1023}] range."
  312. }
  313. }
  314. # Test if TERM looks like to support colors
  315. proc color_term {} {
  316. expr {[info exists ::env(TERM)] && [string match *xterm* $::env(TERM)]}
  317. }
  318. proc colorstr {color str} {
  319. if {[color_term]} {
  320. set b 0
  321. if {[string range $color 0 4] eq {bold-}} {
  322. set b 1
  323. set color [string range $color 5 end]
  324. }
  325. switch $color {
  326. red {set colorcode {31}}
  327. green {set colorcode {32}}
  328. yellow {set colorcode {33}}
  329. blue {set colorcode {34}}
  330. magenta {set colorcode {35}}
  331. cyan {set colorcode {36}}
  332. white {set colorcode {37}}
  333. default {set colorcode {37}}
  334. }
  335. if {$colorcode ne {}} {
  336. return "\033\[$b;${colorcode};49m$str\033\[0m"
  337. }
  338. } else {
  339. return $str
  340. }
  341. }
  342. # Execute a background process writing random data for the specified number
  343. # of seconds to the specified Redis instance.
  344. proc start_write_load {host port seconds} {
  345. set tclsh [info nameofexecutable]
  346. exec $tclsh tests/helpers/gen_write_load.tcl $host $port $seconds &
  347. }
  348. # Stop a process generating write load executed with start_write_load.
  349. proc stop_write_load {handle} {
  350. catch {exec /bin/kill -9 $handle}
  351. }