#!/bin/bash

mkdir -p $BENCH_DIR/bench_output/Number_of_cores_select_only_workload
echo CPU Cores > $BENCH_DIR/bench_output/Number_of_cores_select_only_workload/multirun

# Fake a 0-cores result for nicer graphs
mkdir -p $BENCH_DIR/bench_output/Number_of_cores_select_only_workload/0/1/client
echo -e "1\t\t0" > $BENCH_DIR/bench_output/Number_of_cores_select_only_workload/0/1/client/qps.txt

if [ $DATABASE == "rethinkdb" ]; then
    ../../build/release/rethinkdb create $SSD_DRIVES --force
fi

if [ $DATABASE == "mysql" ]; then
    sudo bash -c "for CPU in {0..23}; do echo 1 > /sys/devices/system/cpu/cpu\$CPU/online; done"
fi

# Store keys in temporary file.
export TMP_KEY_FILE="$(ssh puzzler mktemp)"

export -p > "$BENCH_DIR/environment"


if [ $DATABASE == "rethinkdb" ]; then
    ./dbench --force                                                                                        \
        -d "/tmp/insert_setup_out" -H $SERVER_HOSTS            \
        {server}rethinkdb:"-c 12 -m 32768 $SSD_DRIVES"                                              \
        {client}stress[$STRESS_CLIENT]:"-b 8-32 -v 8-32 -c 1024 -d 2000000i -w 0/0/1/0 -o $TMP_KEY_FILE"     \
        iostat:1 vmstat:1 rdbstat:1
elif [ $DATABASE == "mysql" ]; then
    ./dbench --force                                                                                         \
        -d "/tmp/insert_setup_out" -H $SERVER_HOSTS             \
        {server}mysql:"$MYSQL_COMMON_FLAGS $MYSQL_BUFFER_FLAGS $MYSQL_DURABILITY_FLAGS  $MYSQL_SSD_FLAGS"              \
        {client}mysqlstress[$STRESS_CLIENT]:"-b 8-32 -v 8-32 -c 128 -d 2000000i -w 0/0/1/0 -o $TMP_KEY_FILE" \
        iostat:1 vmstat:1
fi
