2
0

hyperloglog.tcl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 {PFADD, PFCOUNT, PFMERGE type checking works} {
  106. r set foo bar
  107. catch {r pfadd foo 1} e
  108. assert_match {*WRONGTYPE*} $e
  109. catch {r pfcount foo} e
  110. assert_match {*WRONGTYPE*} $e
  111. catch {r pfmerge bar foo} e
  112. assert_match {*WRONGTYPE*} $e
  113. catch {r pfmerge foo bar} e
  114. assert_match {*WRONGTYPE*} $e
  115. }
  116. test {PFMERGE results on the cardinality of union of sets} {
  117. r del hll hll1 hll2 hll3
  118. r pfadd hll1 a b c
  119. r pfadd hll2 b c d
  120. r pfadd hll3 c d e
  121. r pfmerge hll hll1 hll2 hll3
  122. r pfcount hll
  123. } {5}
  124. test {PFCOUNT multiple-keys merge returns cardinality of union} {
  125. r del hll1 hll2 hll3
  126. for {set x 1} {$x < 10000} {incr x} {
  127. # Force dense representation of hll2
  128. r pfadd hll1 "foo-$x"
  129. r pfadd hll2 "bar-$x"
  130. r pfadd hll3 "zap-$x"
  131. set card [r pfcount hll1 hll2 hll3]
  132. set realcard [expr {$x*3}]
  133. set err [expr {abs($card-$realcard)}]
  134. assert {$err < (double($card)/100)*5}
  135. }
  136. }
  137. test {PFDEBUG GETREG returns the HyperLogLog raw registers} {
  138. r del hll
  139. r pfadd hll 1 2 3
  140. llength [r pfdebug getreg hll]
  141. } {16384}
  142. }