#!/bin/bash

# Set up the directory
BENCH_DIR="$HOME/competitor_bench/MySQL"
rm -rf "$BENCH_DIR"
mkdir -p "$BENCH_DIR"
#exec > "$BENCH_DIR/full_bench.log" 2>&1

# Horrible but working way of finding the argument after --email.
email="$(perl -e 'use List::Util qw(first); my $email_idx = 1 + first { $ARGV[$_] eq "--email" } 0 .. $#ARGV; print $ARGV[$email_idx]' -- "$@")"

# Build the stress client
cd bench/stress-client
make
scp stress puzzler:/home/teapot/stress
cd ../dbench

SSD_DRIVES="-f /dev/sdf"
HDD_DRIVES="" # TODO
SSD_RAID_DEVICE="/dev/sdf"
# Set the I/O scheduler to noop
for DRIVE in sdf; do
    sudo set-scheduler $DRIVE noop
done

# NOTE: Expects /dev/sdf to be a RAID of SSD drives
sudo mkfs.ext3 -F "$SSD_RAID_DEVICE"
mkdir -p /tmp/ssd_mount
sudo mount "$SSD_RAID_DEVICE" /tmp/ssd_mount
sudo mkdir -p /tmp/ssd_mount/mysql_data
sudo chmod uog+rwx /tmp/ssd_mount/mysql_data

MYSQL_SSD_CREATE_FLAGS="--innodb_data_home_dir=/tmp/ssd_mount/mysql_data --innodb_log_file_size=1G"
MYSQL_SSD_FLAGS="--innodb_data_home_dir=/tmp/ssd_mount/mysql_data --innodb_log_file_size=1G"

SERVER_HOSTS="magneto,magneto2"
CANONICAL_CLIENTS="96"
CANONICAL_DURATION="1800s"
CANONICAL_MULTIRUN_DURATION="300s"

MYSQL_COMMON_FLAGS="--disable-log-bin --skip-name-resolve --query_cache_size=0 --transaction_isolation=READ-UNCOMMITTED --innodb_support_xa=off --innodb_lock_wait_timeout=5 --innodb_flush_method=O_DIRECT --max-connections=1030 --max-prepared-stmt-count=32764 --innodb_log_group_home_dir=/tmp/ssd_mount/mysql_data"
MYSQL_BUFFER_FLAGS="--innodb_buffer_pool_size=32768m"
MYSQL_DURABILITY_FLAGS="--innodb_flush_log_at_trx_commit=0 --innodb_doublewrite=0"

STRESS_CLIENT="puzzler:/home/teapot/stress"

DATABASE="mysql"

# Remove old bench databases
rm -rf /usr/local/mysql/var/bench

#delete_database_files
rm -f /tmp/ssd_mount/mysql_data/ib_logfile?

# Initialize InnoDB RAW tablespace
/usr/local/mysql/libexec/mysqld $MYSQL_COMMON_FLAGS $MYSQL_BUFFER_FLAGS $MYSQL_DURABILITY_FLAGS $MYSQL_SSD_CREATE_FLAGS --port=9874 &
# Wait for server to finish initialization and shut it down
while ! /usr/local/mysql/bin/mysqladmin shutdown -u root --port=9874 2> /dev/null; do
    sleep 5
done

#sudo set-corecount 12


# Run workloads
for WORKLOAD in ../workloads/*; do
    if [ -d "$WORKLOAD" ]; then
        echo -e "\n\E[37;44m\033[1m<----- Running workload in $WORKLOAD ----->\033[0m\n"
        export BENCH_DIR
        export SSD_DRIVES
        export HDD_DRIVES
        export DATABASE
        export SERVER_HOSTS
        export CANONICAL_CLIENTS
        export CANONICAL_MULTIRUN_DURATION
        export CANONICAL_DURATION
        export MYSQL_COMMON_FLAGS
        export MYSQL_BUFFER_FLAGS
        export MYSQL_DURABILITY_FLAGS
        export MYSQL_SSD_FLAGS
        export MYSQL_SSD_CREATE_FLAGS
        export STRESS_CLIENT
        
        # Drop potentially existing bench tables before continueing
        /usr/local/mysql/libexec/mysqld $MYSQL_COMMON_FLAGS $MYSQL_BUFFER_FLAGS $MYSQL_DURABILITY_FLAGS $MYSQL_SSD_FLAGS --port=9874 &
        # Wait for server to finish initialization and shut it down
        while ! echo exit | /usr/local/mysql/bin/mysql -u root --port=9874 2> /dev/null ; do
            sleep 5
        done
        echo "drop table bench if exists;" | /usr/local/mysql/bin/mysql -u root --port=9874
        echo "drop database if exists bench;" | /usr/local/mysql/bin/mysql -u root --port=9874
        /usr/local/mysql/bin/mysqladmin shutdown -u root --port=9874 2> /dev/null
    
        if [ -e "$WORKLOAD/Setup" ]; then
            "$WORKLOAD/Setup"
        fi
        
        for CONFIGURATION in $( find "$WORKLOAD" -maxdepth 1 -type f -name "R*" | sort ); do
            echo -e "\n\033[1mRunning $CONFIGURATION\033[0m"
            if [ -e "$BENCH_DIR/environment" ]; then
                . "$BENCH_DIR/environment"
            fi
            $CONFIGURATION
        done
        
        if [ -e "$WORKLOAD/Teardown" ]; then
            if [ -e "$BENCH_DIR/environment" ]; then
                . "$BENCH_DIR/environment"
            fi
            "$WORKLOAD/Teardown"
        fi
        
        if [ -e "$BENCH_DIR/environment" ]; then
            rm -f "$BENCH_DIR/environment"
        fi
    fi
done

sudo set-corecount 24


# Now that all benchmarks are done, delete any database files that might have been left over
sudo umount /tmp/ssd_mount

