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