test-lru.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. require 'rubygems'
  2. require 'redis'
  3. r = Redis.new
  4. r.config("SET","maxmemory","2000000")
  5. r.config("SET","maxmemory-policy","allkeys-lru")
  6. r.config("SET","maxmemory-samples",5)
  7. r.config("RESETSTAT")
  8. r.flushall
  9. puts <<EOF
  10. <html>
  11. <body>
  12. <style>
  13. .box {
  14. width:5px;
  15. height:5px;
  16. float:left;
  17. margin: 1px;
  18. }
  19. .old {
  20. border: 1px black solid;
  21. }
  22. .new {
  23. border: 1px green solid;
  24. }
  25. .ex {
  26. background-color: #666;
  27. }
  28. </style>
  29. <pre>
  30. EOF
  31. # Fill
  32. oldsize = r.dbsize
  33. id = 0
  34. while true
  35. id += 1
  36. r.set(id,"foo")
  37. newsize = r.dbsize
  38. break if newsize == oldsize
  39. oldsize = newsize
  40. end
  41. inserted = r.dbsize
  42. first_set_max_id = id
  43. puts "#{r.dbsize} keys inserted"
  44. # Access keys sequentially
  45. puts "Access keys sequentially"
  46. (1..first_set_max_id).each{|id|
  47. r.get(id)
  48. # sleep 0.001
  49. }
  50. # Insert more 50% keys. We expect that the new keys
  51. half = inserted/2
  52. puts "Insert enough keys to evict half the keys we inserted"
  53. add = 0
  54. while true
  55. add += 1
  56. id += 1
  57. r.set(id,"foo")
  58. break if r.info['evicted_keys'].to_i >= half
  59. end
  60. puts "#{add} additional keys added."
  61. puts "#{r.dbsize} keys in DB"
  62. # Check if evicted keys respect LRU
  63. # We consider errors from 1 to N progressively more serious as they violate
  64. # more the access pattern.
  65. errors = 0
  66. e = 1
  67. edecr = 1.0/(first_set_max_id/2)
  68. (1..(first_set_max_id/2)).each{|id|
  69. e -= edecr if e > 0
  70. e = 0 if e < 0
  71. if r.exists(id)
  72. errors += e
  73. end
  74. }
  75. puts "#{errors} errors!"
  76. puts "</pre>"
  77. # Generate the graphical representation
  78. (1..id).each{|id|
  79. # Mark first set and added items in a different way.
  80. c = "box"
  81. if id <= first_set_max_id
  82. c << " old"
  83. else
  84. c << " new"
  85. end
  86. # Add class if exists
  87. c << " ex" if r.exists(id)
  88. puts "<div class=\"#{c}\"></div>"
  89. }
  90. # Close HTML page
  91. puts <<EOF
  92. </body>
  93. </html>
  94. EOF