aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_lru.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_lru.py')
-rw-r--r--test/test_lru.py61
1 files changed, 32 insertions, 29 deletions
diff --git a/test/test_lru.py b/test/test_lru.py
index ade48a3..cde5601 100644
--- a/test/test_lru.py
+++ b/test/test_lru.py
@@ -1,7 +1,10 @@
from mako.util import LRUCache
-import string, unittest, time, random
+import string
+import unittest
+import time
+import random
-import thread
+from mako.compat import thread
class item:
def __init__(self, id):
@@ -13,19 +16,19 @@ class item:
class LRUTest(unittest.TestCase):
- def testlru(self):
+ def testlru(self):
l = LRUCache(10, threshold=.2)
-
+
for id in range(1,20):
l[id] = item(id)
-
+
# first couple of items should be gone
- self.assert_(not l.has_key(1))
- self.assert_(not l.has_key(2))
-
+ self.assert_(1 not in l)
+ self.assert_(2 not in l)
+
# next batch over the threshold of 10 should be present
for id in range(11,20):
- self.assert_(l.has_key(id))
+ self.assert_(id in l)
l[12]
l[15]
@@ -35,25 +38,25 @@ class LRUTest(unittest.TestCase):
l[26] = item(26)
l[27] = item(27)
- self.assert_(not l.has_key(11))
- self.assert_(not l.has_key(13))
-
+ self.assert_(11 not in l)
+ self.assert_(13 not in l)
+
for id in (25, 24, 23, 14, 12, 19, 18, 17, 16, 15):
- self.assert_(l.has_key(id))
+ self.assert_(id in l)
def _disabled_test_threaded(self):
size = 100
threshold = .5
all_elems = 2000
- hot_zone = range(30,40)
+ hot_zone = list(range(30,40))
cache = LRUCache(size, threshold)
-
+
# element to store
class Element(object):
def __init__(self, id):
self.id = id
self.regets = 0
-
+
# return an element. we will favor ids in the relatively small
# "hot zone" 25% of the time.
def get_elem():
@@ -61,7 +64,7 @@ class LRUTest(unittest.TestCase):
return hot_zone[random.randint(0, len(hot_zone) - 1)]
else:
return random.randint(1, all_elems)
-
+
total = [0]
# request thread.
def request_elem():
@@ -74,23 +77,23 @@ class LRUTest(unittest.TestCase):
except KeyError:
e = Element(id)
cache[id] = e
-
+
time.sleep(random.random() / 1000)
for x in range(0,20):
- thread.start_new_thread(request_elem, ())
-
+ _thread.start_new_thread(request_elem, ())
+
# assert size doesn't grow unbounded, doesnt shrink well below size
for x in range(0,5):
time.sleep(1)
- print "size:", len(cache)
+ print("size:", len(cache))
assert len(cache) < size + size * threshold * 2
assert len(cache) > size - (size * .1)
-
+
# computs the average number of times a range of elements were "reused",
# i.e. without being removed from the cache.
def average_regets_in_range(start, end):
- elem = [e for e in cache.values() if e.id >= start and e.id <= end]
+ elem = [e for e in list(cache.values()) if e.id >= start and e.id <= end]
if len(elem) == 0:
return 0
avg = sum([e.regets for e in elem]) / len(elem)
@@ -99,13 +102,13 @@ class LRUTest(unittest.TestCase):
hotzone_avg = average_regets_in_range(30, 40)
control_avg = average_regets_in_range(450,760)
total_avg = average_regets_in_range(0, 2000)
-
+
# hotzone should be way above the others
- print "total fetches", total[0], "hotzone", \
+ print("total fetches", total[0], "hotzone", \
hotzone_avg, "control", \
- control_avg, "total", total_avg
-
+ control_avg, "total", total_avg)
+
assert hotzone_avg > total_avg * 5 > control_avg * 5
-
-
+
+