#!/bin/bash
: ${SRCTREE=/mariadb/10.6}
: ${MDIR=/dev/shm/10.6g}
IDIR=$BLDTREE/include
: ${TDIR=/dev/shm/sbtest}
LD_LIBRARY_PATH="$MDIR/libmysql"
MYSQL_SOCK=$TDIR/mysqld.sock
MYSQL_USER=root
BENCH_TIME=60
BENCH_THREADS="20"
# 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_read_write.lua \
  --mysql-socket=$MYSQL_SOCK \
  --mysql-user=$MYSQL_USER \
  --mysql-db=test \
  --percentile=99 \
  --tables=1 \
  --table_size=20"
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
sh scripts/mysql_install_db --user="$USERNAME" --srcdir="$SRCTREE" --builddir=. --datadir="$TDIR" --innodb-page-size=4k --auth-root-authentication-method=normal
cd ../
numactl --cpunodebind 1 --localalloc \
$MDIR/sql/mysqld --no-defaults --gdb --core-file --innodb \
  --datadir="$TDIR" --lc-messages-dir=./$MDIR/sql/share/ --socket=$MYSQL_SOCK \
  --log-bin=1 \
\
  --sync-binlog=0 --innodb-flush-log-at-trx-commit=0 \
  --innodb_log_file_size=100M\
  --innodb_buffer_pool_size=100M \
  --innodb_io_capacity=100\
  --innodb_io_capacity_max=20000 \
  --innodb_adaptive_flushing_lwm=0 \
  --innodb-adaptive-flushing=1 \
  --innodb_flush_neighbors=1 \
  --innodb_file-per-table=1 \
  --innodb-page-size=4k \
  --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
  $MDIR/client/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 prepare

$MDIR/client/mysql --no-defaults -u $MYSQL_USER -S $MYSQL_SOCK -e \
"alter table sbtest1 add column(pkpad1 char(255) not null default '',pkpad2 char(255) not null default '',pkpad3 char(100) not null default ''), drop primary key, add primary key(id,pkpad1,pkpad2,pkpad3), default charset utf8" test

for i in $BENCH_THREADS
do
  $MDIR/client/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" ] ||
  $MDIR/client/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
$MDIR/client/mysqladmin -u $MYSQL_USER -S $MYSQL_SOCK shutdown
