mirror of
https://github.com/sandronator/finetuning_aufgabe4.git
synced 2026-09-04 08:36:09 +02:00
Two related improvements to make the test runs both usable and
diagnosable:
1. Avoid lock waits / "deadlock" hangs during CLUSTER
The Manager connection ran EXPLAIN ANALYZE without committing, so
psycopg2's default autocommit=False left an open transaction
holding AccessShareLock on publ and auth. The next setup_db then
blocked on DROP INDEX / CLUSTER (both need AccessExclusiveLock),
visible as a hang -- typically at the CLUSTER step. Setting
conn_postsql.autocommit = True releases the locks immediately after
each SELECT, which is safe here because all Manager queries are
read-only.
2. Make the run output identify which test is running
The "Running: ..." line previously printed only the query key
(query_1/query_2), shadowing 'strategy' with the dict key and never
surfacing which join strategy or index config was active. Output
now reads e.g. "[Aufgabe 3b | SortMergeStrategy | idx=nc-both]" by:
- using self.__class__.__name__ so subclasses surface correctly
- passing the current index_config from Manager into the strategy
- threading an optional 'aufgabe' label through Manager.execute()
and into each strategy's run() (including the Nested/SortMerge/
Hash wrappers that override run()).
main.py now tags each execute() call with its assignment number.
Co-Authored-By: Claude Opus 4.7 <[email protected]>
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from setup import get_connection
|
|
import time
|
|
import psycopg2
|
|
import mariadb
|
|
|
|
|
|
|
|
class BaseStrategy:
|
|
|
|
def __init__(self, conn: psycopg2.extensions.connection | mariadb.Connection, db_name, queries: dict[str, str]):
|
|
self.connection = conn
|
|
self.cursor = conn.cursor()
|
|
self.db_name = db_name
|
|
self.queries = queries
|
|
self.index_config = None # vom Manager via setup_db gesetzt
|
|
|
|
def set_queries(self, queries):
|
|
self.queries = queries
|
|
|
|
def run(self, aufgabe: str | None = None):
|
|
strategy_name = self.__class__.__name__
|
|
prefix_parts = []
|
|
if aufgabe:
|
|
prefix_parts.append(aufgabe)
|
|
prefix_parts.append(strategy_name)
|
|
if self.index_config:
|
|
prefix_parts.append(f"idx={self.index_config}")
|
|
prefix = " | ".join(prefix_parts)
|
|
|
|
for query_name, query in self.queries.items():
|
|
print(f"Running: [{prefix}] / {query_name}\n")
|
|
start = time.time()
|
|
self.cursor.execute(query)
|
|
end = time.time()
|
|
self._print_result(query_name, end, start, prefix)
|
|
|
|
|
|
def _print_result(self, query_name, end, start, prefix):
|
|
duration = end - start
|
|
minutes = int(duration // 60)
|
|
seconds = duration % 60
|
|
|
|
print(f"---- RESULTS FROM {self.db_name} | {prefix} | Query: {query_name} ----\n")
|
|
print(f"duration: {minutes}m {seconds:.2f}s")
|
|
_print_cursor(self.cursor)
|
|
|
|
def _print_cursor(cursor):
|
|
for row in cursor.fetchall():
|
|
print(f"{row} '\n'")
|
|
|