~ubuntu-branches/ubuntu/utopic/exabgp/utopic

« back to all changes in this revision

Viewing changes to lib/exabgp/structure/cache.py

  • Committer: Package Import Robot
  • Author(s): Henry-Nicolas Tourneur
  • Date: 2014-03-08 19:07:00 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20140308190700-xjbibpg1g6001c9x
Tags: 3.3.1-1
* New upstream release
* Bump python minimal required version (2.7)
* Closes: #726066 Debian packaging improvements proposed by Vincent Bernat
* Closes: #703774 not existent rundir (/var/run/exabgp) after reboot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# encoding: utf-8
2
 
"""
3
 
cache.py
4
 
 
5
 
Created by David Farrar on 2012-12-27.
6
 
Copyright (c) 2009-2013 Exa Networks. All rights reserved.
7
 
"""
8
 
 
9
 
 
10
 
import time
11
 
 
12
 
class Cache (dict):
13
 
        def __init__ (self, min_items=10, max_items=2000, cache_life=3600):
14
 
                dict.__init__(self)
15
 
                self.ordered = []
16
 
                self.min_items = min_items
17
 
                self.max_items = max_items
18
 
                self.cache_life = cache_life
19
 
                self.last_accessed = int(time.time())
20
 
 
21
 
        def cache (self, key, value):
22
 
                now = int(time.time())
23
 
 
24
 
                if now - self.last_accessed >= self.cache_life:
25
 
                        self.truncate(self.min_items)
26
 
 
27
 
                elif len(self) >= self.max_items:
28
 
                        self.truncate(self.max_items/2)
29
 
 
30
 
                if key not in self:
31
 
                        self.ordered.append(key)
32
 
 
33
 
                self.last_accessed = now
34
 
                self[key] = value
35
 
 
36
 
                return value
37
 
 
38
 
        def retrieve (self, key):
39
 
                now = int(time.time())
40
 
                res = self[key]
41
 
 
42
 
                if now - self.last_accessed >= self.cache_life:
43
 
                        self.truncate(self.min_items)
44
 
 
45
 
                        # only update the access time if we modified the cache
46
 
                        self.last_accessed = now
47
 
 
48
 
                return res
49
 
 
50
 
        def truncate (self, pos):
51
 
                pos = len(self.ordered) - pos
52
 
                expiring = self.ordered[:pos]
53
 
                self.ordered = self.ordered[pos:]
54
 
 
55
 
                for _key in expiring:
56
 
                        self.pop(_key)
57
 
 
58
 
if __name__ == '__main__':
59
 
        class klass1:
60
 
                def __init__ (self, data):
61
 
                        pass
62
 
 
63
 
        class klass2 (object):
64
 
                def __init__ (self, data):
65
 
                        pass
66
 
 
67
 
        class klass3:
68
 
                def __init__ (self, data):
69
 
                        self.a = data[0]
70
 
                        self.b = data[1]
71
 
                        self.c = data[2]
72
 
                        self.d = data[3]
73
 
                        self.e = data[4]
74
 
 
75
 
        class klass4:
76
 
                def __init__ (self, data):
77
 
                        self.a = data[0]
78
 
                        self.b = data[1]
79
 
                        self.c = data[2]
80
 
                        self.d = data[3]
81
 
                        self.e = data[4]
82
 
 
83
 
        class _kparent1:
84
 
                def __init__ (self, data):
85
 
                        self.a = data[0]
86
 
                        self.b = data[1]
87
 
 
88
 
        class _kparent2 (object):
89
 
                def __init__ (self, data):
90
 
                        self.a = data[0]
91
 
                        self.b = data[1]
92
 
 
93
 
        class klass5 (_kparent1):
94
 
                def __init__ (self, data):
95
 
                        _kparent1.__init__(self,data)
96
 
                        self.c = data[2]
97
 
                        self.d = data[3]
98
 
                        self.e = data[4]
99
 
 
100
 
        class klass6 (_kparent2):
101
 
                def __init__ (self, data):
102
 
                        _kparent2.__init__(self,data)
103
 
                        self.c = data[2]
104
 
                        self.d = data[3]
105
 
                        self.e = data[4]
106
 
 
107
 
        class klass7 (klass6):
108
 
                pass
109
 
 
110
 
        class klass8 (klass6):
111
 
                def __init__ (self, data):
112
 
                        klass6.__init__(self,data)
113
 
                        self.s = self.a + self.b + self.c + self.d + self.e
114
 
 
115
 
        class klass9 (klass6):
116
 
                def __init__ (self, data):
117
 
                        klass6.__init__(self,data)
118
 
                        self.s1 = self.a + self.b + self.c + self.d + self.e
119
 
                        self.s2 = self.b + self.c + self.d + self.e
120
 
                        self.s3 = self.c + self.d + self.e
121
 
                        self.s4 = self.d + self.e
122
 
                        self.s5 = self.a + self.b + self.c + self.d
123
 
                        self.s6 = self.a + self.b + self.c
124
 
                        self.s7 = self.a + self.b
125
 
 
126
 
        COUNT = 100000
127
 
        UNIQUE = 5000
128
 
 
129
 
        samples = set()
130
 
        chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:"|;<>?,./[]{}-=_+!@£$%^&*()'
131
 
 
132
 
        from random import choice
133
 
 
134
 
        while len(samples) != UNIQUE:
135
 
                samples.add(choice(chars)+choice(chars)+choice(chars)+choice(chars)+choice(chars))
136
 
 
137
 
        samples = list(samples)
138
 
 
139
 
        for klass in [klass1,klass2,klass3,klass4,klass5,klass6,klass7,klass8,klass9]:
140
 
                cache = {}
141
 
 
142
 
                start = time.time()
143
 
                for val in xrange(COUNT):
144
 
                        val = val % UNIQUE
145
 
                        _ = klass(samples[val])
146
 
                end = time.time()
147
 
                time1 = end-start
148
 
 
149
 
                print COUNT,'iterations of',klass.__name__,'with',UNIQUE,'uniques classes'
150
 
                print "time instance %d" % time1
151
 
 
152
 
                cache = Cache()
153
 
                start = time.time()
154
 
                for val in xrange(COUNT):
155
 
                        val = val % UNIQUE
156
 
 
157
 
                        if val in cache:
158
 
                                _ = cache.retrieve(val)
159
 
                        else:
160
 
                                _ = cache.cache(val, klass(samples[val]))
161
 
 
162
 
                end = time.time()
163
 
                time2 = end-start
164
 
 
165
 
                print "time cached  %d" % time2
166
 
                print "speedup %.3f" % (time1/time2)
167
 
                print