memefficiency.tcl 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if 0 {
  37. start_server {tags {"defrag"}} {
  38. if {[string match {*jemalloc*} [s mem_allocator]]} {
  39. test "Active defrag" {
  40. r config set activedefrag no
  41. r config set active-defrag-threshold-lower 5
  42. r config set active-defrag-ignore-bytes 2mb
  43. r config set maxmemory 100mb
  44. r config set maxmemory-policy allkeys-lru
  45. r debug populate 700000 asdf 150
  46. r debug populate 170000 asdf 300
  47. set frag [s mem_fragmentation_ratio]
  48. assert {$frag >= 1.7}
  49. r config set activedefrag yes
  50. after 1500 ;# active defrag tests the status once a second.
  51. set hits [s active_defrag_hits]
  52. # wait for the active defrag to stop working
  53. set tries 0
  54. while { True } {
  55. incr tries
  56. after 500
  57. set prev_hits $hits
  58. set hits [s active_defrag_hits]
  59. if {$hits == $prev_hits} {
  60. break
  61. }
  62. assert {$tries < 100}
  63. }
  64. # TODO: we need to expose more accurate fragmentation info
  65. # i.e. the allocator used and active pages
  66. # instead we currently look at RSS so we need to ask for purge
  67. r memory purge
  68. # Test the the fragmentation is lower and that the defragger
  69. # stopped working
  70. set frag [s mem_fragmentation_ratio]
  71. assert {$frag < 1.55}
  72. set misses [s active_defrag_misses]
  73. after 500
  74. set misses2 [s active_defrag_misses]
  75. assert {$misses2 == $misses}
  76. }
  77. }
  78. }
  79. }