Skip to content

Commit

Permalink
(test,eval_perf)add sql log filename in pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesfize committed Mar 27, 2024
1 parent 494e98c commit 0872b9c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
30 changes: 23 additions & 7 deletions backend/geonature/tests/benchmarks/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
import logging
import os

import pytest
import pandas
Expand All @@ -14,17 +15,32 @@
logger.setLevel(logging.DEBUG)


@pytest.fixture(scope="class")
def activate_profiling_sql():
@pytest.fixture(scope="session")
def activate_profiling_sql(sqllogfilename: pytest.FixtureDef):
"""
Fixture to activate profiling for SQL queries and storing query's statements and execution times in a csv file.
Fixture to activate profiling for SQL queries and store query's statements and execution times in a CSV file.
This fixture takes a `sqllogfilename` parameter, which is the path to a CSV file where the query statements and
execution times will be stored. If no `sqllogfilename` is provided, the SQL profiling will not be activated.
Parameters
----------
- sqllogfilename: pytest.FixtureDef
The path to the CSV file where the query statements and execution times will be stored.
"""

results_file = "sql_queries.csv"
if not sqllogfilename:
logger.debug("No SQL Log file provided. SQL Profiling will not be activated.")
return

directory = os.path.dirname(sqllogfilename)
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(results_file, mode="a", header=True, index=None, sep=";")
df.to_csv(sqllogfilename, header=True, index=None, sep=";")

# @event.listens_for(Engine, "before_cursor_execute")
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
conn.info.setdefault("query_start_time", []).append(time.time())
logger.debug("Start Query: %s" % statement)
Expand All @@ -36,7 +52,7 @@ def after_cursor_execute(conn, cursor, statement, parameters, context, executema
logger.debug("Total Time: %f" % total)
if statement.startswith("SELECT"):
df = pandas.DataFrame([[statement, total]], columns=["Query", "Total Time"])
df.to_csv(results_file, mode="a", header=False, index=None, sep=";")
df.to_csv(sqllogfilename, mode="a", header=False, index=None, sep=";")

event.listen(db.engine, "before_cursor_execute", before_cursor_execute)
event.listen(db.engine, "after_cursor_execute", after_cursor_execute)
Expand Down
10 changes: 10 additions & 0 deletions backend/geonature/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# force discovery of some fixtures
from .fixtures import app, users, _session
from pypnusershub.tests.fixtures import teardown_logout_user
import pytest


def pytest_addoption(parser):
parser.addoption("--sql-log-filename", action="store", default=None)


@pytest.fixture(scope="session")
def sqllogfilename(request):
return request.config.getoption("--sql-log-filename")

0 comments on commit 0872b9c

Please sign in to comment.