Warms up, gets an object count, runs the test, checks for new objects.
(self, *args, **kwargs)
| 573 | """ |
| 574 | |
| 575 | def decorator(self, *args, **kwargs): |
| 576 | """Warms up, gets an object count, runs the test, checks for new objects.""" |
| 577 | with context.eager_mode(): |
| 578 | gc.disable() |
| 579 | # Run the test 2 times as warmup, in an attempt to fill up caches, which |
| 580 | # should not grow as the test is run repeatedly below. |
| 581 | # |
| 582 | # TODO(b/117156879): Running warmup twice is black magic; we have seen |
| 583 | # tests that fail with 1 warmup run, and pass with 2, on various versions |
| 584 | # of python2.7.x. |
| 585 | for _ in range(2): |
| 586 | f(self, *args, **kwargs) |
| 587 | gc.collect() |
| 588 | previous_count = len(gc.get_objects()) |
| 589 | if ops.has_default_graph(): |
| 590 | collection_sizes_before = { |
| 591 | collection: len(ops.get_collection(collection)) |
| 592 | for collection in ops.get_default_graph().collections |
| 593 | } |
| 594 | for _ in range(3): |
| 595 | f(self, *args, **kwargs) |
| 596 | # Note that gc.get_objects misses anything that isn't subject to garbage |
| 597 | # collection (C types). Collections are a common source of leaks, so we |
| 598 | # test for collection sizes explicitly. |
| 599 | if ops.has_default_graph(): |
| 600 | for collection_key in ops.get_default_graph().collections: |
| 601 | collection = ops.get_collection(collection_key) |
| 602 | size_before = collection_sizes_before.get(collection_key, 0) |
| 603 | if len(collection) > size_before: |
| 604 | raise AssertionError( |
| 605 | ("Collection %s increased in size from " |
| 606 | "%d to %d (current items %s).") % |
| 607 | (collection_key, size_before, len(collection), collection)) |
| 608 | # Make sure our collection checks don't show up as leaked memory by |
| 609 | # removing references to temporary variables. |
| 610 | del collection |
| 611 | del collection_key |
| 612 | del size_before |
| 613 | del collection_sizes_before |
| 614 | gc.collect() |
| 615 | # There should be no new Python objects hanging around. |
| 616 | new_count = len(gc.get_objects()) |
| 617 | # In some cases (specifacally on MacOS), new_count is somehow |
| 618 | # smaller than previous_count. |
| 619 | # Using plain assert because not all classes using this decorator |
| 620 | # have assertLessEqual |
| 621 | assert new_count <= previous_count, ( |
| 622 | "new_count(%d) is not less than or equal to previous_count(%d)" % |
| 623 | (new_count, previous_count)) |
| 624 | gc.enable() |
| 625 | |
| 626 | return decorator |
| 627 |
no test coverage detected