#!/usr/bin/env sh
##

function f_PrintLog
{
  typeset _LogFlag=$1
  typeset LogFile="${LogFile:-"/tmp/${ShellName}.log"}"
  touch "${LogFile}" 2>/dev/null
  if [ -w "${LogFile}" ]
  then
    echo "[${_LogFlag}]:$(date "+%Y%m%d.%H%M%S"):${UserName}@${HostName}:${2}"|tee -a "${LogFile}";
  else
    echo "[${_LogFlag}]:$(date "+%Y%m%d.%H%M%S"):${UserName}@${HostName}:${2}";
  fi
}

function f_modify_conf_threads
{
    f_PrintLog "Info"  "Start run function f_modify_conf!!"
    typeset _thread_num=$1
    #typeset _config_file=$2
    typeset _t1=$(awk -F\= '/^threads/ {print $2;exit}' ${bench_config_file})
    if [ ${_t1} -eq ${_thread_num} ]
    then 
        f_PrintLog "Info" "config thread in ${bench_config_file} is ${_t1}, not need update"
    else
        f_PrintLog "Info" "config thread in ${bench_config_file} is ${_t1}, will update to ${_thread_num}"
        cp ${bench_config_file} ${bench_config_file}_bak
        rm -f ${bench_config_file}
        cat ${bench_config_file}_bak | sed "s/^threads=${_t1}.*/threads=${_thread_num}/g"  > ${bench_config_file}
    fi
}

function f_run_sysbench
{
    f_PrintLog "Info"  "****Start run function f_run_sysbench!!"
    typeset _test_case=$1
    typeset _t1=$(awk -F\= '/^threads/ {print $2;exit}' ${bench_config_file})
    f_PrintLog "Info"  "Run ${_test_case} with ${_t1} threads, start!!"
    f_PrintLog "Info"  "sysbench --config-file=${bench_config_file} ${_test_case} --tables=32 --table-size=10000000 run"
    echo "sysbench --config-file=${bench_config_file} ${_test_case} --tables=32 --table-size=10000000 run"
    sysbench --config-file=${bench_config_file} ${_test_case} --tables=32 --table-size=10000000 run |tee -a "${LogFile}"
    uptime |tee -a "${LogFile}"
    f_PrintLog "Info"  "****Run ${_test_case} with ${_t1} threads, finish!!"
}

function f_prepare_sbtest_tables
{
    f_PrintLog "Info"  "Start run function f_prepare_tables!!"
    typeset _ip=$(awk -F\= '/^mysql-host/ {print $2;exit}' ${bench_config_file})
    typeset _port=$(awk -F\= '/^mysql-port/ {print $2;exit}' ${bench_config_file})
    typeset _db=$(awk -F\= '/^mysql-db/ {print $2;exit}' ${bench_config_file})
    table_count=$(mysql -h${_ip} -P${_port} -uroot -NBe "select count(1) from information_schema.tables where table_schema='${_db}' and table_name like 'sbtest%'")
    if [ ${table_count} -eq 0 ]
    then 
        mysql -h${_ip} -P${_port} -uroot -NBe "create database ${_db}"
        echo "sysbench oltp_common --threads=16 --rand-type=uniform --db-driver=mysql --mysql-db=${_db} --mysql-host=${_ip} --mysql-port=${_por
t} --mysql-user=root --mysql-password= --tables=32 --table-size=10000000 prepare"
        sysbench oltp_common --threads=16 --rand-type=uniform --db-driver=mysql --mysql-db=${_db} --mysql-host=${_ip} --mysql-port=${_port} --m
ysql-user=root --mysql-password= --tables=32 --table-size=10000000 prepare |tee -a "${LogFile}"
    else
        f_PrintLog "Info"  "already ${table_count} tables in ${_db} , quit!"
    fi
}

###MAIN####
#default variable
ShellName="$(echo $0|awk -F / '{print $NF}')";
WorkDir="$(echo $0|sed s/${ShellName}//g)";[ -z "${WorkDir}" ] && WorkDir=${PWD};cd ${WorkDir};WorkDir=${PWD}
LogDir="${WorkDir}/logs";mkdir -p ${LogDir};chmod ugo+rwx ${LogDir} 2>/dev/null
LogFile="${LogDir}/${ShellName}.$(date +%Y%m%d.%H%M%S).log"
HostName=$(hostname);Platform=$(uname -s|tr a-z A-Z)
ShellOption="$@";

#shell variable
bench_config_file="confha01"
f_PrintLog "Info"  "Pls check config content ${bench_config_file}!!!"
cat ${bench_config_file}

#create sbtest tables
f_prepare_sbtest_tables

#run sysbench 
for testcase in  "oltp_point_select"
do 
    f_PrintLog "Info"  "********Start run test ${testcase}!"
    for thread in "16" "32" "64" "128" "256"
    do
        if [ "${testcase}" != "oltp_point_select" -a ${thread} -gt 2048 ]
        then
          f_PrintLog "Info"  "****Not run test ${testcase} with ${thread} threads."
        else
          f_PrintLog "Info"  "****Start run test ${testcase} with ${thread} threads."
          f_modify_conf_threads ${thread} 
          f_run_sysbench ${testcase}
        fi
        sleep 60
        f_PrintLog "Info"  " "
    done
    sleep 120
    f_PrintLog "Info"  " "
done

