#!/bin/bash

# Config TiDB settings 
mysql -h ${tidb_lb} -P 4000 -u root \
-e "set global tidb_hashagg_final_concurrency=1;" \
-e "set global tidb_hashagg_partial_concurrency=1;" \
-e "set global tidb_enable_async_commit = 1;" \
-e "set global tidb_enable_1pc = 1;" \
-e "set global tidb_guarantee_linearizability = 0;" \
-e "set global tidb_enable_clustered_index = 1;" \
-e "create database tpcc"

# Import the TPC-C 5000 Warehouse data
tiup bench tpcc prepare --warehouses 1000 --db tpcc -H ${tidb_lb} -P 4000 -U root -T 200 

# backup test data to s3
/home/centos/.tiup/bin/tiup br backup db \
    --db tpcc \
    --pd ${dashboard_ip}:2379 \
    --storage "s3://${s3_bucket}/tpcc_backup" \
    --s3.region "${region}" \
    --send-credentials-to-tikv=false \
    --log-file backuptable.log 

# create result file
touch tpcc_result

# Run test
for threads in 100 700
do
    echo "############### TPCC test, thread number $threads " >> tpcc_result
    # run test
    tiup bench tpcc run -U root --db tpcc \
        --host ${tidb_lb} \
        --port 4000 \
        --time 300s \
        --warehouses 1000 \
        -T $threads | grep 'tpmC' | tee -a tpcc_result

    # drop database
    mysql -h ${tidb_lb} -P 4000 -u root -e "drop database tpcc;"

    # restore test data
    /home/centos/.tiup/bin/tiup br restore db \
        --db tpcc \
        --pd ${dashboard_ip}:2379 \
        --storage "s3://${s3_bucket}/tpcc_backup" \
        --s3.region "${region}" \
        --send-credentials-to-tikv=false \
        --log-file backuptable.log 
done

# cleanup test data from tidb database
mysql -h ${tidb_lb} -P 4000 -u root -e "drop database tpcc;"

# cleanup backup data in s3
aws s3 rm --recursive s3://${s3_bucket}/tpcc_backup
