2
0

memefficiency.tcl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. proc test_memory_efficiency {range} {
  2. r flushall
  3. set rd [redis_deferring_client]
  4. set base_mem [s used_memory]
  5. set written 0
  6. for {set j 0} {$j < 10000} {incr j} {
  7. set key key:$j
  8. set val [string repeat A [expr {int(rand()*$range)}]]
  9. $rd set $key $val
  10. incr written [string length $key]
  11. incr written [string length $val]
  12. incr written 2 ;# A separator is the minimum to store key-value data.
  13. }
  14. for {set j 0} {$j < 10000} {incr j} {
  15. $rd read ; # Discard replies
  16. }
  17. set current_mem [s used_memory]
  18. set used [expr {$current_mem-$base_mem}]
  19. set efficiency [expr {double($written)/$used}]
  20. return $efficiency
  21. }
  22. start_server {tags {"memefficiency"}} {
  23. foreach {size_range expected_min_efficiency} {
  24. 32 0.15
  25. 64 0.25
  26. 128 0.35
  27. 1024 0.75
  28. 16384 0.82
  29. } {
  30. test "Memory efficiency with values in range $size_range" {
  31. set efficiency [test_memory_efficiency $size_range]
  32. assert {$efficiency >= $expected_min_efficiency}
  33. }
  34. }
  35. }
  36. start_server {tags {"defrag"}} {
  37. if {[string match {*jemalloc*} [s mem_allocator]]} {
  38. test "Active defrag" {
  39. r config set activedefrag no
  40. r config set active-defrag-threshold-lower 5
  41. r config set active-defrag-cycle-min 65
  42. r config set active-defrag-cycle-max 75
  43. r config set active-defrag-ignore-bytes 2mb
  44. r config set maxmemory 100mb
  45. r config set maxmemory-policy allkeys-lru
  46. r debug populate 700000 asdf 150
  47. r debug populate 170000 asdf 300
  48. r ping ;# trigger eviction following the previous population
  49. after 120 ;# serverCron only updates the info once in 100ms
  50. set frag [s allocator_frag_ratio]
  51. if {$::verbose} {
  52. puts "frag $frag"
  53. }
  54. assert {$frag >= 1.4}
  55. catch {r config set activedefrag yes} e
  56. if {![string match {DISABLED*} $e]} {
  57. # Wait for the active defrag to start working (decision once a
  58. # second).
  59. wait_for_condition 50 100 {
  60. [s active_defrag_running] ne 0
  61. } else {
  62. fail "defrag not started."
  63. }
  64. # Wait for the active defrag to stop working.
  65. wait_for_condition 150 100 {
  66. [s active_defrag_running] eq 0
  67. } else {
  68. after 120 ;# serverCron only updates the info once in 100ms
  69. puts [r info memory]
  70. puts [r memory malloc-stats]
  71. fail "defrag didn't stop."
  72. }
  73. # Test the the fragmentation is lower.
  74. after 120 ;# serverCron only updates the info once in 100ms
  75. set frag [s allocator_frag_ratio]
  76. if {$::verbose} {
  77. puts "frag $frag"
  78. }
  79. assert {$frag < 1.1}
  80. } else {
  81. set _ ""
  82. }
  83. } {}
  84. test "Active defrag big keys" {
  85. r flushdb
  86. r config resetstat
  87. r config set save "" ;# prevent bgsave from interfereing with save below
  88. r config set activedefrag no
  89. r config set active-defrag-max-scan-fields 1000
  90. r config set active-defrag-threshold-lower 5
  91. r config set active-defrag-cycle-min 65
  92. r config set active-defrag-cycle-max 75
  93. r config set active-defrag-ignore-bytes 2mb
  94. r config set maxmemory 0
  95. r config set list-max-ziplist-size 5 ;# list of 10k items will have 2000 quicklist nodes
  96. r config set stream-node-max-entries 5
  97. r hmset hash h1 v1 h2 v2 h3 v3
  98. r lpush list a b c d
  99. r zadd zset 0 a 1 b 2 c 3 d
  100. r sadd set a b c d
  101. r xadd stream * item 1 value a
  102. r xadd stream * item 2 value b
  103. r xgroup create stream mygroup 0
  104. r xreadgroup GROUP mygroup Alice COUNT 1 STREAMS stream >
  105. # create big keys with 10k items
  106. set rd [redis_deferring_client]
  107. for {set j 0} {$j < 10000} {incr j} {
  108. $rd hset bighash $j [concat "asdfasdfasdf" $j]
  109. $rd lpush biglist [concat "asdfasdfasdf" $j]
  110. $rd zadd bigzset $j [concat "asdfasdfasdf" $j]
  111. $rd sadd bigset [concat "asdfasdfasdf" $j]
  112. $rd xadd bigstream * item 1 value a
  113. }
  114. for {set j 0} {$j < 50000} {incr j} {
  115. $rd read ; # Discard replies
  116. }
  117. set expected_frag 1.7
  118. if {$::accurate} {
  119. # scale the hash to 1m fields in order to have a measurable the latency
  120. for {set j 10000} {$j < 1000000} {incr j} {
  121. $rd hset bighash $j [concat "asdfasdfasdf" $j]
  122. }
  123. for {set j 10000} {$j < 1000000} {incr j} {
  124. $rd read ; # Discard replies
  125. }
  126. # creating that big hash, increased used_memory, so the relative frag goes down
  127. set expected_frag 1.3
  128. }
  129. # add a mass of string keys
  130. for {set j 0} {$j < 500000} {incr j} {
  131. $rd setrange $j 150 a
  132. }
  133. for {set j 0} {$j < 500000} {incr j} {
  134. $rd read ; # Discard replies
  135. }
  136. assert {[r dbsize] == 500010}
  137. # create some fragmentation
  138. for {set j 0} {$j < 500000} {incr j 2} {
  139. $rd del $j
  140. }
  141. for {set j 0} {$j < 500000} {incr j 2} {
  142. $rd read ; # Discard replies
  143. }
  144. assert {[r dbsize] == 250010}
  145. # start defrag
  146. after 120 ;# serverCron only updates the info once in 100ms
  147. set frag [s allocator_frag_ratio]
  148. if {$::verbose} {
  149. puts "frag $frag"
  150. }
  151. assert {$frag >= $expected_frag}
  152. r config set latency-monitor-threshold 5
  153. r latency reset
  154. set digest [r debug digest]
  155. catch {r config set activedefrag yes} e
  156. if {![string match {DISABLED*} $e]} {
  157. # wait for the active defrag to start working (decision once a second)
  158. wait_for_condition 50 100 {
  159. [s active_defrag_running] ne 0
  160. } else {
  161. fail "defrag not started."
  162. }
  163. # wait for the active defrag to stop working
  164. wait_for_condition 500 100 {
  165. [s active_defrag_running] eq 0
  166. } else {
  167. after 120 ;# serverCron only updates the info once in 100ms
  168. puts [r info memory]
  169. puts [r memory malloc-stats]
  170. fail "defrag didn't stop."
  171. }
  172. # test the the fragmentation is lower
  173. after 120 ;# serverCron only updates the info once in 100ms
  174. set frag [s allocator_frag_ratio]
  175. set max_latency 0
  176. foreach event [r latency latest] {
  177. lassign $event eventname time latency max
  178. if {$eventname == "active-defrag-cycle"} {
  179. set max_latency $max
  180. }
  181. }
  182. if {$::verbose} {
  183. puts "frag $frag"
  184. puts "max latency $max_latency"
  185. puts [r latency latest]
  186. puts [r latency history active-defrag-cycle]
  187. }
  188. assert {$frag < 1.1}
  189. # due to high fragmentation, 10hz, and active-defrag-cycle-max set to 75,
  190. # we expect max latency to be not much higher than 75ms
  191. assert {$max_latency <= 120}
  192. }
  193. # verify the data isn't corrupted or changed
  194. set newdigest [r debug digest]
  195. assert {$digest eq $newdigest}
  196. r save ;# saving an rdb iterates over all the data / pointers
  197. } {OK}
  198. }
  199. }