#!/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=7000000i
    
    #MEM_SIZE=59392     # For EC2 quadruple large memory
    #MEM_SIZE=11000     # For EC2 large memory
    MEM_SIZE=24000      # For local tests

    if [ $DATABASE == "rethinkdb" ]; then
        ../../build/release/rethinkdb create $SSD_DRIVES --force -s 120
        #./dbench                                                                                      \
        #    -f -d "/tmp/netflix_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 
    elif [ $DATABASE == "mysql" ]; then
        if [ "$NUMBER_OF_CLIENTS" -lt "1025" ]; then
            ./dbench                                                                                   \
                -f -d "/tmp/netflix_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 -k 10-100 -v 3-10240 -w 0/0/1/0 -o /tmp/keys" \
                iostat:1 vmstat:1
        fi
    elif [ $DATABASE == "membase" ]; then
        #./dbench                                                                                   \
        #    -f -d "/tmp/netflix_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 -k 10-100 -v 3-10240 -w 0/0/1/0 -o /tmp/keys" \
        #    iostat:1 vmstat:1
	echo foo
    fi
    
    # Run the benchmark
    if [ $DATABASE == "rethinkdb" ]; then
        ./dbench                                                                                      \
            -d "$BENCH_DIR/bench_output/Netflix_workload" -H $SERVER_HOSTS    \
            {server}rethinkdb:"-m ${MEM_SIZE} $SSD_DRIVES --flush-timer 1000"                                            \
            {client}stress[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $CANONICAL_DURATION -k 10-100 -v 3-10240 -w 2/26/2/200 -i /tmp/keys" \
            -i {insert}stressinsert[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $DATABASE_SIZE -k 10-100 -v 3-10240 -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                                                                                   \
                -d "$BENCH_DIR/bench_output/Netflix_workload" -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_DURATION -k 10-100 -v 3-10240 -w 2/26/2/200 -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/Netflix_workload" -H $SERVER_HOSTS -p 11211 \
            {server}membase:"-d $MEMBASE_DATA_PATH -m ${MEM_SIZE}"                                       \
            {client}stress[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $CANONICAL_DURATION -k 10-100 -v 3-10240 -w 2/26/2/200 -i /tmp/keys" \
            -i {insert}stressinsert[$STRESS_CLIENT]:"-c $NUMBER_OF_CLIENTS -d $DATABASE_SIZE -k 10-100 -v 3-10240 -w 0/0/1/0 -o /tmp/keys" \
            iostat:1 vmstat:1
    else
        echo "No workload configuration for $DATABASE"
    fi
}

