#!/bin/bash

# Define global variables
export SCHEMA=tpch
export TPCH_SQL=tpch.sql
export SF=1000
export DATA_DIR=/git/tpch/sf1000

echo "=== PHASE 1: Initialize Environment and Load Data ==="
# Restart MariaDB to ensure a clean state before loading
./start-mariadb.sh
sleep 20
mariadb -e "source query-settings.sql;" >query-settings.log
#mariadb -e "source create-tables-2-duckdb-tbl.sql;use tpch; show tables; show create table tpch.lineitem;" 
./start-all-monitor.sh
sleep 30
echo "Loading data start: $(date)"
SCHEMA=tpch SF=1000 DATA_DIR=/git/tpch/sf1000 TPCH_SQL=tpch.sql  ./04_load-2-tbl-duckdb.sh
echo "Loading data end: $(date)"
sleep 20


echo "=== PHASE 2: Iteration 1 (Cold Queries) ==="
# Restart MariaDB to completely flush the InnoDB buffer pool and OS cache
./start-mariadb.sh
sleep 20

echo "Cold queries start: $(date)"
SCHEMA=tpch SF=1000 DATA_DIR=/git/tpch/sf1000 TPCH_SQL=tpch.sql   ./05_run_queries.sh
echo "Cold queries end: $(date)"
sleep 20


echo "=== PHASE 3: Iteration 2 (Hot Queries - 1st Pass) ==="
# DO NOT RESTART: Run directly on the already running instance (Warm cache)
echo "Hot queries 1st start: $(date)"
SCHEMA=tpch SF=1000 DATA_DIR=/git/tpch/sf1000 TPCH_SQL=tpch.sql  time ./05_run_queries.sh
echo "Hot queries 1st end: $(date)"
sleep 20


echo "=== PHASE 4: Iteration 3 (Hot Queries - 2nd Pass) ==="
# DO NOT RESTART: Run directly again to test fully saturated memory buffer pools
echo "Hot queries 2nd start: $(date)"
SCHEMA=tpch SF=1000 DATA_DIR=/git/tpch/sf1000 TPCH_SQL=tpch.sql  time ./05_run_queries.sh
echo "Hot queries 2nd end: $(date)"
sleep 20
./stop-all-monitor.sh

