← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~bjornt/maas:perf-test-cli-options into maas:master

 

Björn Tillenius has proposed merging ~bjornt/maas:perf-test-cli-options into maas:master with ~bjornt/maas:perf-test-no-custom-runner as a prerequisite.

Commit message:
Add pytest CLI options for maasperf.

Now --perf-output-file and --perf-profiling-tag can be specified
when running pytest to control where the output timeing and profiling
data.

I also fixed conftest.py so that pytest can process it in --help.
It had global imports of maasserver, which doesn't work since
django isn't set up when --help runs.

I also made use of pytest_plugins rather than importing all the
fixtures explictly.



Requested reviews:
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~bjornt/maas/+git/maas/+merge/433596
-- 
Your team MAAS Maintainers is requested to review the proposed merge of ~bjornt/maas:perf-test-cli-options into maas:master.
diff --git a/src/maasperf/tests/conftest.py b/src/maasperf/tests/conftest.py
index 02a24bb..3850845 100644
--- a/src/maasperf/tests/conftest.py
+++ b/src/maasperf/tests/conftest.py
@@ -3,24 +3,18 @@
 
 from pytest import fixture
 
-from maasserver.models.user import get_auth_tokens
-from maasserver.testing.factory import factory as maasserver_factory
-from maasserver.testing.testclient import MAASSensibleOAuthClient
-from maastesting.perftest import perf
-from maastesting.pytest import configure_seeds, random_seed
-
 __all__ = [
     "admin_api_client",
     "api_client",
-    "configure_seeds",
     "django_db_setup",
     "factory",
     "maas_user",
-    "perf",
-    "random_seed",
 ]
 
 
+pytest_plugins = "maastesting.pytest.perftest,maastesting.pytest.seeds"
+
+
 # override pytest-django's db setup
 @fixture(scope="session")
 def django_db_setup():
@@ -29,6 +23,9 @@ def django_db_setup():
 
 @fixture(scope="session")
 def factory():
+    # Local imports from maasserver so that pytest --help works
+    from maasserver.testing.factory import factory as maasserver_factory
+
     return maasserver_factory
 
 
@@ -44,6 +41,10 @@ def maas_user(factory):
 
 @fixture()
 def api_client(maas_user):
+    # Local imports from maasserver so that pytest --help works
+    from maasserver.models.user import get_auth_tokens
+    from maasserver.testing.testclient import MAASSensibleOAuthClient
+
     return MAASSensibleOAuthClient(
         user=maas_user, token=get_auth_tokens(maas_user)[0]
     )
@@ -51,4 +52,8 @@ def api_client(maas_user):
 
 @fixture()
 def admin_api_client(admin):
+    # Local imports from maasserver so that pytest --help works
+    from maasserver.models.user import get_auth_tokens
+    from maasserver.testing.testclient import MAASSensibleOAuthClient
+
     return MAASSensibleOAuthClient(user=admin, token=get_auth_tokens(admin)[0])
diff --git a/src/maastesting/perftest.py b/src/maastesting/pytest/perftest.py
similarity index 85%
rename from src/maastesting/perftest.py
rename to src/maastesting/pytest/perftest.py
index 6c66950..827a9e6 100644
--- a/src/maastesting/perftest.py
+++ b/src/maastesting/pytest/perftest.py
@@ -32,13 +32,31 @@ def maas_data():
     return None
 
 
+def pytest_addoption(parser):
+    parser.addoption(
+        "--perf-output-file",
+        help="The file where to write the performance measurement as JSON.",
+    )
+    parser.addoption(
+        "--perf-profiling-tag",
+        help="If specified, create profiling dumps for the measured tests.",
+    )
+
+
 @fixture(scope="session")
-def perf():
+def perf(pytestconfig):
+    profiling_tag = os.environ.get("MAAS_PROFILING")
+    if not profiling_tag:
+        profiling_tag = pytestconfig.getoption("--perf-profiling-tag", None)
     perf_tester = PerfTester(
-        os.environ.get("GIT_BRANCH"), os.environ.get("GIT_HASH")
+        os.environ.get("GIT_BRANCH"),
+        os.environ.get("GIT_HASH"),
+        profiling_tag,
     )
     yield perf_tester
     output = os.environ.get("OUTPUT_FILE")
+    if not output:
+        output = pytestconfig.getoption("--perf-output-file", None)
     if output:
         with open(output, "w") as f:
             perf_tester.finish_build(f)
@@ -68,15 +86,15 @@ def measure_time():
 class PerfTester:
     """PerfTester is responsible for recording performance tests"""
 
-    def __init__(self, git_branch, git_hash):
+    def __init__(self, git_branch, git_hash, profiling_tag):
         self.results = {"branch": git_branch, "commit": git_hash, "tests": {}}
+        self.profiling_tag = profiling_tag
 
     @contextmanager
     def record(self, name):
         with ExitStack() as stack:
-            profiling_tag = os.environ.get("MAAS_PROFILING")
-            if profiling_tag:
-                stack.enter_context(profile(name, profiling_tag))
+            if self.profiling_tag:
+                stack.enter_context(profile(name, self.profiling_tag))
             timing = stack.enter_context(measure_time())
             yield
         self.results["tests"][name] = {"duration": timing.duration}
diff --git a/src/maastesting/pytest.py b/src/maastesting/pytest/seeds.py
similarity index 100%
rename from src/maastesting/pytest.py
rename to src/maastesting/pytest/seeds.py

Follow ups