Skip to content

Commit

Permalink
feat(eval-perf,sql log): now include endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesfize committed Mar 27, 2024
1 parent 082d8cf commit 55be4d8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/eval.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ on:

jobs:
build:
runs-on: self-hosted
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
debian-version: ['11', '12']
include:
- debian-version: '11'
# python-version: "3.9"
python-version: '3.9'
postgres-version: '13'
postgis-version: '3.2'
- debian-version: '12'
# python-version: "3.11"
python-version: '3.11'
postgres-version: '15'
postgis-version: '3.3'

Expand Down
14 changes: 9 additions & 5 deletions backend/geonature/tests/benchmarks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
from geonature.utils.env import db
from .benchmark_generator import CLater, BenchmarkTest
from geonature.tests.test_synthese import blur_sensitive_observations
import traceback
from geonature.tests.fixtures import app

logging.basicConfig()
logger = logging.getLogger("logger-name")
logger.setLevel(logging.DEBUG)


@pytest.fixture(scope="session")
def activate_profiling_sql(sqllogfilename: pytest.FixtureDef):
@pytest.fixture(scope="function")
def activate_profiling_sql(sqllogfilename: pytest.FixtureDef, app: pytest.FixtureDef):
"""
Fixture to activate profiling for SQL queries and store query's statements and execution times in a CSV file.
Expand All @@ -29,6 +31,7 @@ def activate_profiling_sql(sqllogfilename: pytest.FixtureDef):
The path to the CSV file where the query statements and execution times will be stored.
"""
columns = ["Endpoint", "Query", "Total Time [s.]"]

if not sqllogfilename:
logger.debug("No SQL Log file provided. SQL Profiling will not be activated.")
Expand All @@ -38,8 +41,9 @@ def activate_profiling_sql(sqllogfilename: pytest.FixtureDef):
if directory and not os.path.exists(directory):
raise FileNotFoundError(f"Directory {directory} does not exists ! ")

df = pandas.DataFrame([], columns=["Query", "Total Time [s.]"])
df.to_csv(sqllogfilename, header=True, index=None, sep=";")
if not os.path.exists(sqllogfilename):
df = pandas.DataFrame([], columns=columns)
df.to_csv(sqllogfilename, header=True, index=None, sep=";")

def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
conn.info.setdefault("query_start_time", []).append(time.time())
Expand All @@ -51,7 +55,7 @@ def after_cursor_execute(conn, cursor, statement, parameters, context, executema
logger.debug("Query Complete!")
logger.debug("Total Time: %f" % total)
if statement.startswith("SELECT"):
df = pandas.DataFrame([[statement, total]], columns=["Query", "Total Time"])
df = pandas.DataFrame([[pytest.endpoint, statement, total]], columns=columns)
df.to_csv(sqllogfilename, mode="a", header=False, index=None, sep=";")

event.listen(db.engine, "before_cursor_execute", before_cursor_execute)
Expand Down
2 changes: 2 additions & 0 deletions backend/geonature/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from pypnusershub.tests.fixtures import teardown_logout_user
import pytest

pytest.endpoint = ""


def pytest_addoption(parser):
parser.addoption("--sql-log-filename", action="store", default=None)
Expand Down
6 changes: 5 additions & 1 deletion backend/geonature/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest
import sqlalchemy as sa

from flask import current_app, testing, url_for
from flask import current_app, testing, url_for, request
from geoalchemy2.shape import from_shape
from PIL import Image
from shapely.geometry import Point
Expand Down Expand Up @@ -97,6 +97,10 @@ def app():
app.test_client_class = GeoNatureClient
app.config["SERVER_NAME"] = "test.geonature.fr" # required by url_for

@app.before_request
def get_endpoint():
pytest.endpoint = request.endpoint

with app.app_context():
"""
Note: This may seem redundant with 'temporary_transaction' fixture.
Expand Down

0 comments on commit 55be4d8

Please sign in to comment.