summaryrefslogtreecommitdiff
path: root/utils/lru/test-lru.rb
blob: ee0527ef43e5536dec65eda7338ff64554cf8965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require 'rubygems'
require 'redis'

r = Redis.new
r.config("SET","maxmemory","2000000")
r.config("SET","maxmemory-policy","allkeys-lru")
r.config("SET","maxmemory-samples",5)
r.config("RESETSTAT")
r.flushall

puts <<EOF
<html>
<body>
<style>
.box {
    width:5px;
    height:5px;
    float:left;
    margin: 1px;
}

.old {
    border: 1px black solid;
}

.new {
    border: 1px green solid;
}

.ex {
    background-color: #666;
}
</style>
<pre>
EOF

# Fill
oldsize = r.dbsize
id = 0
while true
    id += 1
    r.set(id,"foo")
    newsize = r.dbsize
    break if newsize == oldsize
    oldsize = newsize
end

inserted = r.dbsize
first_set_max_id = id
puts "#{r.dbsize} keys inserted"

# Access keys sequentially

puts "Access keys sequentially"
(1..first_set_max_id).each{|id|
    r.get(id)
#    sleep 0.001
}

# Insert more 50% keys. We expect that the new keys
half = inserted/2
puts "Insert enough keys to evict half the keys we inserted"
add = 0
while true
    add += 1
    id += 1
    r.set(id,"foo")
    break if r.info['evicted_keys'].to_i >= half
end

puts "#{add} additional keys added."
puts "#{r.dbsize} keys in DB"

# Check if evicted keys respect LRU
# We consider errors from 1 to N progressively more serious as they violate
# more the access pattern.

errors = 0
e = 1
edecr = 1.0/(first_set_max_id/2)
(1..(first_set_max_id/2)).each{|id|
    e -= edecr if e > 0
    e = 0 if e < 0
    if r.exists(id)
        errors += e
    end
}

puts "#{errors} errors!"
puts "</pre>"

# Generate the graphical representation
(1..id).each{|id|
    # Mark first set and added items in a different way.
    c = "box"
    if id <= first_set_max_id
        c << " old"
    else
        c << " new"
    end

    # Add class if exists
    c << " ex" if r.exists(id)
    puts "<div class=\"#{c}\"></div>"
}

# Close HTML page

puts <<EOF
</body>
</html>
EOF