~jonas-drange/ubuntu-start-page/1252899-mobile-friendly

« back to all changes in this revision

Viewing changes to src/Mako-0.1.9/test/lru.py

  • Committer: Matthew Nuzum
  • Date: 2008-04-18 01:58:53 UTC
  • Revision ID: matthew.nuzum@canonical.com-20080418015853-2b8rf979z2c2exxl
adding files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from mako.util import LRUCache
 
2
import string, unittest, time, random
 
3
 
 
4
import thread
 
5
 
 
6
class item:
 
7
    def __init__(self, id):
 
8
        self.id = id
 
9
 
 
10
    def __str__(self):
 
11
        return "item id %d" % self.id
 
12
 
 
13
class LRUTest(unittest.TestCase):
 
14
 
 
15
 
 
16
    def testlru(self):                
 
17
        l = LRUCache(10, threshold=.2)
 
18
        
 
19
        for id in range(1,20):
 
20
            l[id] = item(id)
 
21
        
 
22
        # first couple of items should be gone
 
23
        self.assert_(not l.has_key(1))    
 
24
        self.assert_(not l.has_key(2))
 
25
        
 
26
        # next batch over the threshold of 10 should be present
 
27
        for id in range(11,20):
 
28
            self.assert_(l.has_key(id))
 
29
 
 
30
        l[12]
 
31
        l[15]
 
32
        l[23] = item(23)
 
33
        l[24] = item(24)
 
34
        l[25] = item(25)
 
35
        l[26] = item(26)
 
36
        l[27] = item(27)
 
37
 
 
38
        self.assert_(not l.has_key(11))
 
39
        self.assert_(not l.has_key(13))
 
40
        
 
41
        for id in (25, 24, 23, 14, 12, 19, 18, 17, 16, 15):
 
42
            self.assert_(l.has_key(id))    
 
43
 
 
44
    def disabled_test_threaded(self):
 
45
        size = 100
 
46
        threshold = .5
 
47
        all_elems = 2000
 
48
        hot_zone = range(30,40)
 
49
        cache = LRUCache(size, threshold)
 
50
        
 
51
        # element to store
 
52
        class Element(object):
 
53
            def __init__(self, id):
 
54
                self.id = id
 
55
                self.regets = 0
 
56
                
 
57
        # return an element.  we will favor ids in the relatively small
 
58
        # "hot zone" 25% of  the time.
 
59
        def get_elem():
 
60
            if random.randint(1,4) == 1:
 
61
                return hot_zone[random.randint(0, len(hot_zone) - 1)]
 
62
            else:
 
63
                return random.randint(1, all_elems)
 
64
        
 
65
        total = [0]
 
66
        # request thread.
 
67
        def request_elem():
 
68
            while True:
 
69
                total[0] += 1
 
70
                id = get_elem()
 
71
                try:
 
72
                    elem = cache[id]
 
73
                    elem.regets += 1
 
74
                except KeyError:
 
75
                    e = Element(id)
 
76
                    cache[id] = e
 
77
                    
 
78
                time.sleep(random.random() / 1000)
 
79
        for x in range(0,20):
 
80
            thread.start_new_thread(request_elem, ())
 
81
        
 
82
        # assert size doesn't grow unbounded, doesnt shrink well below size
 
83
        for x in range(0,5):
 
84
            time.sleep(1)
 
85
            print "size:", len(cache)
 
86
            assert len(cache) < size + size * threshold * 2
 
87
            assert len(cache) > size - (size * .1)
 
88
        
 
89
        # computs the average number of times a range of elements were "reused",
 
90
        # i.e. without being removed from the cache.
 
91
        def average_regets_in_range(start, end):
 
92
            elem = [e for e in cache.values() if e.id >= start and e.id <= end]
 
93
            if len(elem) == 0:
 
94
                return 0
 
95
            avg = sum([e.regets for e in elem]) / len(elem)
 
96
            return avg
 
97
 
 
98
        hotzone_avg = average_regets_in_range(30, 40)
 
99
        control_avg = average_regets_in_range(450,760)
 
100
        total_avg = average_regets_in_range(0, 2000)
 
101
        
 
102
        # hotzone should be way above the others
 
103
        print "total fetches", total[0], "hotzone", hotzone_avg, "control", control_avg, "total", total_avg
 
104
        
 
105
        assert hotzone_avg > total_avg * 5 > control_avg * 5
 
106
        
 
107
        
 
108
if __name__ == "__main__":
 
109
    unittest.main()