#!/bin/sh
# - oltp_tables_count
# - oltp_table_size
# - nr_threads
# - rand_type
# - rand_spec_pct
# - runtime
# - report_interval
# - oltp_read_only
# - events
# mysql_user
# mysql_host
# mysql_port
# mysql_db
# mysql_password
# db_driver
# mysql_socket

: "${mysql_user:=root}"
: "${mysql_host:=$server}"
: "${mysql_port:=3306}"
: "${mysql_db:=sysbench_test}"
: "${mysql_password:=mysql_password}"
: "${db_driver:=mysql}"
: "${oltp_tables_count:=1000}"
: "${oltp_table_size:=100000}"
: "${rand_type:=special}"
: "${rand_spec_pct:=100}"
: "${nr_threads:=256}"
: "${runtime:=7200}"
: "${report_interval:=1}"
: "${oltp_read_only:=off}"
: "${events:=2000000000}"
: "${mysql_socket:=/data/mysql/run/mysql.sock}"

args1=(
	 --mysql-user=$mysql_user
	 --mysql-host=$mysql_host
 	 --mysql-port=$mysql_port
 	 --mysql-db=$mysql_db
	 --db-driver=$db_driver
	 --mysql-password=$mysql_password
	 --table-size=$oltp_table_size
	 --tables=$oltp_tables_count
	 --rand-type=$rand_type
	 --rand-spec-pct=$rand_spec_pct
	 --threads=$nr_threads
	 --time=$runtime
	 --mysql-socket=$mysql_socket
)

args2=(
	--mysql-user=$mysql_user
	--mysql-socket=$mysql_socket
	--mysql-password=$mysql_password
	--mysql-host=$mysql_host
	--mysql-port=$mysql_port
	--mysql-db=$mysql_db
	--threads=$nr_threads
	--oltp-read-only=$oltp_read_only
	--oltp-table-size=$oltp_table_size
	--oltp-tables-count=$oltp_tables_count
	--report-interval=$report_interval
	--time=$runtime
	--events=$events
)

stop_firewalld()
{
	systemctl stop firewalld >/dev/null 2>&1
	iptables -F >/dev/null 2>&1
}

run_sysbench_step()
{
	lua_script=$1
	shift
	sysbench /usr/local/share/sysbench/tests/include/oltp_legacy/$lua_script "$@"
}

run_sysbench_mysql()
{
	sysbench "${args1[@]}" oltp_read_write prepare > /dev/null

	run_sysbench_step oltp.lua "${args2[@]}" run

	run_sysbench_step oltp.lua "${args2[@]}" cleanup > /dev/null
}

stop_firewalld
run_sysbench_mysql
