#!/bin/bash
set -x -v
: ${SRCTREE=/mariadb/10.5m}
: ${MDIR=/dev/shm/10.5m}
IDIR=$BLDTREE/include
: ${TDIR=/tmp/sbtest}
LD_LIBRARY_PATH="$MDIR/libmysql"
MYSQL_SOCK=$TDIR/mysqld.sock
MYSQL_USER=root
BENCH_TIME=1200
BENCH_THREADS="8"
# Frequently used tests: oltp_update_index, oltp_update_non_index, oltp_read_write, connect, oltp_read_only, oltp_point_select
SYSBENCH="sysbench /usr/share/sysbench/oltp_update_index.lua \
  --mysql-socket=$MYSQL_SOCK \
  --mysql-user=$MYSQL_USER \
  --mysql-db=test \
  --percentile=99 \
  --tables=8 \
  --table_size=200000"
PFS=off
  #--mysql_storage_engine=aria --point_selects=1000 --simple_ranges=0 --sum_ranges=0 --order_ranges=0 --distinct_ranges=0 \
  #--aria-pagecache-buffer-size=2048M \
#rm -rf "$TDIR"
#cd $MDIR
mysql_install_db --user="$USERNAME" --datadir="$TDIR" --auth-root-authentication-method=normal
#cd ../
numactl --cpunodebind 0 --localalloc \
mysqld --no-defaults --gdb --core-file --loose-debug-sync-timeout=300 --innodb \
  --datadir="$TDIR"  --socket=$MYSQL_SOCK \
  --innodb_log_file_size=256M\
  --innodb_buffer_pool_size=8G \
  --innodb_io_capacity=70000\
  --innodb_io_capacity_max=90000 \
  --innodb_flush_log_at_trx_commit=0 \
  --innodb_adaptive_flushing_lwm=0 \
  --innodb_file-per-table=1 \
  --innodb-fast-shutdown=0 \
  --innodb-flush-method=O_DIRECT \
  --innodb_flush_sync=1 \
  --innodb_lru_scan_depth=1024 \
  --innodb_lru_flush_size=256 \
\
  --max-connections=2048 \
  --table_open_cache=4096 \
  --max_prepared_stmt_count=1048576 \
  --aria-checkpoint-interval=0 \
\
  --loose-performance-schema-consumer-events_waits_current=on \
  --loose-performance-schema-consumer-events_statements_current=off \
  --loose-performance-schema-consumer-statements_digest=off \
  --loose-performance-schema-instrument='%=off' \
  --loose-performance-schema-instrument='wait/synch/mutex/%=on' \
  --loose-performance-schema-instrument='wait/synch/rwlock/%=on' \
  --loose-performance-schema=$PFS > "$TDIR"/mysqld.err 2>&1 &
timeo=600
echo -n "waiting for server to come up "
while [ $timeo -gt 0 ]
do
  mysqladmin -S $MYSQL_SOCK -u $MYSQL_USER -b -s ping && break
  echo -n "."
  timeo=$(($timeo - 1))
  sleep 1
done

if [ $timeo -eq 0 ]
then
  echo " server not starting! Abort!"
  break
fi

numactl --cpunodebind 0 --localalloc $SYSBENCH --threads=$BENCH_THREADS cleanup || echo already clean
numactl --cpunodebind 0 --localalloc $SYSBENCH --threads=$BENCH_THREADS prepare

for i in $BENCH_THREADS
do
  mysql --no-defaults -u $MYSQL_USER -S $MYSQL_SOCK -e "truncate table events_waits_summary_global_by_event_name" performance_schema
  numactl --cpunodebind 0 --localalloc $SYSBENCH --rand-seed=42 --rand-type=uniform --max-requests=0 --time=$BENCH_TIME --report-interval=5 --threads=$i run
  [ "$PFS" = "off" ] ||
  mysql -u $MYSQL_USER -S $MYSQL_SOCK -e
  "select EVENT_NAME,COUNT_STAR,ROUND(SUM_TIMER_WAIT/1000000000000,2) AS SECONDS,SUM_TIMER_WAIT,MIN_TIMER_WAIT,AVG_TIMER_WAIT,MAX_TIMER_WAIT \
  from performance_schema.events_waits_summary_global_by_event_name order by sum_timer_wait desc limit 20" performance_schema
done

#$SYSBENCH cleanup
mysqladmin -u $MYSQL_USER -S $MYSQL_SOCK shutdown
