#!/usr/bin/env bash

PGHOME=/var/lib/loongdb 
PGUSER=loongdb
export PATH=/usr/local/loongdb/loongdb-1/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/loongdb/loongdb-1/lib:$LD_LIBRARY_PATH
export PGDATA=/var/lib/loongdb/1/data
export BMSQL_HOME=./benchmarksql-5.0/run/

cd $BMSQL_HOME
LOGDIR=log

cpuCount=$(cat /proc/cpuinfo | grep processor | wc -l)

function runTPCC()
{

    [[ -f $LOGDIR ]] && mv $LOGDIR $LOGDIR_bak
    mkdir -p $LOGDIR

    genConf $1
    time_str=$(date "+%Y%m%d%H%M%S")

    logfile=$LOGDIR/test.$1.out.$time_str
    echo "runDatabaseDestroy..."
    #numactl --interleave=all  bash ./runDatabaseDestroy.sh props.vb > $logfile
    bash ./runDatabaseDestroy.sh props.vb > $logfile
    sleep 10

    echo "Stop Database..."
    su - $PGUSER -c "/usr/local/loongdb/loongdb-1/bin/pg_ctl stop -D $PGDATA" >> $logfile
    sleep 20

    echo 3 > /proc/sys/vm/drop_caches
    sleep 30

    echo "Start Database..."
    #su - $PGUSER -c "numactl --interleave=all pg_ctl start" >> $logfile
    su - $PGUSER -c "/usr/local/loongdb/loongdb-1/bin/pg_ctl start -D $PGDATA" >> $logfile

    sleep 30
    echo "runDatabaseBuild..."
    bash ./runDatabaseBuild.sh props.vb >> $logfile
    su - $PGUSER -c "/usr/local/loongdb/loongdb-1/bin/psql loongdb -Uloongdb -c \"checkpoint;\""

    sleep 180
    echo "runBenchmark..."
    bash ./runBenchmark.sh props.vb >> $logfile
}

function genConf()
{
    echo "db=postgres" > props.vb
    echo "driver=org.postgresql.Driver" >> props.vb
    echo "conn=jdbc:postgresql://localhost:5432/benchmark?ApplicationName=benchmark&prepareThreshold=1&batchMode=on&fetchsize=10" >> props.vb
    echo "user=benchmark" >> props.vb
    echo "password=123456" >> props.vb
    echo "warehouses=100" >> props.vb
    echo "loadWorkers=${cpuCount}" >> props.vb
    echo "terminals=$1" >> props.vb
    echo "runTxnsPerTerminal=0" >> props.vb
    echo "runMins=5" >> props.vb
    echo "limitTxnsPerMin=0" >> props.vb
    echo "terminalWarehouseFixed=false" >> props.vb
    echo "newOrderWeight=45" >> props.vb
    echo "paymentWeight=43" >> props.vb
    echo "orderStatusWeight=4" >> props.vb
    echo "deliveryWeight=4" >> props.vb
    echo "stockLevelWeight=4" >> props.vb
}

function RunAll()
{
#    runTPCC 500
#    runTPCC 500
    runTPCC 500
#    runTPCC 300
#    runTPCC 300
    runTPCC 300
#    runTPCC 100
#    runTPCC 100
    runTPCC 100
}


# unlog 表测试
#cp -f sql.common/tableCreates.sql.unlog sql.common/tableCreates.sql
#LOGDIR=$BMSQL_HOME/log/disk_unlog
#RunAll

cp -f sql.common/tableCreates.sql.bak sql.common/tableCreates.sql
LOGDIR=$BMSQL_HOME/log/disk_log
RunAll


# 内存盘测试

#su - $PGUSER -c "source $PGHOME/.bashrc; pg_ctl stop"
#sleep 10
#echo 3 > /proc/sys/vm/drop_caches
#sleep 10


#su - postgres -c " mv $PGHOME/loongdb-1/data /dev/shm/; ln -s /dev/shm/data $PGHOME/loongdb-1/data"

#cp -f sql.common/tableCreates.sql.bak sql.common/tableCreates.sql
#LOGDIR=$BMSQL_HOME/log/mem_log
#RunAll

#cp -f sql.common/tableCreates.sql.unlog sql.common/tableCreates.sql
#LOGDIR=$BMSQL_HOME/log/mem_unlog
#RunAll

#su - postgres -c "rm -rf $PGHOME/data ; mv /dev/shm/data $PGHOME/"

