From fcd520a093a366ab5407f845e2b75da24f5edd28 Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Mar 21 2017 12:40:36 +0000 Subject: Add performance checks to the test suite Signed-off-by: Patrick Uiterwijk --- diff --git a/tests/__init__.py b/tests/__init__.py index 418a01a..7267906 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -19,6 +19,9 @@ import tempfile import os logging.basicConfig(stream=sys.stderr) +# Always enable performance counting for tests +os.environ['PAGURE_PERFREPO'] = 'true' + from datetime import date from datetime import datetime from datetime import timedelta @@ -38,6 +41,7 @@ import pagure import pagure.lib import pagure.lib.model from pagure.lib.repo import PagureRepo +import pagure.perfrepo as perfrepo DB_PATH = 'sqlite:///:memory:' FAITOUT_URL = 'http://faitout.fedorainfracloud.org/' @@ -99,8 +103,32 @@ class Modeltests(unittest.TestCase): self.gitrepo = None self.gitrepos = None + def perfMaxWalks(self, max_walks, max_steps): + """ Check that we have not performed too many walks/steps. """ + num_walks = 0 + num_steps = 0 + for reqstat in perfrepo.REQUESTS: + for walk in reqstat['walks'].values(): + num_walks += 1 + num_steps += walk['steps'] + self.assertLessEqual(num_walks, max_walks, + '%s git repo walks performed, at most %s allowed' + % (num_walks, max_walks)) + self.assertLessEqual(num_steps, max_steps, + '%s git repo steps performed, at most %s allowed' + % (num_steps, max_steps)) + + def perfReset(self): + """ Reset perfrepo stats. """ + perfrepo.reset_stats() + perfrepo.REQUESTS = [] + def setUp(self): # pylint: disable=invalid-name """ Set up the environnment, ran before every tests. """ + # Clean up test performance info + perfrepo.reset_stats() + perfrepo.REQUESTS = [] + # Clean up eventual git repo left in the present folder. pagure.REDIS = None pagure.lib.REDIS = None diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index 6b43c34..dbd3bc8 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -1271,6 +1271,8 @@ class PagureFlaskRepotests(tests.Modeltests): output = self.app.get('/test') # No git repo associated self.assertEqual(output.status_code, 404) + self.perfMaxWalks(0, 0) + self.perfReset() tests.create_projects_git(self.path, bare=True) @@ -1280,6 +1282,8 @@ class PagureFlaskRepotests(tests.Modeltests): self.assertIn( '
\n' 'test project #1
', output.data) + self.perfMaxWalks(0, 0) + self.perfReset() output = self.app.get('/test/') self.assertEqual(output.status_code, 200) @@ -1287,10 +1291,13 @@ class PagureFlaskRepotests(tests.Modeltests): self.assertIn( '
\n' 'test project #1
', output.data) + self.perfMaxWalks(0, 0) + self.perfReset() # Add some content to the git repo tests.add_content_git_repo(os.path.join(self.path, 'test.git')) tests.add_readme_git_repo(os.path.join(self.path, 'test.git')) + self.perfReset() output = self.app.get('/test') self.assertEqual(output.status_code, 200) @@ -1299,6 +1306,8 @@ class PagureFlaskRepotests(tests.Modeltests): self.assertIn( '
\n' 'test project #1
', output.data) + self.perfMaxWalks(3, 8) # Target: (1, 3) + self.perfReset() # Turn that repo into a fork repo = pagure.lib.get_project(self.session, 'test') @@ -1324,6 +1333,8 @@ class PagureFlaskRepotests(tests.Modeltests): '
\n' 'test project #1
', output.data) self.assertTrue('Forked from' in output.data) + self.perfMaxWalks(1, 3) + self.perfReset() # Add a fork of a fork item = pagure.lib.model.Project( @@ -1352,6 +1363,8 @@ class PagureFlaskRepotests(tests.Modeltests): '
\n' 'test project #3
', output.data) self.assertTrue('Forked from' in output.data) + self.perfMaxWalks(3, 18) # Ideal: (1, 3) + self.perfReset() def test_view_repo_empty(self): """ Test the view_repo endpoint on a repo w/o master branch. """