summaryrefslogtreecommitdiff
path: root/test/test_lru.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-05-29 18:18:04 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2019-05-31 14:03:02 -0400
commitfb7f0437323ba836c68947d38f3604c3336e3a9b (patch)
tree756512a2b68f1c7270f028b487228fd0e3f13778 /test/test_lru.py
parentdb498f217e03d772e0c0c37a526226d48ed790e2 (diff)
downloadmako-fb7f0437323ba836c68947d38f3604c3336e3a9b.tar.gz
Use tox / zimports
Change-Id: Ia047c7052a6d242c2cf1c7a83981f1e38ea4d928
Diffstat (limited to 'test/test_lru.py')
-rw-r--r--test/test_lru.py97
1 files changed, 12 insertions, 85 deletions
diff --git a/test/test_lru.py b/test/test_lru.py
index 6152799..7281537 100644
--- a/test/test_lru.py
+++ b/test/test_lru.py
@@ -1,34 +1,30 @@
-from mako.util import LRUCache
-import string
import unittest
-import time
-import random
-from mako.compat import thread
+from mako.util import LRUCache
+
class item:
- def __init__(self, id):
- self.id = id
+ def __init__(self, id_):
+ self.id = id_
def __str__(self):
return "item id %d" % self.id
-class LRUTest(unittest.TestCase):
-
+class LRUTest(unittest.TestCase):
def testlru(self):
- l = LRUCache(10, threshold=.2)
+ l = LRUCache(10, threshold=0.2)
- for id in range(1,20):
- l[id] = item(id)
+ for id_ in range(1, 20):
+ l[id_] = item(id_)
# first couple of items should be gone
assert 1 not in l
assert 2 not in l
# next batch over the threshold of 10 should be present
- for id in range(11,20):
- assert id in l
+ for id_ in range(11, 20):
+ assert id_ in l
l[12]
l[15]
@@ -41,74 +37,5 @@ class LRUTest(unittest.TestCase):
assert 11 not in l
assert 13 not in l
- for id in (25, 24, 23, 14, 12, 19, 18, 17, 16, 15):
- assert id in l
-
- def _disabled_test_threaded(self):
- size = 100
- threshold = .5
- all_elems = 2000
- 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():
- if random.randint(1,4) == 1:
- return hot_zone[random.randint(0, len(hot_zone) - 1)]
- else:
- return random.randint(1, all_elems)
-
- total = [0]
- # request thread.
- def request_elem():
- while True:
- total[0] += 1
- id = get_elem()
- try:
- elem = cache[id]
- elem.regets += 1
- 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, ())
-
- # assert size doesn't grow unbounded, doesnt shrink well below size
- for x in range(0,5):
- time.sleep(1)
- 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 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)
- return avg
-
- 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", \
- hotzone_avg, "control", \
- control_avg, "total", total_avg)
-
- assert hotzone_avg > total_avg * 5 > control_avg * 5
-
-
-
+ for id_ in (25, 24, 23, 14, 12, 19, 18, 17, 16, 15):
+ assert id_ in l