#!/bin/bash

# Set up the directory
BENCH_DIR="$HOME/competitor_bench/Membase"
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

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

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

DATABASE="membase"


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/membase_data
sudo chmod uog+rwx /tmp/ssd_mount/membase_data

MEMBASE_DATA_PATH="/tmp/ssd_mount/membase_data"

# TODO: erlang appears to crash if CPUs are switched off!
#echo "To get the canonical hardware, we have to disable all but 12 CPUs."
#echo 'Please give me authorization to run sh -c "for CPU in 12 13 14 15 16 17 18 19 20 21 22 23; do echo 0 > /sys/devices/system/cpu/cpu\$CPU/online; done"'
#sudo sh -c "for CPU in 12 13 14 15 16 17 18 19 20 21 22 23; do echo 0 > /sys/devices/system/cpu/cpu\$CPU/online; done"

# 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_DURATION
        export CANONICAL_MULTIRUN_DURATION
        export MEMBASE_DATA_PATH
        export STRESS_CLIENT
    
        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
            epmd -kill
        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

#echo "We can turn on the CPUs again now..."
#echo 'Please give me authorization to run sh -c "for CPU in 12 13 14 15 16 17 18 19 20 21 22 23; do echo 1 > /sys/devices/system/cpu/cpu\$CPU/online; done"'
#sudo sh -c "for CPU in 12 13 14 15 16 17 18 19 20 21 22 23; do echo 1 > /sys/devices/system/cpu/cpu\$CPU/online; done"


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

