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