1 | |
---|
2 | import unittest |
---|
3 | from caching import * |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | # Define a test function to be cached |
---|
8 | # |
---|
9 | def f(a,b,c,N,x=0,y='abcdefg'): |
---|
10 | """f(a,b,c,N) |
---|
11 | Do something time consuming and produce a complex result. |
---|
12 | """ |
---|
13 | |
---|
14 | import string |
---|
15 | |
---|
16 | B = [] |
---|
17 | for n in range(N): |
---|
18 | s = str(n+2.0/(n + 4.0))+'.a'*10 |
---|
19 | B.append((a,b,c,s,n,x,y)) |
---|
20 | return(B) |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | |
---|
27 | |
---|
28 | class Test_Caching(unittest.TestCase): |
---|
29 | def setUp(self): |
---|
30 | set_option('verbose', 0) #Why |
---|
31 | |
---|
32 | pass |
---|
33 | |
---|
34 | def tearDown(self): |
---|
35 | pass |
---|
36 | |
---|
37 | def test_simple(self): |
---|
38 | """Check set_option (and switch stats off) |
---|
39 | """ |
---|
40 | |
---|
41 | set_option('savestat', 0) |
---|
42 | assert options['savestat'] == 0 |
---|
43 | set_option('verbose', 0) |
---|
44 | assert options['verbose'] == 0 |
---|
45 | |
---|
46 | |
---|
47 | def test_basic_caching(self): |
---|
48 | |
---|
49 | |
---|
50 | # Make some test input arguments |
---|
51 | # |
---|
52 | N = 5000 #Make N fairly small here |
---|
53 | |
---|
54 | a = [1,2] |
---|
55 | b = ('Thou shalt count the number three',4) |
---|
56 | c = {'Five is right out': 6, (7,8): 9} |
---|
57 | x = 3 |
---|
58 | y = 'holy hand granate' |
---|
59 | |
---|
60 | # Test caching |
---|
61 | # |
---|
62 | |
---|
63 | comprange = 2 |
---|
64 | |
---|
65 | for comp in range(comprange): |
---|
66 | |
---|
67 | # Evaluate and store |
---|
68 | # |
---|
69 | T1 = cache(f, (a,b,c,N), {'x':x, 'y':y}, evaluate=1, \ |
---|
70 | compression=comp) |
---|
71 | |
---|
72 | # Retrieve |
---|
73 | # |
---|
74 | T2 = cache(f, (a,b,c,N), {'x':x, 'y':y}, compression=comp) |
---|
75 | |
---|
76 | # Reference result |
---|
77 | # |
---|
78 | T3 = f(a,b,c,N,x=x,y=y) # Compute without caching |
---|
79 | |
---|
80 | |
---|
81 | assert T1 == T2, 'Cached result does not match computed result' |
---|
82 | assert T2 == T3, 'Cached result does not match computed result' |
---|
83 | |
---|
84 | |
---|
85 | def test_cachefiles(self): |
---|
86 | """Test existence of cachefiles |
---|
87 | """ |
---|
88 | N = 5000 #Make N fairly small here |
---|
89 | |
---|
90 | a = [1,2] |
---|
91 | b = ('Thou shalt count the number three',4) |
---|
92 | c = {'Five is right out': 6, (7,8): 9} |
---|
93 | x = 3 |
---|
94 | y = 'holy hand granate' |
---|
95 | |
---|
96 | |
---|
97 | FN = cache(f,(a,b,c,N), {'x':x, 'y':y}, verbose=0, \ |
---|
98 | return_filename = 1) |
---|
99 | |
---|
100 | |
---|
101 | assert FN[:2] == 'f[' |
---|
102 | |
---|
103 | CD = checkdir(cachedir) |
---|
104 | compression = 1 |
---|
105 | |
---|
106 | (datafile,compressed0) = myopen(CD+FN+'_'+file_types[0],"rb",compression) |
---|
107 | (argsfile,compressed1) = myopen(CD+FN+'_'+file_types[1],"rb",compression) |
---|
108 | (admfile,compressed2) = myopen(CD+FN+'_'+file_types[2],"rb",compression) |
---|
109 | |
---|
110 | datafile.close() |
---|
111 | argsfile.close() |
---|
112 | admfile.close() |
---|
113 | |
---|
114 | def test_test(self): |
---|
115 | """Test 'test' function when cache is present |
---|
116 | """ |
---|
117 | N = 5000 #Make N fairly small here |
---|
118 | |
---|
119 | a = [1,2] |
---|
120 | b = ('Thou shalt count the number three',4) |
---|
121 | c = {'Five is right out': 6, (7,8): 9} |
---|
122 | x = 3 |
---|
123 | y = 'holy hand granate' |
---|
124 | |
---|
125 | |
---|
126 | T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}, evaluate=1) |
---|
127 | |
---|
128 | T4 = cache(f,(a,b,c,N), {'x':x, 'y':y}, test=1) |
---|
129 | assert T1 == T4, "Option 'test' when cache file present failed" |
---|
130 | |
---|
131 | |
---|
132 | def test_clear(self): |
---|
133 | """Test that 'clear' works |
---|
134 | """ |
---|
135 | N = 5000 #Make N fairly small here |
---|
136 | |
---|
137 | a = [1,2] |
---|
138 | b = ('Thou shalt count the number three',4) |
---|
139 | c = {'Five is right out': 6, (7,8): 9} |
---|
140 | x = 3 |
---|
141 | y = 'holy hand granate' |
---|
142 | |
---|
143 | |
---|
144 | T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}, evaluate = 1) |
---|
145 | |
---|
146 | |
---|
147 | cache(f,(a,b,c,N), {'x':x, 'y':y}, clear = 1) |
---|
148 | |
---|
149 | |
---|
150 | # Test 'test' function when cache is absent |
---|
151 | |
---|
152 | |
---|
153 | T4 = cache(f,(a,b,c,N), {'x':x, 'y':y}, test=1) |
---|
154 | assert T4 is None, "Option 'test' when cache absent failed" |
---|
155 | |
---|
156 | |
---|
157 | def test_dependencies(self): |
---|
158 | |
---|
159 | # Make a dependency file |
---|
160 | CD = checkdir(cachedir) |
---|
161 | |
---|
162 | DepFN = CD + 'testfile.tmp' |
---|
163 | DepFN_wildcard = CD + 'test*.tmp' |
---|
164 | Depfile = open(DepFN,'w') |
---|
165 | Depfile.write('We are the knights who say NI!') |
---|
166 | Depfile.close() |
---|
167 | |
---|
168 | |
---|
169 | # Test |
---|
170 | # |
---|
171 | |
---|
172 | N = 5000 #Make N fairly small here |
---|
173 | |
---|
174 | a = [1,2] |
---|
175 | b = ('Thou shalt count the number three',4) |
---|
176 | c = {'Five is right out': 6, (7,8): 9} |
---|
177 | x = 3 |
---|
178 | y = 'holy hand granate' |
---|
179 | |
---|
180 | T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN) |
---|
181 | T2 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN) |
---|
182 | |
---|
183 | assert T1 == T2, 'Dependencies do not work' |
---|
184 | |
---|
185 | |
---|
186 | # Test basic wildcard dependency |
---|
187 | T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN_wildcard) |
---|
188 | |
---|
189 | assert T1 == T3, 'Dependencies with wildcards do not work' |
---|
190 | |
---|
191 | |
---|
192 | # Test that changed timestamp in dependencies triggers recomputation |
---|
193 | |
---|
194 | # Modify dependency file |
---|
195 | Depfile = open(DepFN,'a') |
---|
196 | Depfile.write('You must cut down the mightiest tree in the forest with a Herring') |
---|
197 | Depfile.close() |
---|
198 | |
---|
199 | T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN, test = 1) |
---|
200 | |
---|
201 | assert T3 is None, 'Changed dependencies not recognised' |
---|
202 | |
---|
203 | # Test recomputation when dependencies have changed |
---|
204 | # |
---|
205 | T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN) |
---|
206 | assert T1 == T3, 'Recomputed value with changed dependencies failed' |
---|
207 | |
---|
208 | def test_performance(self): |
---|
209 | """Performance test (with statistics) |
---|
210 | Don't really rely on this as it will depend on specific computer. |
---|
211 | """ |
---|
212 | import time |
---|
213 | set_option('savestat', 1) |
---|
214 | |
---|
215 | N = 300000 #Should be large on fast computers... |
---|
216 | a = [1,2] |
---|
217 | b = ('Thou shalt count the number three',4) |
---|
218 | c = {'Five is right out': 6, (7,8): 9} |
---|
219 | x = 3 |
---|
220 | y = 'holy hand granate' |
---|
221 | |
---|
222 | |
---|
223 | tt = time.time() |
---|
224 | T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}) |
---|
225 | t1 = time.time() - tt |
---|
226 | |
---|
227 | tt = time.time() |
---|
228 | T2 = cache(f,(a,b,c,N), {'x':x, 'y':y}) |
---|
229 | t2 = time.time() - tt |
---|
230 | |
---|
231 | assert T1 == T2 |
---|
232 | assert t1 > t2, 'WARNING: Performance a bit low - this could be specific to current platform. Try to increase N in this test' |
---|
233 | #test_OK('Performance test: relative time saved = %s pct' \ |
---|
234 | # %str(round((t1-t2)*100/t1,2))) |
---|
235 | |
---|
236 | |
---|
237 | def test_statsfile(self): |
---|
238 | """Test presence of statistics file |
---|
239 | """ |
---|
240 | import os, string |
---|
241 | statsfile = '.cache_stat' # Basefilename for cached statistics. |
---|
242 | |
---|
243 | CD = checkdir(cachedir) |
---|
244 | DIRLIST = os.listdir(CD) |
---|
245 | SF = [] |
---|
246 | for FN in DIRLIST: |
---|
247 | if string.find(FN,statsfile) >= 0: |
---|
248 | try: |
---|
249 | fid = open(CD+FN,'r') |
---|
250 | fid.close() |
---|
251 | except: |
---|
252 | raise 'Statistics files cannot be opened' |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | # def test_network_cachedir(self): |
---|
257 | |
---|
258 | # #set_option('cachedir', 'H:\\.python_cache\\') |
---|
259 | # set_option('cachedir', 'V:\\2\\cit\\.python_cache\\') |
---|
260 | # set_option('verbose', 1) |
---|
261 | |
---|
262 | |
---|
263 | # # Make some test input arguments |
---|
264 | # # |
---|
265 | # N = 5000 #Make N fairly small here |
---|
266 | |
---|
267 | # a = [1,2] |
---|
268 | # b = ('Thou shalt count the number three',4) |
---|
269 | # c = {'Five is right out': 6, (7,8): 9} |
---|
270 | # x = 3 |
---|
271 | # y = 'holy hand granate' |
---|
272 | |
---|
273 | # # Test caching |
---|
274 | # # |
---|
275 | |
---|
276 | # comprange = 2 |
---|
277 | |
---|
278 | # for comp in range(comprange): |
---|
279 | |
---|
280 | # # Evaluate and store |
---|
281 | # # |
---|
282 | # T1 = cache(f, (a,b,c,N), {'x':x, 'y':y}, evaluate=1, \ |
---|
283 | # compression=comp) |
---|
284 | |
---|
285 | # # Retrieve |
---|
286 | # # |
---|
287 | # T2 = cache(f, (a,b,c,N), {'x':x, 'y':y}, compression=comp) |
---|
288 | |
---|
289 | # # Reference result |
---|
290 | # # |
---|
291 | # T3 = f(a,b,c,N,x=x,y=y) # Compute without caching |
---|
292 | |
---|
293 | |
---|
294 | # assert T1 == T2, 'Cached result does not match computed result' |
---|
295 | # assert T2 == T3, 'Cached result does not match computed result' |
---|
296 | |
---|
297 | |
---|
298 | |
---|
299 | |
---|
300 | |
---|
301 | #------------------------------------------------------------- |
---|
302 | if __name__ == "__main__": |
---|
303 | suite = unittest.makeSuite(Test_Caching,'test') |
---|
304 | runner = unittest.TextTestRunner() |
---|
305 | runner.run(suite) |
---|