util.tcl 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. foreach k [lsort [{*}$r keys *]] {
  247. set type [{*}$r type $k]
  248. append o [csvstring $k] , [csvstring $type] ,
  249. switch $type {
  250. string {
  251. append o [csvstring [{*}$r get $k]] "\n"
  252. }
  253. list {
  254. foreach e [{*}$r lrange $k 0 -1] {
  255. append o [csvstring $e] ,
  256. }
  257. append o "\n"
  258. }
  259. set {
  260. foreach e [lsort [{*}$r smembers $k]] {
  261. append o [csvstring $e] ,
  262. }
  263. append o "\n"
  264. }
  265. zset {
  266. foreach e [{*}$r zrange $k 0 -1 withscores] {
  267. append o [csvstring $e] ,
  268. }
  269. append o "\n"
  270. }
  271. hash {
  272. set fields [{*}$r hgetall $k]
  273. set newfields {}
  274. foreach {k v} $fields {
  275. lappend newfields [list $k $v]
  276. }
  277. set fields [lsort -index 0 $newfields]
  278. foreach kv $fields {
  279. append o [csvstring [lindex $kv 0]] ,
  280. append o [csvstring [lindex $kv 1]] ,
  281. }
  282. append o "\n"
  283. }
  284. }
  285. }
  286. return $o
  287. }
  288. proc csvstring s {
  289. return "\"$s\""
  290. }
  291. proc roundFloat f {
  292. format "%.10g" $f
  293. }
  294. proc find_available_port start {
  295. for {set j $start} {$j < $start+1024} {incr j} {
  296. if {[catch {
  297. set fd [socket 127.0.0.1 $j]
  298. }]} {
  299. return $j
  300. } else {
  301. close $fd
  302. }
  303. }
  304. if {$j == $start+1024} {
  305. error "Can't find a non busy port in the $start-[expr {$start+1023}] range."
  306. }
  307. }
  308. # Test if TERM looks like to support colors
  309. proc color_term {} {
  310. expr {[info exists ::env(TERM)] && [string match *xterm* $::env(TERM)]}
  311. }
  312. proc colorstr {color str} {
  313. if {[color_term]} {
  314. set b 0
  315. if {[string range $color 0 4] eq {bold-}} {
  316. set b 1
  317. set color [string range $color 5 end]
  318. }
  319. switch $color {
  320. red {set colorcode {31}}
  321. green {set colorcode {32}}
  322. yellow {set colorcode {33}}
  323. blue {set colorcode {34}}
  324. magenta {set colorcode {35}}
  325. cyan {set colorcode {36}}
  326. white {set colorcode {37}}
  327. default {set colorcode {37}}
  328. }
  329. if {$colorcode ne {}} {
  330. return "\033\[$b;${colorcode};40m$str\033\[0m"
  331. }
  332. } else {
  333. return $str
  334. }
  335. }