2
0

memefficiency.tcl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 25
  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 100 100 {
  66. [s active_defrag_running] eq 0
  67. } else {
  68. puts [r info memory]
  69. puts [r memory malloc-stats]
  70. fail "defrag didn't stop."
  71. }
  72. # Test the the fragmentation is lower.
  73. after 120 ;# serverCron only updates the info once in 100ms
  74. set frag [s allocator_frag_ratio]
  75. if {$::verbose} {
  76. puts "frag $frag"
  77. }
  78. assert {$frag < 1.1}
  79. } else {
  80. set _ ""
  81. }
  82. } {}
  83. test "Active defrag big keys" {
  84. r flushdb
  85. r config resetstat
  86. r config set activedefrag no
  87. r config set active-defrag-max-scan-fields 1000
  88. r config set active-defrag-threshold-lower 5
  89. r config set active-defrag-cycle-min 65
  90. r config set active-defrag-cycle-max 75
  91. r config set active-defrag-ignore-bytes 2mb
  92. r config set maxmemory 0
  93. r config set list-max-ziplist-size 5 ;# list of 10k items will have 2000 quicklist nodes
  94. r hmset hash h1 v1 h2 v2 h3 v3
  95. r lpush list a b c d
  96. r zadd zset 0 a 1 b 2 c 3 d
  97. r sadd set a b c d
  98. # create big keys with 10k items
  99. set rd [redis_deferring_client]
  100. for {set j 0} {$j < 10000} {incr j} {
  101. $rd hset bighash $j [concat "asdfasdfasdf" $j]
  102. $rd lpush biglist [concat "asdfasdfasdf" $j]
  103. $rd zadd bigzset $j [concat "asdfasdfasdf" $j]
  104. $rd sadd bigset [concat "asdfasdfasdf" $j]
  105. }
  106. for {set j 0} {$j < 40000} {incr j} {
  107. $rd read ; # Discard replies
  108. }
  109. set expected_frag 1.7
  110. if {$::accurate} {
  111. # scale the hash to 1m fields in order to have a measurable the latency
  112. for {set j 10000} {$j < 1000000} {incr j} {
  113. $rd hset bighash $j [concat "asdfasdfasdf" $j]
  114. }
  115. for {set j 10000} {$j < 1000000} {incr j} {
  116. $rd read ; # Discard replies
  117. }
  118. # creating that big hash, increased used_memory, so the relative frag goes down
  119. set expected_frag 1.3
  120. }
  121. # add a mass of string keys
  122. for {set j 0} {$j < 500000} {incr j} {
  123. $rd setrange $j 150 a
  124. }
  125. for {set j 0} {$j < 500000} {incr j} {
  126. $rd read ; # Discard replies
  127. }
  128. assert {[r dbsize] == 500008}
  129. # create some fragmentation
  130. for {set j 0} {$j < 500000} {incr j 2} {
  131. $rd del $j
  132. }
  133. for {set j 0} {$j < 500000} {incr j 2} {
  134. $rd read ; # Discard replies
  135. }
  136. assert {[r dbsize] == 250008}
  137. # start defrag
  138. after 120 ;# serverCron only updates the info once in 100ms
  139. set frag [s allocator_frag_ratio]
  140. if {$::verbose} {
  141. puts "frag $frag"
  142. }
  143. assert {$frag >= $expected_frag}
  144. r config set latency-monitor-threshold 5
  145. r latency reset
  146. catch {r config set activedefrag yes} e
  147. if {![string match {DISABLED*} $e]} {
  148. # wait for the active defrag to start working (decision once a second)
  149. wait_for_condition 50 100 {
  150. [s active_defrag_running] ne 0
  151. } else {
  152. fail "defrag not started."
  153. }
  154. # wait for the active defrag to stop working
  155. wait_for_condition 500 100 {
  156. [s active_defrag_running] eq 0
  157. } else {
  158. puts [r info memory]
  159. puts [r memory malloc-stats]
  160. fail "defrag didn't stop."
  161. }
  162. # test the the fragmentation is lower
  163. after 120 ;# serverCron only updates the info once in 100ms
  164. set frag [s allocator_frag_ratio]
  165. set max_latency 0
  166. foreach event [r latency latest] {
  167. lassign $event eventname time latency max
  168. if {$eventname == "active-defrag-cycle"} {
  169. set max_latency $max
  170. }
  171. }
  172. if {$::verbose} {
  173. puts "frag $frag"
  174. puts "max latency $max_latency"
  175. puts [r latency latest]
  176. puts [r latency history active-defrag-cycle]
  177. }
  178. assert {$frag < 1.1}
  179. # due to high fragmentation, 10hz, and active-defrag-cycle-max set to 75,
  180. # we expect max latency to be not much higher than 75ms
  181. assert {$max_latency <= 80}
  182. } else {
  183. set _ ""
  184. }
  185. } {}
  186. }
  187. }