hyperloglog.tcl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. start_server {tags {"hll"}} {
  2. test {HyperLogLog self test passes} {
  3. catch {r pfselftest} e
  4. set e
  5. } {OK}
  6. test {PFADD without arguments creates an HLL value} {
  7. r pfadd hll
  8. r exists hll
  9. } {1}
  10. test {Approximated cardinality after creation is zero} {
  11. r pfcount hll
  12. } {0}
  13. test {PFADD returns 1 when at least 1 reg was modified} {
  14. r pfadd hll a b c
  15. } {1}
  16. test {PFADD returns 0 when no reg was modified} {
  17. r pfadd hll a b c
  18. } {0}
  19. test {PFADD works with empty string (regression)} {
  20. r pfadd hll ""
  21. }
  22. # Note that the self test stresses much better the
  23. # cardinality estimation error. We are testing just the
  24. # command implementation itself here.
  25. test {PFCOUNT returns approximated cardinality of set} {
  26. r del hll
  27. set res {}
  28. r pfadd hll 1 2 3 4 5
  29. lappend res [r pfcount hll]
  30. # Call it again to test cached value invalidation.
  31. r pfadd hll 6 7 8 8 9 10
  32. lappend res [r pfcount hll]
  33. set res
  34. } {5 10}
  35. test {HyperLogLogs are promote from sparse to dense} {
  36. r del hll
  37. r config set hll-sparse-max-bytes 3000
  38. set n 0
  39. while {$n < 100000} {
  40. set elements {}
  41. for {set j 0} {$j < 100} {incr j} {lappend elements [expr rand()]}
  42. incr n 100
  43. r pfadd hll {*}$elements
  44. set card [r pfcount hll]
  45. set err [expr {abs($card-$n)}]
  46. assert {$err < (double($card)/100)*5}
  47. if {$n < 1000} {
  48. assert {[r pfdebug encoding hll] eq {sparse}}
  49. } elseif {$n > 10000} {
  50. assert {[r pfdebug encoding hll] eq {dense}}
  51. }
  52. }
  53. }
  54. test {HyperLogLog sparse encoding stress test} {
  55. for {set x 0} {$x < 1000} {incr x} {
  56. r del hll1 hll2
  57. set numele [randomInt 100]
  58. set elements {}
  59. for {set j 0} {$j < $numele} {incr j} {
  60. lappend elements [expr rand()]
  61. }
  62. # Force dense representation of hll2
  63. r pfadd hll2
  64. r pfdebug todense hll2
  65. r pfadd hll1 {*}$elements
  66. r pfadd hll2 {*}$elements
  67. assert {[r pfdebug encoding hll1] eq {sparse}}
  68. assert {[r pfdebug encoding hll2] eq {dense}}
  69. # Cardinality estimated should match exactly.
  70. assert {[r pfcount hll1] eq [r pfcount hll2]}
  71. }
  72. }
  73. test {Corrupted sparse HyperLogLogs are detected: Additionl at tail} {
  74. r del hll
  75. r pfadd hll a b c
  76. r append hll "hello"
  77. set e {}
  78. catch {r pfcount hll} e
  79. set e
  80. } {*INVALIDOBJ*}
  81. test {Corrupted sparse HyperLogLogs are detected: Broken magic} {
  82. r del hll
  83. r pfadd hll a b c
  84. r setrange hll 0 "0123"
  85. set e {}
  86. catch {r pfcount hll} e
  87. set e
  88. } {*WRONGTYPE*}
  89. test {Corrupted sparse HyperLogLogs are detected: Invalid encoding} {
  90. r del hll
  91. r pfadd hll a b c
  92. r setrange hll 4 "x"
  93. set e {}
  94. catch {r pfcount hll} e
  95. set e
  96. } {*WRONGTYPE*}
  97. test {Corrupted dense HyperLogLogs are detected: Wrong length} {
  98. r del hll
  99. r pfadd hll a b c
  100. r setrange hll 4 "\x00"
  101. set e {}
  102. catch {r pfcount hll} e
  103. set e
  104. } {*WRONGTYPE*}
  105. test {Fuzzing dense/sparse encoding: Redis should always detect errors} {
  106. for {set j 0} {$j < 1000} {incr j} {
  107. r del hll
  108. set items {}
  109. set numitems [randomInt 3000]
  110. for {set i 0} {$i < $numitems} {incr i} {
  111. lappend items [expr {rand()}]
  112. }
  113. r pfadd hll {*}$items
  114. # Corrupt it in some random way.
  115. for {set i 0} {$i < 5} {incr i} {
  116. set len [r strlen hll]
  117. set pos [randomInt $len]
  118. set byte [randstring 1 1 binary]
  119. r setrange hll $pos $byte
  120. # Don't modify more bytes 50% of times
  121. if {rand() < 0.5} break
  122. }
  123. # Use the hyperloglog to check if it crashes
  124. # Redis in some way.
  125. catch {
  126. r pfcount hll
  127. }
  128. }
  129. }
  130. test {PFADD, PFCOUNT, PFMERGE type checking works} {
  131. r set foo bar
  132. catch {r pfadd foo 1} e
  133. assert_match {*WRONGTYPE*} $e
  134. catch {r pfcount foo} e
  135. assert_match {*WRONGTYPE*} $e
  136. catch {r pfmerge bar foo} e
  137. assert_match {*WRONGTYPE*} $e
  138. catch {r pfmerge foo bar} e
  139. assert_match {*WRONGTYPE*} $e
  140. }
  141. test {PFMERGE results on the cardinality of union of sets} {
  142. r del hll hll1 hll2 hll3
  143. r pfadd hll1 a b c
  144. r pfadd hll2 b c d
  145. r pfadd hll3 c d e
  146. r pfmerge hll hll1 hll2 hll3
  147. r pfcount hll
  148. } {5}
  149. test {PFCOUNT multiple-keys merge returns cardinality of union #1} {
  150. r del hll1 hll2 hll3
  151. for {set x 1} {$x < 10000} {incr x} {
  152. r pfadd hll1 "foo-$x"
  153. r pfadd hll2 "bar-$x"
  154. r pfadd hll3 "zap-$x"
  155. set card [r pfcount hll1 hll2 hll3]
  156. set realcard [expr {$x*3}]
  157. set err [expr {abs($card-$realcard)}]
  158. assert {$err < (double($card)/100)*5}
  159. }
  160. }
  161. test {PFCOUNT multiple-keys merge returns cardinality of union #2} {
  162. r del hll1 hll2 hll3
  163. set elements {}
  164. for {set x 1} {$x < 10000} {incr x} {
  165. for {set j 1} {$j <= 3} {incr j} {
  166. set rint [randomInt 20000]
  167. r pfadd hll$j $rint
  168. lappend elements $rint
  169. }
  170. }
  171. set realcard [llength [lsort -unique $elements]]
  172. set card [r pfcount hll1 hll2 hll3]
  173. set err [expr {abs($card-$realcard)}]
  174. assert {$err < (double($card)/100)*5}
  175. }
  176. test {PFDEBUG GETREG returns the HyperLogLog raw registers} {
  177. r del hll
  178. r pfadd hll 1 2 3
  179. llength [r pfdebug getreg hll]
  180. } {16384}
  181. test {PFADD / PFCOUNT cache invalidation works} {
  182. r del hll
  183. r pfadd hll a b c
  184. r pfcount hll
  185. assert {[r getrange hll 15 15] eq "\x00"}
  186. r pfadd hll a b c
  187. assert {[r getrange hll 15 15] eq "\x00"}
  188. r pfadd hll 1 2 3
  189. assert {[r getrange hll 15 15] eq "\x80"}
  190. }
  191. }