Skip to content

Commit 0ba2e0e

Browse files
committed
Add test for checking the setup time
- run performance tests only if configured per env var - times are taken from current CI tests with some margin - safeguard against performance degradation in setup
1 parent 61ad750 commit 0ba2e0e

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

.github/workflows/pythonpackage.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ jobs:
8181
shell: bash
8282
- name: Run performance tests
8383
run: |
84-
python -m pyfakefs.tests.performance_test SetupPerformanceTest
85-
python -m pyfakefs.tests.performance_test SetupNoCachePerformanceTest
84+
export TEST_PERFORMANCE=1
85+
python -m pyfakefs.tests.performance_test
8686
shell: bash
8787

8888
dockertests:

pyfakefs/tests/performance_test.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,63 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212
"""Shall provide tests to check performance overhead of pyfakefs."""
13-
13+
import os
14+
import time
1415
import unittest
1516

1617
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()
1735

36+
class SetupNoCachePerformanceTest(TestCase):
37+
@classmethod
38+
def setUpClass(cls) -> None:
39+
cls.start_time = time.time()
1840

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))
2246

47+
def setUp(self) -> None:
48+
self.setUpPyfakefs(use_cache=False)
2349

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+
"""
2755

56+
def test_cached_time(self):
57+
self.assertLess(SetupPerformanceTest.elapsed_time, 0.4)
2858

29-
def test_setup(self):
30-
pass
59+
def test_uncached_time(self):
60+
self.assertLess(SetupNoCachePerformanceTest.elapsed_time, 6)
3161

62+
def test_setup(self):
63+
pass
3264

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)
3870

39-
if __name__ == "__main__":
40-
unittest.main()
71+
if __name__ == "__main__":
72+
unittest.main()

0 commit comments

Comments
 (0)