-
Notifications
You must be signed in to change notification settings - Fork 44
/
test_cache.py
60 lines (44 loc) · 1.33 KB
/
test_cache.py
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
from __future__ import print_function
import logging
import time
import pysparkling
class Manip(object):
def __init__(self):
self.count = 0
def trivial_manip_with_debug(self, e):
self.count += 1
print('manipulating {0}'.format(e))
return e
def test_cache_empty_partition():
m = Manip()
c = pysparkling.Context()
rdd = c.parallelize(range(10), 2)
rdd = rdd.map(m.trivial_manip_with_debug)
rdd = rdd.filter(lambda e: e > 6).cache()
print(rdd.collect())
print(rdd.collect())
print('count of map executions: {}'.format(m.count))
assert m.count == 10
def test_timed_cache():
m = Manip()
# create a timed cache manager
cm = pysparkling.TimedCacheManager(timeout=1.0)
# create a cache entry
c = pysparkling.Context(cache_manager=cm)
rdd = c.parallelize(range(10), 2)
rdd = rdd.map(m.trivial_manip_with_debug).cache()
print(rdd.collect())
# make sure the cache is working
count_before = m.count
print(rdd.collect())
count_after = m.count
assert count_before == count_after
# wait to have the cache expire
time.sleep(1.5)
cm.gc()
print(rdd.collect())
assert m.count > count_after
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
# test_cache_empty_partition()
test_timed_cache()