1 | |
---|
2 | import unittest |
---|
3 | |
---|
4 | from copy import deepcopy |
---|
5 | |
---|
6 | from anuga.caching import * |
---|
7 | from anuga.caching.dummy_classes_for_testing import Dummy, Dummy_memorytest |
---|
8 | |
---|
9 | import numpy as num |
---|
10 | |
---|
11 | |
---|
12 | # Define a test function to be cached |
---|
13 | # |
---|
14 | def f(a,b,c,N,x=0,y='abcdefg'): |
---|
15 | """f(a,b,c,N) |
---|
16 | Do something time consuming and produce a complex result. |
---|
17 | """ |
---|
18 | |
---|
19 | import string |
---|
20 | |
---|
21 | B = [] |
---|
22 | for n in range(N): |
---|
23 | s = str(n+2.0/(n + 4.0))+'.a'*10 |
---|
24 | B.append((a,b,c,s,n,x,y)) |
---|
25 | return(B) |
---|
26 | |
---|
27 | def f_numeric(A, B): |
---|
28 | """Operation on numeric arrays |
---|
29 | """ |
---|
30 | |
---|
31 | return 3.1*A + B + 1 |
---|
32 | |
---|
33 | |
---|
34 | def f_object(A, B): |
---|
35 | """Operation of objecs of class Dummy |
---|
36 | """ |
---|
37 | |
---|
38 | return A.value+B.value, A.another+B.another |
---|
39 | |
---|
40 | |
---|
41 | def f_generic(A): |
---|
42 | return A |
---|
43 | |
---|
44 | def clear_and_create_cache(Dummy, verbose=False): |
---|
45 | |
---|
46 | a = cache(Dummy, 'clear', verbose=verbose) |
---|
47 | a = cache(Dummy, args=(9,10), |
---|
48 | verbose=verbose) |
---|
49 | |
---|
50 | |
---|
51 | def retrieve_cache(Dummy, verbose=False): |
---|
52 | if verbose: print('Check that cache is there') |
---|
53 | |
---|
54 | X = cache(Dummy, args=(9,10), test=1, |
---|
55 | verbose=verbose) |
---|
56 | |
---|
57 | msg = 'Cached value was not found' |
---|
58 | assert X is not None, msg |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | class Test_Caching(unittest.TestCase): |
---|
65 | def setUp(self): |
---|
66 | set_option('verbose', 0) #Why |
---|
67 | |
---|
68 | pass |
---|
69 | |
---|
70 | def tearDown(self): |
---|
71 | pass |
---|
72 | |
---|
73 | def test_simple(self): |
---|
74 | """Check set_option (and switch stats off) |
---|
75 | """ |
---|
76 | |
---|
77 | set_option('savestat', 0) |
---|
78 | assert options['savestat'] == 0 |
---|
79 | set_option('verbose', 0) |
---|
80 | assert options['verbose'] == 0 |
---|
81 | |
---|
82 | |
---|
83 | def test_basic_caching(self): |
---|
84 | |
---|
85 | verbose=False |
---|
86 | # Make some test input arguments |
---|
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 | # Test caching |
---|
97 | # |
---|
98 | |
---|
99 | comprange = 2 |
---|
100 | |
---|
101 | for comp in range(comprange): |
---|
102 | |
---|
103 | # Evaluate and store |
---|
104 | # |
---|
105 | T1 = cache(f, (a,b,c,N), {'x':x, 'y':y}, evaluate=1, \ |
---|
106 | compression=comp, verbose=verbose) |
---|
107 | |
---|
108 | # Retrieve |
---|
109 | # |
---|
110 | T2 = cache(f, (a,b,c,N), {'x':x, 'y':y}, compression=comp) |
---|
111 | |
---|
112 | # Reference result |
---|
113 | # |
---|
114 | T3 = f(a,b,c,N,x=x,y=y) # Compute without caching |
---|
115 | |
---|
116 | |
---|
117 | assert T1 == T2, 'Cached result does not match computed result' |
---|
118 | assert T2 == T3, 'Cached result does not match computed result' |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | def test_caching_of_numeric_arrays(self): |
---|
123 | """test_caching_of_numeric_arrays |
---|
124 | |
---|
125 | Test that numeric arrays can be recognised by caching even if their id's are different |
---|
126 | """ |
---|
127 | |
---|
128 | verbose = False |
---|
129 | |
---|
130 | # Make some test input arguments |
---|
131 | A0 = num.arange(5) |
---|
132 | B0 = num.array([1.1, 2.2, 0.0, -5, -5]) |
---|
133 | |
---|
134 | A1 = A0.copy() |
---|
135 | B1 = B0.copy() |
---|
136 | |
---|
137 | # Check that their ids are different |
---|
138 | assert id(A0) != id(A1) |
---|
139 | assert id(B0) != id(B1) |
---|
140 | |
---|
141 | |
---|
142 | # Test caching |
---|
143 | comprange = 2 |
---|
144 | for comp in range(comprange): |
---|
145 | |
---|
146 | # Evaluate and store |
---|
147 | T1 = cache(f_numeric, (A0, B0), evaluate=1, |
---|
148 | compression=comp, verbose=verbose) |
---|
149 | |
---|
150 | # Retrieve |
---|
151 | T2 = cache(f_numeric, (A1, B1), |
---|
152 | compression=comp, test=1, verbose=verbose) |
---|
153 | |
---|
154 | # Check for presence of cached result |
---|
155 | msg = 'Different array objects with same contents were not recognised' |
---|
156 | assert T2 is not None, msg |
---|
157 | |
---|
158 | # Reference result |
---|
159 | T3 = f_numeric(A0, B0) # Compute without caching |
---|
160 | |
---|
161 | |
---|
162 | assert num.alltrue(T1 == T2), 'Cached result does not match computed result' |
---|
163 | assert num.alltrue(T2 == T3), 'Cached result does not match computed result' |
---|
164 | |
---|
165 | |
---|
166 | def test_hash_collision(self): |
---|
167 | """Test that hash collisons are dealt with correctly""" |
---|
168 | |
---|
169 | verbose = False |
---|
170 | |
---|
171 | # Make test input arguments |
---|
172 | A0 = num.arange(5)*1.0 |
---|
173 | B = ('x', 15) |
---|
174 | |
---|
175 | # Create different A that hashes to the same address (having the same average) |
---|
176 | A1 = num.array([2.0, 2.0, 2.0, 2.0, 2.0]) |
---|
177 | |
---|
178 | assert myhash(A0) == myhash(A1) |
---|
179 | |
---|
180 | |
---|
181 | # Test caching |
---|
182 | comprange = 2 |
---|
183 | for comp in range(comprange): |
---|
184 | |
---|
185 | # Clear |
---|
186 | cache(f_numeric, (A0, A0), clear=1, |
---|
187 | compression=comp, verbose=verbose) |
---|
188 | cache(f_numeric, (A1, A1), clear=1, |
---|
189 | compression=comp, verbose=verbose) |
---|
190 | |
---|
191 | |
---|
192 | # Evaluate and store |
---|
193 | T1 = cache(f_numeric, (A0, A0), evaluate=1, |
---|
194 | compression=comp, verbose=verbose) |
---|
195 | |
---|
196 | |
---|
197 | # Check that A1 doesn't trigger retrieval of the previous result |
---|
198 | # even though it hashes to the same address |
---|
199 | T2 = cache(f_numeric, (A1, A1), |
---|
200 | compression=comp, verbose=verbose) |
---|
201 | |
---|
202 | T1_ref = f_numeric(A0, A0) |
---|
203 | T2_ref = f_numeric(A1, A1) |
---|
204 | T1_ref = f_numeric(A0, A0) |
---|
205 | T2_ref = f_numeric(A1, A1) |
---|
206 | |
---|
207 | assert num.alltrue(T1 == T1_ref) |
---|
208 | assert num.alltrue(T2 == T2_ref) |
---|
209 | |
---|
210 | |
---|
211 | def test_caching_of_dictionaries(self): |
---|
212 | """test_caching_of_dictionaries |
---|
213 | |
---|
214 | Real example from ANUGA that caused some |
---|
215 | hashing problems |
---|
216 | """ |
---|
217 | |
---|
218 | |
---|
219 | verbose = False #True |
---|
220 | |
---|
221 | D = {'point_attributes': None, |
---|
222 | 'use_cache': True, |
---|
223 | 'vertex_coordinates': None, |
---|
224 | 'verbose': True, |
---|
225 | 'max_read_lines': 500, |
---|
226 | 'acceptable_overshoot': 1.01, |
---|
227 | 'mesh': None, |
---|
228 | 'data_origin': None, |
---|
229 | 'alpha': 0.02, |
---|
230 | 'mesh_origin': None, |
---|
231 | 'attribute_name': None, |
---|
232 | 'triangles': None} |
---|
233 | |
---|
234 | DD = deepcopy(D) # Mangles the dictionary ordering |
---|
235 | |
---|
236 | assert myhash(DD) == myhash(D) |
---|
237 | |
---|
238 | # Also test caching now that we are at it |
---|
239 | comprange = 2 |
---|
240 | for comp in range(comprange): |
---|
241 | |
---|
242 | # Evaluate and store using D |
---|
243 | T1 = cache(f_generic, D, evaluate=1, |
---|
244 | compression=comp, verbose=verbose) |
---|
245 | |
---|
246 | # Retrieve using copy (DD) |
---|
247 | T2 = cache(f_generic, DD, |
---|
248 | compression=comp, test=1, verbose=verbose) |
---|
249 | |
---|
250 | # Check for presence of cached result |
---|
251 | msg = 'Cached object was not found' |
---|
252 | assert T2 is not None, msg |
---|
253 | |
---|
254 | # Reference result |
---|
255 | T3 = f_generic(D) # Compute without caching |
---|
256 | |
---|
257 | |
---|
258 | msg = 'Cached result does not match computed result' |
---|
259 | |
---|
260 | # Compare dictionaries |
---|
261 | for key in T1: |
---|
262 | assert T1[key] == T2[key] |
---|
263 | assert T2[key] == T3[key] |
---|
264 | |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | |
---|
269 | def test_caching_of_objects(self): |
---|
270 | """test_caching_of_objects |
---|
271 | |
---|
272 | Test that Objecs can be recognised as input variabelse |
---|
273 | by caching even if their id's are different |
---|
274 | """ |
---|
275 | |
---|
276 | |
---|
277 | verbose = False |
---|
278 | |
---|
279 | # Make some test input arguments |
---|
280 | A0 = Dummy(5, 7) |
---|
281 | B0 = Dummy(2.2, -5) |
---|
282 | |
---|
283 | A0.new_attribute = 'x' |
---|
284 | B0.new_attribute = 'x' |
---|
285 | |
---|
286 | A1 = deepcopy(A0) |
---|
287 | B1 = deepcopy(B0) |
---|
288 | |
---|
289 | # Check that their ids are different |
---|
290 | assert id(A0) != id(A1) |
---|
291 | assert id(B0) != id(B1) |
---|
292 | |
---|
293 | |
---|
294 | # Test caching |
---|
295 | comprange = 2 |
---|
296 | for comp in range(comprange): |
---|
297 | |
---|
298 | # Evaluate and store |
---|
299 | T1 = cache(f_object, (A0, B0), evaluate=1, |
---|
300 | compression=comp, verbose=verbose) |
---|
301 | |
---|
302 | # Retrieve |
---|
303 | T2 = cache(f_object, (A1, B1), |
---|
304 | compression=comp, |
---|
305 | test=1, |
---|
306 | verbose=verbose) |
---|
307 | |
---|
308 | # Check for presence of cached result |
---|
309 | msg = 'Different objects with same attributes were not recognised' |
---|
310 | assert T2 is not None, msg |
---|
311 | |
---|
312 | # Reference result |
---|
313 | T3 = f_object(A0, B0) # Compute without caching |
---|
314 | |
---|
315 | |
---|
316 | assert T1 == T2, 'Cached result does not match computed result' |
---|
317 | assert T2 == T3, 'Cached result does not match computed result' |
---|
318 | |
---|
319 | |
---|
320 | def test_caching_of_circular_structures(self): |
---|
321 | """test_caching_of_circular_structures |
---|
322 | |
---|
323 | Test that Caching doesn't recurse infinitely in case of |
---|
324 | circular or self-referencing structures |
---|
325 | """ |
---|
326 | |
---|
327 | verbose = False |
---|
328 | |
---|
329 | # Create input argument |
---|
330 | A = Dummy(5, 7) |
---|
331 | B = {'x': 10, 'A': A} |
---|
332 | C = [B, 15] |
---|
333 | A.value = C # Make it circular |
---|
334 | A.x = [1,2,C,5,A] # More circular and self referential |
---|
335 | |
---|
336 | AA = deepcopy(A) |
---|
337 | |
---|
338 | # Test caching |
---|
339 | comprange = 2 |
---|
340 | for comp in range(comprange): |
---|
341 | |
---|
342 | # Evaluate and store |
---|
343 | T1 = cache(f_generic, A, |
---|
344 | evaluate=1, |
---|
345 | compression=comp, verbose=verbose) |
---|
346 | |
---|
347 | # Retrieve |
---|
348 | T2 = cache(f_generic, AA, |
---|
349 | compression=comp, |
---|
350 | test=1, verbose=verbose) |
---|
351 | |
---|
352 | # Check for presence of cached result |
---|
353 | msg = 'Cached object was not found' |
---|
354 | assert T2 is not None, msg |
---|
355 | |
---|
356 | # Reference result |
---|
357 | T3 = f_generic(A) # Compute without caching |
---|
358 | |
---|
359 | |
---|
360 | msg = 'Cached result does not match computed result' |
---|
361 | assert str(T1) == str(T2), msg |
---|
362 | assert str(T2) == str(T3), msg |
---|
363 | |
---|
364 | |
---|
365 | def test_caching_of_callable_objects(self): |
---|
366 | """test_caching_of_callable_objects(self) |
---|
367 | |
---|
368 | Test that caching will discern between calls of |
---|
369 | two different instances of a callable object with the same input. |
---|
370 | |
---|
371 | This cause problems with add_quantity in ANUGA in changeset:6225 |
---|
372 | where a previous instance of Polygon_function was picked up. |
---|
373 | """ |
---|
374 | |
---|
375 | class call: |
---|
376 | |
---|
377 | def __init__(self, a, b): |
---|
378 | self.a = a |
---|
379 | self.b = b |
---|
380 | |
---|
381 | def __call__(self, x): |
---|
382 | return self.a*x + self.b |
---|
383 | |
---|
384 | |
---|
385 | f1 = call(2, 3) |
---|
386 | f2 = call(5, 7) |
---|
387 | |
---|
388 | # Check that hash value of callable objects don't change |
---|
389 | # FIXME (Ole): The hash values do appear to change when OS |
---|
390 | # and/or dependencies are upgraded |
---|
391 | if os.name == 'posix' and os.uname()[4] in ['x86_64', 'ia64']: |
---|
392 | # 64 bit hash values |
---|
393 | f1hash = 7079146893884768701 |
---|
394 | f2hash = -6995306676314913340 |
---|
395 | |
---|
396 | # Prior to cluster upgrades Feb 2009 |
---|
397 | #f1hash = 1914027059797211698 |
---|
398 | #f2hash = 1914027059807087171 |
---|
399 | else: |
---|
400 | # 32 bit hash values |
---|
401 | f1hash = -758136387 |
---|
402 | f2hash = -11221564 |
---|
403 | |
---|
404 | assert myhash(f1) == f1hash |
---|
405 | assert myhash(f2) == f2hash |
---|
406 | |
---|
407 | bc1 = get_bytecode(f1) |
---|
408 | bc2 = get_bytecode(f2) |
---|
409 | |
---|
410 | msg = 'Byte code should be different' |
---|
411 | assert bc1 != bc2, msg |
---|
412 | |
---|
413 | |
---|
414 | x = num.arange(10).astype(num.float) |
---|
415 | |
---|
416 | ref1 = f1(x) |
---|
417 | ref2 = f2(x) |
---|
418 | |
---|
419 | # Clear cache for f1(x) and f2(x) and verify that all is clear |
---|
420 | cache(f1, x, clear=True, verbose=False) |
---|
421 | flag = cache(f1, x, test=True, verbose=False) |
---|
422 | assert flag is None |
---|
423 | |
---|
424 | cache(f2, x, clear=True, verbose=False) |
---|
425 | flag = cache(f2, x, test=True, verbose=False) |
---|
426 | assert flag is None |
---|
427 | |
---|
428 | # Run f1(x) and cache |
---|
429 | res1 = cache(f1, x, verbose=False) |
---|
430 | assert num.allclose(res1, ref1) |
---|
431 | |
---|
432 | # Test that f1(x) has been cached correctly |
---|
433 | res1 = cache(f1, x, test=True, verbose=False) |
---|
434 | assert num.allclose(res1, ref1) |
---|
435 | |
---|
436 | # Test that f2(x) is still clear |
---|
437 | cache(f2, x, clear=True, verbose=False) |
---|
438 | flag = cache(f2, x, test=True, verbose=False) |
---|
439 | assert flag is None |
---|
440 | |
---|
441 | # Run f2(x) and test result |
---|
442 | res2 = cache(f2, x, verbose=False) |
---|
443 | msg = 'Wrong result for f2(x)' |
---|
444 | assert num.allclose(res2, ref2), msg |
---|
445 | |
---|
446 | |
---|
447 | |
---|
448 | |
---|
449 | def test_uniqueness_of_hash_values(self): |
---|
450 | """test_uniqueness_of_hash_values(self): |
---|
451 | |
---|
452 | Test that Caching can handle a realistic |
---|
453 | complex structure by hashing it consistently and |
---|
454 | uniquely. |
---|
455 | """ |
---|
456 | |
---|
457 | verbose = False |
---|
458 | |
---|
459 | # Create input argument |
---|
460 | A = Dummy(5, 7) |
---|
461 | B = {'x': 10, 'A': A} |
---|
462 | C = [B, num.array([1.2, 3, 5, 0.1])] |
---|
463 | A.value = C # Make it circular |
---|
464 | |
---|
465 | # Create identical but separate object |
---|
466 | AA = Dummy(None, None) |
---|
467 | BB = {'A': AA, 'x': 10} |
---|
468 | CC = [BB, num.array([1.200, 3.000, 5.00, 1.0/10])] |
---|
469 | AA.value = CC # Make it circular |
---|
470 | AA.another = 3+4 |
---|
471 | |
---|
472 | |
---|
473 | assert myhash(A) == myhash(AA) |
---|
474 | |
---|
475 | |
---|
476 | |
---|
477 | # Also test caching now that we are at it |
---|
478 | comprange = 2 |
---|
479 | for comp in range(comprange): |
---|
480 | |
---|
481 | # Evaluate and store using A |
---|
482 | T1 = cache(f_generic, A, evaluate=1, |
---|
483 | compression=comp, verbose=verbose) |
---|
484 | |
---|
485 | # Retrieve using copy (AA) |
---|
486 | T2 = cache(f_generic, AA, |
---|
487 | compression=comp, test=1, verbose=verbose) |
---|
488 | |
---|
489 | # Check for presence of cached result |
---|
490 | msg = 'Cached object was not found' |
---|
491 | assert T2 is not None, msg |
---|
492 | |
---|
493 | # Reference result |
---|
494 | T3 = f_generic(A) # Compute without caching |
---|
495 | |
---|
496 | |
---|
497 | msg = 'Cached result does not match computed result' |
---|
498 | assert str(T1) == str(T2), msg |
---|
499 | assert str(T2) == str(T3), msg |
---|
500 | |
---|
501 | |
---|
502 | |
---|
503 | def test_caching_of_simple_circular_dictionaries(self): |
---|
504 | """test_caching_of_circular_structures |
---|
505 | |
---|
506 | Test that Caching doesn't recurse infinitely in case of |
---|
507 | circular or self-referencing structures |
---|
508 | """ |
---|
509 | |
---|
510 | verbose = False #True |
---|
511 | |
---|
512 | # Create input argument |
---|
513 | A = {'x': 10, 'B': None} |
---|
514 | B = [A, 15] |
---|
515 | A['B'] = B # Make it circular |
---|
516 | |
---|
517 | # Test caching |
---|
518 | comprange = 2 |
---|
519 | for comp in range(comprange): |
---|
520 | |
---|
521 | # Evaluate and store |
---|
522 | T1 = cache(f_generic, A, evaluate=1, |
---|
523 | compression=comp, verbose=verbose) |
---|
524 | |
---|
525 | |
---|
526 | # Retrieve |
---|
527 | T2 = cache(f_generic, A, |
---|
528 | compression=comp, test=1, verbose=verbose) |
---|
529 | |
---|
530 | # Check for presence of cached result |
---|
531 | msg = 'Cached object was not found' |
---|
532 | assert T2 is not None, msg |
---|
533 | |
---|
534 | # Reference result |
---|
535 | T3 = f_generic(A) # Compute without caching |
---|
536 | |
---|
537 | |
---|
538 | msg = 'Cached result does not match computed result' |
---|
539 | assert str(T1) == str(T2), msg |
---|
540 | assert str(T2) == str(T3), msg |
---|
541 | |
---|
542 | |
---|
543 | |
---|
544 | def test_cachefiles(self): |
---|
545 | """Test existence of cachefiles |
---|
546 | """ |
---|
547 | N = 5000 #Make N fairly small here |
---|
548 | |
---|
549 | a = [1,2] |
---|
550 | b = ('Thou shalt count the number three',4) |
---|
551 | c = {'Five is right out': 6, (7,8): 9} |
---|
552 | x = 3 |
---|
553 | y = 'holy hand granate' |
---|
554 | |
---|
555 | |
---|
556 | FN = cache(f,(a,b,c,N), {'x':x, 'y':y}, verbose=0, \ |
---|
557 | return_filename = 1) |
---|
558 | |
---|
559 | |
---|
560 | assert FN[:2] == 'f[' |
---|
561 | |
---|
562 | CD = checkdir(cachedir) |
---|
563 | compression = 1 |
---|
564 | |
---|
565 | (datafile,compressed0) = myopen(CD+FN+'_'+file_types[0],"rb",compression) |
---|
566 | (argsfile,compressed1) = myopen(CD+FN+'_'+file_types[1],"rb",compression) |
---|
567 | (admfile,compressed2) = myopen(CD+FN+'_'+file_types[2],"rb",compression) |
---|
568 | |
---|
569 | datafile.close() |
---|
570 | argsfile.close() |
---|
571 | admfile.close() |
---|
572 | |
---|
573 | def test_test(self): |
---|
574 | """Test 'test' function when cache is present |
---|
575 | """ |
---|
576 | |
---|
577 | verbose = False |
---|
578 | |
---|
579 | N = 5 |
---|
580 | |
---|
581 | a = [1,2] |
---|
582 | b = ('Thou shalt count the number three',4) |
---|
583 | c = {'Five is right out': 6, (7,8): 9} |
---|
584 | x = 3 |
---|
585 | y = 'holy hand granate' |
---|
586 | |
---|
587 | |
---|
588 | T1 = cache(f, (a,b,c,N), {'x':x, 'y':y}, |
---|
589 | evaluate=1, |
---|
590 | verbose=verbose) |
---|
591 | |
---|
592 | T2 = cache(f, (a,b,c,N), {'x':x, 'y':y}, |
---|
593 | test=1, |
---|
594 | verbose=verbose) |
---|
595 | |
---|
596 | |
---|
597 | # Check for presence of cached result |
---|
598 | msg = 'Different objects with same attributes were not recognised' |
---|
599 | assert T2 is not None, msg |
---|
600 | |
---|
601 | assert T1 == T2, "Option 'test' when cache file present failed" |
---|
602 | |
---|
603 | |
---|
604 | def test_clear(self): |
---|
605 | """Test that 'clear' works |
---|
606 | """ |
---|
607 | |
---|
608 | N = 5000 #Make N fairly small here |
---|
609 | |
---|
610 | a = [1,2] |
---|
611 | b = ('Thou shalt count the number three',4) |
---|
612 | c = {'Five is right out': 6, (7,8): 9} |
---|
613 | x = 3 |
---|
614 | y = 'holy hand granate' |
---|
615 | |
---|
616 | |
---|
617 | T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}, evaluate = 1) |
---|
618 | |
---|
619 | |
---|
620 | cache(f, (a,b,c,N), {'x':x, 'y':y}, clear = 1) |
---|
621 | |
---|
622 | |
---|
623 | # Test 'test' function when cache is absent |
---|
624 | |
---|
625 | |
---|
626 | T4 = cache(f, (a,b,c,N), {'x':x, 'y':y}, test=1) |
---|
627 | assert T4 is None, "Option 'test' when cache absent failed" |
---|
628 | |
---|
629 | |
---|
630 | def test_dependencies(self): |
---|
631 | |
---|
632 | # Make a dependency file |
---|
633 | CD = checkdir(cachedir) |
---|
634 | |
---|
635 | DepFN = CD + 'testfile.tmp' |
---|
636 | DepFN_wildcard = CD + 'test*.tmp' |
---|
637 | Depfile = open(DepFN,'w') |
---|
638 | Depfile.write('We are the knights who say NI!') |
---|
639 | Depfile.close() |
---|
640 | |
---|
641 | |
---|
642 | # Test |
---|
643 | # |
---|
644 | |
---|
645 | N = 5000 #Make N fairly small here |
---|
646 | |
---|
647 | a = [1,2] |
---|
648 | b = ('Thou shalt count the number three',4) |
---|
649 | c = {'Five is right out': 6, (7,8): 9} |
---|
650 | x = 3 |
---|
651 | y = 'holy hand granate' |
---|
652 | |
---|
653 | T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN) |
---|
654 | T2 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN) |
---|
655 | |
---|
656 | assert T1 == T2, 'Dependencies do not work' |
---|
657 | |
---|
658 | |
---|
659 | # Test basic wildcard dependency |
---|
660 | T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN_wildcard) |
---|
661 | |
---|
662 | assert T1 == T3, 'Dependencies with wildcards do not work' |
---|
663 | |
---|
664 | |
---|
665 | # Test that changed timestamp in dependencies triggers recomputation |
---|
666 | |
---|
667 | # Modify dependency file |
---|
668 | Depfile = open(DepFN,'a') |
---|
669 | Depfile.write('You must cut down the mightiest tree in the forest with a Herring') |
---|
670 | Depfile.close() |
---|
671 | |
---|
672 | T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN, test = 1) |
---|
673 | |
---|
674 | assert T3 is None, 'Changed dependencies not recognised' |
---|
675 | |
---|
676 | # Test recomputation when dependencies have changed |
---|
677 | # |
---|
678 | T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN) |
---|
679 | assert T1 == T3, 'Recomputed value with changed dependencies failed' |
---|
680 | |
---|
681 | #def test_performance(self): |
---|
682 | # """Performance test (with statistics) |
---|
683 | # Don't really rely on this as it will depend on specific computer. |
---|
684 | # """ |
---|
685 | # |
---|
686 | # import time |
---|
687 | # set_option('savestat', 1) |
---|
688 | # |
---|
689 | # N = 300000 #Should be large on fast computers... |
---|
690 | # a = [1,2] |
---|
691 | # b = ('Thou shalt count the number three',4) |
---|
692 | # c = {'Five is right out': 6, (7,8): 9} |
---|
693 | # x = 3 |
---|
694 | # y = 'holy hand granate' |
---|
695 | # |
---|
696 | # |
---|
697 | # tt = time.time() |
---|
698 | # T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}) |
---|
699 | # t1 = time.time() - tt |
---|
700 | # |
---|
701 | # tt = time.time() |
---|
702 | # T2 = cache(f,(a,b,c,N), {'x':x, 'y':y}) |
---|
703 | # t2 = time.time() - tt |
---|
704 | |
---|
705 | # assert T1 == T2 |
---|
706 | # assert t1 > t2, 'WARNING: Performance a bit low - this could be specific to current platform. Try to increase N in this test' |
---|
707 | # #test_OK('Performance test: relative time saved = %s pct' \ |
---|
708 | # # %str(round((t1-t2)*100/t1,2))) |
---|
709 | |
---|
710 | |
---|
711 | def test_statsfile(self): |
---|
712 | """Test presence of statistics file |
---|
713 | """ |
---|
714 | import os, string |
---|
715 | statsfile = '.cache_stat' # Basefilename for cached statistics. |
---|
716 | |
---|
717 | CD = checkdir(cachedir) |
---|
718 | DIRLIST = os.listdir(CD) |
---|
719 | SF = [] |
---|
720 | for FN in DIRLIST: |
---|
721 | if string.find(FN,statsfile) >= 0: |
---|
722 | try: |
---|
723 | fid = open(CD+FN,'r') |
---|
724 | fid.close() |
---|
725 | except: |
---|
726 | raise Exception('Statistics files cannot be opened') |
---|
727 | |
---|
728 | |
---|
729 | |
---|
730 | # def test_network_cachedir(self): |
---|
731 | |
---|
732 | # #set_option('cachedir', 'H:\\.python_cache\\') |
---|
733 | # set_option('cachedir', 'V:\\2\\cit\\.python_cache\\') |
---|
734 | # set_option('verbose', 1) |
---|
735 | |
---|
736 | |
---|
737 | # # Make some test input arguments |
---|
738 | # # |
---|
739 | # N = 5000 #Make N fairly small here |
---|
740 | |
---|
741 | # a = [1,2] |
---|
742 | # b = ('Thou shalt count the number three',4) |
---|
743 | # c = {'Five is right out': 6, (7,8): 9} |
---|
744 | # x = 3 |
---|
745 | # y = 'holy hand granate' |
---|
746 | |
---|
747 | # # Test caching |
---|
748 | # # |
---|
749 | |
---|
750 | # comprange = 2 |
---|
751 | |
---|
752 | # for comp in range(comprange): |
---|
753 | |
---|
754 | # # Evaluate and store |
---|
755 | # # |
---|
756 | # T1 = cache(f, (a,b,c,N), {'x':x, 'y':y}, evaluate=1, \ |
---|
757 | # compression=comp) |
---|
758 | |
---|
759 | # # Retrieve |
---|
760 | # # |
---|
761 | # T2 = cache(f, (a,b,c,N), {'x':x, 'y':y}, compression=comp) |
---|
762 | |
---|
763 | # # Reference result |
---|
764 | # # |
---|
765 | # T3 = f(a,b,c,N,x=x,y=y) # Compute without caching |
---|
766 | |
---|
767 | |
---|
768 | # assert T1 == T2, 'Cached result does not match computed result' |
---|
769 | # assert T2 == T3, 'Cached result does not match computed result' |
---|
770 | |
---|
771 | |
---|
772 | |
---|
773 | def Will_fail_test_objects(self): |
---|
774 | """ |
---|
775 | This test shows how instances can't be effectively cached. |
---|
776 | myhash uses hash which uses id which uses the memory address. |
---|
777 | |
---|
778 | This will be a NIL problem once caching can handle instances with different id's and |
---|
779 | identical content. |
---|
780 | |
---|
781 | The test is disabled. |
---|
782 | """ |
---|
783 | |
---|
784 | |
---|
785 | |
---|
786 | verbose = True |
---|
787 | #verbose = False |
---|
788 | |
---|
789 | for i in range(2): |
---|
790 | if verbose: print "clear cache" |
---|
791 | a = cache(Dummy, 'clear') |
---|
792 | |
---|
793 | if verbose: print "cache for first time" |
---|
794 | a = cache(Dummy, args=(9,10), verbose=verbose) |
---|
795 | hash_value = myhash(a) |
---|
796 | |
---|
797 | #print "hash_value",hash_value |
---|
798 | if verbose: print "cache for second time" |
---|
799 | a = cache(Dummy, args=(9,10), verbose=verbose) |
---|
800 | |
---|
801 | #print "myhash(a)",myhash(a) |
---|
802 | assert hash_value == myhash(a) |
---|
803 | |
---|
804 | |
---|
805 | # This test works in the caching dir and in anuga_core, but not in the |
---|
806 | # anuga_core/source/anuga dir |
---|
807 | # This has to do with pickle (see e.g. http://telin.ugent.be/~slippens/drupal/pickleproblem) |
---|
808 | # The error message is |
---|
809 | # PicklingError: Can't pickle test_caching.Dummy: it's not the same object as test_caching.Dummy |
---|
810 | # |
---|
811 | # This problem was fixed by moving the class into the separate module |
---|
812 | |
---|
813 | def test_objects_are_created(self): |
---|
814 | """ |
---|
815 | This test shows how instances can be created from cache |
---|
816 | as long as input arguments are unchanged. |
---|
817 | |
---|
818 | Such instances will have different id's and cannot be currently be used as input |
---|
819 | arguments in subsequent caches. However, this is still useful. |
---|
820 | |
---|
821 | Do it for all combinations of compression |
---|
822 | |
---|
823 | """ |
---|
824 | |
---|
825 | verbose = False |
---|
826 | |
---|
827 | for compression_store in [False, True]: |
---|
828 | for compression_retrieve in [False, True]: |
---|
829 | |
---|
830 | if verbose: print 'clear cache' |
---|
831 | a = cache(Dummy, 'clear') |
---|
832 | |
---|
833 | if verbose: print 'cache for first time' |
---|
834 | a_ref = cache(Dummy, args=(9,10), |
---|
835 | compression=compression_store, |
---|
836 | verbose=verbose) |
---|
837 | |
---|
838 | if verbose: print 'Check that cache is there' |
---|
839 | assert cache(Dummy, args=(9,10), test=1, |
---|
840 | compression=compression_retrieve, |
---|
841 | verbose=verbose) |
---|
842 | |
---|
843 | if verbose: print 'Check cached result' |
---|
844 | a = cache(Dummy, args=(9,10), |
---|
845 | compression=compression_store, |
---|
846 | verbose=verbose) |
---|
847 | assert a.__dict__ == a_ref.__dict__ |
---|
848 | |
---|
849 | |
---|
850 | |
---|
851 | # NOTE (Ole): This test has been commented out because, |
---|
852 | # although the test will pass (not anymore!) |
---|
853 | # inside the caching dir and also at the anuga_core level, |
---|
854 | # it won't pass at the anuga_core/source/anuga level. |
---|
855 | # It may have to do with the comments above. |
---|
856 | # |
---|
857 | # But this is a very nice test to run occasionally within the caching |
---|
858 | # area |
---|
859 | def Xtest_objects_are_created_memory(self): |
---|
860 | """ |
---|
861 | |
---|
862 | This test shows how instances can be created from cache |
---|
863 | as long as input arguments are unchanged - even if the class |
---|
864 | lives in different memory locations. |
---|
865 | |
---|
866 | This is using cache created in the main program below |
---|
867 | """ |
---|
868 | |
---|
869 | verbose = True #False |
---|
870 | |
---|
871 | # Redefine class Dummy_memorytest |
---|
872 | class Dummy_memorytest: |
---|
873 | def __init__(self, value, another): |
---|
874 | self.value = value |
---|
875 | |
---|
876 | # Make sure that class has been redefined to another address |
---|
877 | print |
---|
878 | print 'Initial_addr ', initial_addr |
---|
879 | print 'Redefined addr', repr(Dummy_memorytest) |
---|
880 | msg = 'Redefined class ended up at same memory location as ' |
---|
881 | msg += 'original class making this test irrelevant. Try to run ' |
---|
882 | msg += 'it again and see if this error goes away.' |
---|
883 | msg += 'If it persists contact Ole.Nielsen@ga.gov.au' |
---|
884 | assert initial_addr != repr(Dummy_memorytest), msg |
---|
885 | |
---|
886 | |
---|
887 | retrieve_cache(Dummy_memorytest, verbose=verbose) |
---|
888 | |
---|
889 | # Cache created for use with 'test_objects_are_created_memory' |
---|
890 | #initial_addr = `Dummy_memorytest` |
---|
891 | #clear_and_create_cache(Dummy_memorytest, verbose=False) |
---|
892 | |
---|
893 | |
---|
894 | |
---|
895 | #------------------------------------------------------------- |
---|
896 | if __name__ == "__main__": |
---|
897 | suite = unittest.makeSuite(Test_Caching, 'test') |
---|
898 | runner = unittest.TextTestRunner() |
---|
899 | runner.run(suite) |
---|