#!/bin/bash

# Takes the number of clients as its argument
function run_with_clients {
    NUMBER_OF_CLIENTS=$1
    
    
    # Create a database containing $DATABASE_SIZE values
    DATABASE_SIZE=3000000i

    #MEM_SIZE=59392 # EC2 quadruple memory extra large
    #MEM_SIZE=10360 # EC2 extra large
    MEM_SIZE=24000  # magneto

    if [ $DATABASE == "rethinkdb" ]; then
        ../../build/release/rethinkdb create $SSD_DRIVES --force
        #./dbench                                                                                      \
        #    -f -d "/tmp/zynga_insert" -H $SERVER_HOSTS    \
        #    {server}rethinkdb:"-m $MEM_SIZE $SSD_DRIVES"                                            \
        #    {client}stress[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $DATABASE_SIZE -v 10240-16384 -w 0/0/1/0 -o /tmp/keys" \
        #    iostat:1 vmstat:1 rdbstat:1
    elif [ $DATABASE == "mysql" ]; then
        if [ "$NUMBER_OF_CLIENTS" -lt "1025" ]; then
            ./dbench                                                                                   \
                -f -d "/tmp/zynga_insert" -H $SERVER_HOSTS \
                {server}mysql:"$MYSQL_COMMON_FLAGS --innodb_buffer_pool_size=${MEM_SIZE}m $MYSQL_DURABILITY_FLAGS $MYSQL_SSD_FLAGS"        \
                {client}mysqlstress[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $DATABASE_SIZE -v 10240-16000 -w 0/0/1/0 -o /tmp/keys" \
                iostat:1 vmstat:1
        fi
    elif [ $DATABASE == "membase" ]; then
        #./dbench                                                                                   \
        #    -f -d "/tmp/zynga_insert" -H $SERVER_HOSTS -p 11211 \
        #    {server}membase:"-d $MEMBASE_DATA_PATH -m ${MEM_SIZE}"                                       \
        #    {client}stress[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $DATABASE_SIZE -v 10240-16384 -w 0/0/1/0 -o /tmp/keys" \
        #    iostat:1 vmstat:1
        echo foo
    fi
    
    # Kill the page cache!
    sync
    sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches'
    sync
    sleep 30
    sudo sh -c 'echo 0 > /proc/sys/vm/drop_caches'
    
    # Run the benchmark
    if [ $DATABASE == "rethinkdb" ]; then
        ./dbench                                                                                       \
            -d "$BENCH_DIR/bench_output/Zynga_workload/$NUMBER_OF_CLIENTS" -H $SERVER_HOSTS    \
            {server}rethinkdb:"-m ${MEM_SIZE} $SSD_DRIVES"                                            \
            -i {insert}stressinsert[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $DATABASE_SIZE -v 10240-16384 -w 0/0/1/0 -o /tmp/keys" \
            {client}stress[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $CANONICAL_MULTIRUN_DURATION -v 10240-16384 -w 1/90/1/8 -i /tmp/keys" \
            iostat:1 vmstat:1 rdbstat:1
    elif [ $DATABASE == "mysql" ]; then
        if [ "$NUMBER_OF_CLIENTS" -lt "1025" ]; then
            ./dbench                                                                                    \
                -d "$BENCH_DIR/bench_output/Zynga_workload/$NUMBER_OF_CLIENTS" -H $SERVER_HOSTS \
                {server}mysql:"$MYSQL_COMMON_FLAGS --innodb_buffer_pool_size=${MEM_SIZE}m $MYSQL_DURABILITY_FLAGS $MYSQL_SSD_FLAGS"        \
                {client}mysqlstress[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $CANONICAL_MULTIRUN_DURATION -v 10240-16000 -w 1/90/1/8 -i /tmp/keys" \
                iostat:1 vmstat:1
        else
            echo "Not running MySQL for more than 1024 clients"
        fi
    elif [ $DATABASE == "membase" ]; then
        ./dbench                                                                                        \
            -d "$BENCH_DIR/bench_output/Zynga_workload/$NUMBER_OF_CLIENTS" -H $SERVER_HOSTS -p 11211 \
            {server}membase:"-d $MEMBASE_DATA_PATH -m ${MEM_SIZE}"                                       \
            -i {insert}stressinsert[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $DATABASE_SIZE -v 10240-16384 -w 0/0/1/0 -o /tmp/keys" \
            {client}stress[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $CANONICAL_MULTIRUN_DURATION -v 10240-16384 -w 1/90/1/8 -i /tmp/keys" \
            iostat:1 vmstat:1
    else
        echo "No workload configuration for $DATABASE"
    fi
}

