|
10 | 10 | # See the License for the specific language governing permissions and |
11 | 11 | # limitations under the License. |
12 | 12 | """Shall provide tests to check performance overhead of pyfakefs.""" |
13 | | - |
| 13 | +import os |
| 14 | +import time |
14 | 15 | import unittest |
15 | 16 |
|
16 | 17 | from pyfakefs.fake_filesystem_unittest import TestCase |
| 18 | +from pyfakefs.helpers import IS_PYPY |
| 19 | + |
| 20 | +if os.environ.get('TEST_PERFORMANCE'): |
| 21 | + |
| 22 | + class SetupPerformanceTest(TestCase): |
| 23 | + @classmethod |
| 24 | + def setUpClass(cls) -> None: |
| 25 | + cls.start_time = time.time() |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def tearDownClass(cls) -> None: |
| 29 | + cls.elapsed_time = time.time() - cls.start_time |
| 30 | + print('Elapsed time per test for cached setup: {:.3f} ms'.format( |
| 31 | + cls.elapsed_time * 10)) |
| 32 | + |
| 33 | + def setUp(self) -> None: |
| 34 | + self.setUpPyfakefs() |
17 | 35 |
|
| 36 | + class SetupNoCachePerformanceTest(TestCase): |
| 37 | + @classmethod |
| 38 | + def setUpClass(cls) -> None: |
| 39 | + cls.start_time = time.time() |
18 | 40 |
|
19 | | -class SetupPerformanceTest(TestCase): |
20 | | - def setUp(self) -> None: |
21 | | - self.setUpPyfakefs() |
| 41 | + @classmethod |
| 42 | + def tearDownClass(cls) -> None: |
| 43 | + cls.elapsed_time = time.time() - cls.start_time |
| 44 | + print('Elapsed time per test for uncached setup: {:.3f} ms'.format( |
| 45 | + cls.elapsed_time * 10)) |
22 | 46 |
|
| 47 | + def setUp(self) -> None: |
| 48 | + self.setUpPyfakefs(use_cache=False) |
23 | 49 |
|
24 | | -class SetupNoCachePerformanceTest(TestCase): |
25 | | - def setUp(self) -> None: |
26 | | - self.setUpPyfakefs(use_cache=False) |
| 50 | + @unittest.skipIf(IS_PYPY, 'PyPy times are not comparable') |
| 51 | + class TimePerformanceTest(TestCase): |
| 52 | + """Make sure performance degradation in setup is noticed. |
| 53 | + The numbers are related to the CI builds and may fail in local builds. |
| 54 | + """ |
27 | 55 |
|
| 56 | + def test_cached_time(self): |
| 57 | + self.assertLess(SetupPerformanceTest.elapsed_time, 0.4) |
28 | 58 |
|
29 | | -def test_setup(self): |
30 | | - pass |
| 59 | + def test_uncached_time(self): |
| 60 | + self.assertLess(SetupNoCachePerformanceTest.elapsed_time, 6) |
31 | 61 |
|
| 62 | + def test_setup(self): |
| 63 | + pass |
32 | 64 |
|
33 | | -for n in range(100): |
34 | | - test_name = "test_" + str(n) |
35 | | - setattr(SetupPerformanceTest, test_name, test_setup) |
36 | | - test_name = "test_nocache" + str(n) |
37 | | - setattr(SetupNoCachePerformanceTest, test_name, test_setup) |
| 65 | + for n in range(100): |
| 66 | + test_name = "test_" + str(n) |
| 67 | + setattr(SetupPerformanceTest, test_name, test_setup) |
| 68 | + test_name = "test_nocache" + str(n) |
| 69 | + setattr(SetupNoCachePerformanceTest, test_name, test_setup) |
38 | 70 |
|
39 | | -if __name__ == "__main__": |
40 | | - unittest.main() |
| 71 | + if __name__ == "__main__": |
| 72 | + unittest.main() |
0 commit comments