#! /bin/bash

###
# #%L
# run-tests.sh - mongodb-async-driver - Allanbank Consulting, Inc.
# %%
# Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
# %%
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# #L%
###

logdir="target/logs"
mongohost="192.168.1.15:27017"
suite="baseline_2.8.0-rc3_wt"

export JAVA_HOME=${HOME}/bin/jdk1.8.0_05

# Command to remove the previous run data but leave empty data files.
compact="db.usertable.remove();printjson(db.runCommand({'compact':'usertable','force':true}));"
#compact="db.usertable.drop();"

mkdir -p ${logdir}

threadConnMap[1]="1 2 5 10 20 25"
threadConnMap[2]="2 5 10 15 20 25 50"
threadConnMap[5]="10 25 50 75 100"
threadConnMap[10]="10 15 20 30 40 50 75 100 125"
threadConnMap[25]="25 30 40 50 75 100 125 150"
threadConnMap[50]="50 75 100 125 150 175 200"

for workload in workloada workloadb workloadc workloadd workloade workloadf ; do
  for connections in 1 2 5 10 25 50 ; do
    for threads in ${threadConnMap[${connections}]} ; do
      for mode in mongodb-async mongodb ; do
      #for mode in mongodb mongodb-async targeted; do
        #
        # Remove all documents and compact (avoid re-disk allocation)."
        #
        mongo --eval "${compact}" ${mongohost}/ycsb >> /dev/null 2>&1 

        driver=${mode}
        unset MONGODB_SHARDING_MODE
        if [ "$mode" == "targeted" ] ; then
          export MONGODB_SHARDING_MODE=targeted
          driver=mongodb-async
        fi
      
        #
        # Load the data set.
        #
        now=$( date --utc "+%F %H:%M:%S" )
        printf "%s: Load: %9s with %-13s thread=%3s conns=%2s" "${now}" ${workload} ${mode} ${threads} ${connections}
      
        let start=$( date --utc "+%s" )
        ./bin/ycsb load ${driver} -s -P workloads/${workload} -threads ${threads}            \
                 -p "mongodb.url=mongodb://${mongohost}/ycsb?w=1&maxPoolSize=${connections}&waitQueueMultiple=${threads}" \
                 >  ${logdir}/${workload}-${mode}-threads_${threads}-conns_${connections}-load-${suite}.out \
                 2> ${logdir}/${workload}-${mode}-threads_${threads}-conns_${connections}-load-${suite}.err
        let end=$( date --utc "+%s" )
        let delta=end-start
        printf " --> %10d seconds\n" ${delta}           
      
        #
        # Run the workload.
        #
        now=$( date --utc "+%F %H:%M:%S" )
        printf "%s:  Run: %9s with %-13s thread=%3s conns=%2s" "${now}" ${workload} ${mode} ${threads} ${connections}
      
        let start=$( date --utc "+%s" )
        ./bin/ycsb run ${driver} -s -P workloads/${workload} -threads ${threads}             \
                 -p "mongodb.url=mongodb://${mongohost}/ycsb?w=1&maxPoolSize=${connections}&waitQueueMultiple=${threads}" \
                  >  ${logdir}/${workload}-${mode}-threads_${threads}-conns_${connections}-run-${suite}.out \
                  2> ${logdir}/${workload}-${mode}-threads_${threads}-conns_${connections}-run-${suite}.err                  
        let end=$( date --utc "+%s" )
        let delta=end-start
        printf " --> %10d seconds\n" ${delta}           
      done
    done
  done
done
