88from pytest_benchmark .fixture import BenchmarkFixture
99from datetime import UTC , datetime
1010
11+ import psycopg2
12+
1113from app import database
1214from app .database import get_session
13- from configuration import configuration
15+ from configuration import configuration , AppConfig
1416from utils .suid import get_suid
1517from models .database .conversations import UserConversation
1618
2022LARGE_DB_RECORDS_COUNT = 10000
2123
2224
23- @pytest .fixture (name = "configuration_filename " )
24- def configuration_filename_fixture () -> str :
25+ @pytest .fixture (name = "configuration_filename_sqlite " )
26+ def configuration_filename_sqlite_fixture () -> str :
2527 """Retrieve configuration file name to be used by benchmarks.
2628
2729 Parameters:
@@ -33,8 +35,21 @@ def configuration_filename_fixture() -> str:
3335 return "tests/configuration/benchmarks-sqlite.yaml"
3436
3537
38+ @pytest .fixture (name = "configuration_filename_postgres" )
39+ def configuration_filename_postgres_fixture () -> str :
40+ """Retrieve configuration file name to be used by benchmarks.
41+
42+ Parameters:
43+ None
44+
45+ Returns:
46+ str: Path to the benchmark configuration file to load.
47+ """
48+ return "tests/configuration/benchmarks-postgres.yaml"
49+
50+
3651@pytest .fixture (name = "sqlite_database" )
37- def sqlite_database_fixture (configuration_filename : str , tmp_path : Path ) -> None :
52+ def sqlite_database_fixture (configuration_filename_sqlite : str , tmp_path : Path ) -> None :
3853 """Initialize a temporary SQLite database for benchmarking.
3954
4055 This fixture:
@@ -44,14 +59,14 @@ def sqlite_database_fixture(configuration_filename: str, tmp_path: Path) -> None
4459 - Initializes the DB engine and creates required tables.
4560
4661 Parameters:
47- configuration_filename (str): Path to the YAML configuration file to load.
62+ configuration_filename_sqlite (str): Path to the YAML configuration file to load.
4863 tmp_path (Path): pytest-provided temporary directory for creating the DB file.
4964
5065 Raises:
5166 AssertionError: If the configuration does not include an sqlite configuration.
5267 """
5368 # try to load the configuration containing SQLite database setup
54- configuration .load_configuration (configuration_filename )
69+ configuration .load_configuration (configuration_filename_sqlite )
5570 assert configuration .database_configuration .sqlite is not None
5671
5772 # we need to start each benchmark with empty database
@@ -62,6 +77,62 @@ def sqlite_database_fixture(configuration_filename: str, tmp_path: Path) -> None
6277 database .create_tables ()
6378
6479
80+ def drop_postgres_tables (configuration : AppConfig ) -> None :
81+ """Drop postgres tables used by benchmarks.
82+
83+ The tables will be re-created so every benchmark start with fresh DB.
84+ """
85+
86+ pgconfig = configuration .database_configuration .postgres
87+ assert pgconfig is not None
88+
89+ # try to connect to Postgres
90+ conn = psycopg2 .connect (
91+ database = pgconfig .db ,
92+ user = pgconfig .user ,
93+ password = pgconfig .password .get_secret_value (),
94+ host = pgconfig .host ,
95+ port = pgconfig .port ,
96+ )
97+
98+ # try to drop tables used by benchmarks
99+ try :
100+ with conn .cursor () as cursor :
101+ cursor .execute ("DROP TABLE IF EXISTS user_turn;" )
102+ cursor .execute ("DROP TABLE IF EXISTS user_conversation;" )
103+ conn .commit ()
104+ finally :
105+ # closing the connection
106+ conn .close ()
107+
108+
109+ @pytest .fixture (name = "postgres_database" )
110+ def postgres_database_fixture (configuration_filename_postgres : str ) -> None :
111+ """Initialize a temporary postgres database for benchmarking.
112+
113+ This fixture:
114+ - Loads the provided configuration file.
115+ - Ensures an Postgres configuration is present.
116+ - Initializes the DB engine and creates required tables.
117+
118+ Parameters:
119+ configuration_filename_postgres (str): Path to the YAML configuration file to load.
120+
121+ Raises:
122+ AssertionError: If the configuration does not include an postgres configuration.
123+ """
124+ # try to load the configuration containing postgres database setup
125+ configuration .load_configuration (configuration_filename_postgres )
126+ assert configuration .database_configuration .postgres is not None
127+
128+ # make sure all tables will be re-initialized
129+ drop_postgres_tables (configuration )
130+
131+ # initialize database session and create tables
132+ database .initialize_database ()
133+ database .create_tables ()
134+
135+
65136def generate_provider () -> str :
66137 """Return a randomly chosen provider name.
67138
0 commit comments