From ddfe0b660ee7b6f901ad4bdc8adae5244a8b8b88 Mon Sep 17 00:00:00 2001 From: Nicola Tarocco Date: Mon, 26 Feb 2024 11:28:24 +0100 Subject: [PATCH] tests: make report generation time limit configurable --- .gitlab-ci.yml | 3 +-- README.md | 4 +--- app-config/openshift/deploymentconfig.yaml | 4 ++-- caimira/apps/calculator/__init__.py | 4 ++-- caimira/profiler.py | 4 ++++ caimira/tests/apps/calculator/test_report_generator.py | 6 ++++-- caimira/tests/apps/calculator/test_webapp.py | 9 +++------ 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fc846dc9..13cf58bb 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,7 +12,6 @@ include: variables: project_name: caimira - PY_VERSION: "3.9" # ################################################################################################### @@ -110,7 +109,7 @@ check_openshift_config_prod: - /kaniko/executor --context ${CI_PROJECT_DIR}/${DOCKER_CONTEXT_DIRECTORY} --dockerfile ${CI_PROJECT_DIR}/${DOCKERFILE_DIRECTORY}/Dockerfile --destination ${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:${IMAGE_TAG} # Print the full registry path of the pushed image - echo "Image pushed successfully to ${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:${IMAGE_TAG}" - + auth-service-image_builder: extends: diff --git a/README.md b/README.md index 94c68656..a2684aed 100644 --- a/README.md +++ b/README.md @@ -156,9 +156,7 @@ pytest ./caimira ### Running the profiler -The profiler is enabled in one of the following cases: -- the calculator app is running in `debug` mode -- the environment variable `CAIMIRA_PROFILER_ENABLED` is set to 1 +The profiler is enabled when the environment variable `CAIMIRA_PROFILER_ENABLED` is set to 1. When visiting http://localhost:8080/profiler, you can start a new session and choose between [PyInstrument](https://github.com/joerick/pyinstrument) or [cProfile](https://docs.python.org/3/library/profile.html#module-cProfile). The app includes two different profilers, mainly because they can give different information. diff --git a/app-config/openshift/deploymentconfig.yaml b/app-config/openshift/deploymentconfig.yaml index 01023cd3..ac58a1ea 100644 --- a/app-config/openshift/deploymentconfig.yaml +++ b/app-config/openshift/deploymentconfig.yaml @@ -284,7 +284,7 @@ key: ARVE_API_KEY name: arve-api - name: DATA_SERVICE_ENABLED - value: 'False' + value: 'True' image: '${PROJECT_NAME}/calculator-app' ports: - containerPort: 8080 @@ -363,7 +363,7 @@ - name: CAIMIRA_CALCULATOR_PREFIX value: /calculator-open - name: DATA_SERVICE_ENABLED - value: 'False' + value: 'True' image: '${PROJECT_NAME}/calculator-app' ports: - containerPort: 8080 diff --git a/caimira/apps/calculator/__init__.py b/caimira/apps/calculator/__init__.py index d7b53f7d..8898f272 100644 --- a/caimira/apps/calculator/__init__.py +++ b/caimira/apps/calculator/__init__.py @@ -516,8 +516,8 @@ def make_app( 'filename': 'userguide.html.j2'}), ] - profiler = os.environ.get('CAIMIRA_PROFILER_ENABLED', 0) - if debug or profiler: + profiler_enabled = os.environ.get('CAIMIRA_PROFILER_ENABLED', 0) + if profiler_enabled: urls += [ (get_root_url(CaimiraProfiler.ROOT_URL), ProfilerPage), (get_root_url(r'{root_url}/(.*)'.format(root_url=CaimiraProfiler.ROOT_URL)), ProfilerReport), diff --git a/caimira/profiler.py b/caimira/profiler.py index 6c39212e..8f3c26a0 100644 --- a/caimira/profiler.py +++ b/caimira/profiler.py @@ -195,6 +195,10 @@ class CaimiraProfiler: @property def is_active(self): """Return True if a session is active.""" + enabled = os.environ.get("CAIMIRA_PROFILER_ENABLED", 0) + if not enabled: + return False + if not os.path.exists(self._cache_filepath): return False diff --git a/caimira/tests/apps/calculator/test_report_generator.py b/caimira/tests/apps/calculator/test_report_generator.py index 18190777..22355c5d 100644 --- a/caimira/tests/apps/calculator/test_report_generator.py +++ b/caimira/tests/apps/calculator/test_report_generator.py @@ -1,8 +1,8 @@ import concurrent.futures from functools import partial +import os import time -import numpy.testing import numpy as np import pytest @@ -16,7 +16,7 @@ def test_generate_report(baseline_form) -> None: # generate a report for it. Because this is what happens in the caimira # calculator, we confirm that the generation happens within a reasonable # time threshold. - time_limit: float = 20.0 # seconds + time_limit: float = float(os.environ.get("CAIMIRA_TESTS_CALCULATOR_TIMEOUT", 10.)) start = time.perf_counter() @@ -25,6 +25,8 @@ def test_generate_report(baseline_form) -> None: concurrent.futures.ThreadPoolExecutor, 1, )) end = time.perf_counter() + total = end-start + print(f"Time limit: {time_limit} | Time taken: {end} - {start} = {total} < {time_limit}") assert report != "" assert end - start < time_limit diff --git a/caimira/tests/apps/calculator/test_webapp.py b/caimira/tests/apps/calculator/test_webapp.py index 32d22352..dc6ea769 100644 --- a/caimira/tests/apps/calculator/test_webapp.py +++ b/caimira/tests/apps/calculator/test_webapp.py @@ -1,13 +1,13 @@ +import os from pathlib import Path import pytest import tornado.testing -from retry import retry import caimira.apps.calculator from caimira.apps.calculator.report_generator import generate_permalink -_TIMEOUT = 20. +_TIMEOUT = float(os.environ.get("CAIMIRA_TESTS_CALCULATOR_TIMEOUT", 10.)) @pytest.fixture @@ -34,7 +34,6 @@ async def test_404(http_server_client): assert resp.code == 404 -@retry(tries=10) class TestBasicApp(tornado.testing.AsyncHTTPTestCase): def get_app(self): return caimira.apps.calculator.make_app() @@ -63,7 +62,6 @@ class TestBasicApp(tornado.testing.AsyncHTTPTestCase): assert 'expected number of new cases is' in response.body.decode() -@retry(tries=10) class TestCernApp(tornado.testing.AsyncHTTPTestCase): def get_app(self): cern_theme = Path(caimira.apps.calculator.__file__).parent.parent / 'themes' / 'cern' @@ -76,7 +74,6 @@ class TestCernApp(tornado.testing.AsyncHTTPTestCase): assert 'expected number of new cases is' in response.body.decode() -retry(tries=10) class TestOpenApp(tornado.testing.AsyncHTTPTestCase): def get_app(self): return caimira.apps.calculator.make_app(calculator_prefix="/mycalc") @@ -149,7 +146,7 @@ class TestCERNGenericPage(tornado.testing.AsyncHTTPTestCase): ] return tornado.web.Application(pages, **app.settings) - + @tornado.testing.gen_test(timeout=_TIMEOUT) def test_user_guide(self): response = yield self.http_client.fetch(self.get_url('/calculator/user-guide'))