#!/bin/bash

/usr/bin/clear
/bin/echo "**************************************************************"
/bin/echo "*           Please run this script as root                   *"
/bin/echo "*                                                            *"
/bin/echo "*  Installing required package for Benchmark and other test  *"
/bin/echo "*                                                            *"
/bin/echo "*                        Please wait                         *"
/bin/echo "*                                                            *"
/bin/echo "**************************************************************"
/bin/sleep 4

# Enable EPEL and install packages
dnf install epel-release -y
/usr/bin/yum install -y https://repo.percona.com/yum/percona-release-latest.noarch.rpm
/usr/bin/yum install -y sysbench
/usr/bin/yum install -y curl

# Install MySQL
/usr/bin/yum install -y mysql
/usr/bin/yum install -y mysql-server.x86_64
/usr/bin/systemctl start mysqld.service
/usr/bin/mysql -u root -e "create database test"

# Start benchmarks
/usr/bin/clear
/bin/echo "**************************************************************"
/bin/echo "*            Performing Benchmark and other test             *"
/bin/echo "*                                                            *"
/bin/echo "*                        Please wait                         *"
/bin/echo "*                                                            *"
/bin/echo "**************************************************************"
/bin/sleep 2

/bin/echo "**************************************************************"
/bin/echo "*      The Storage r/w test writes 8GB of data files         *"
/bin/echo "*                                                            *"
/bin/echo "*      Please make sure you have 8GB space available         *"
/bin/echo "*                                                            *"
/bin/echo "*               Else this test will fail                     *"
/bin/echo "*                                                            *"
/bin/echo "**************************************************************"
/bin/sleep 6

# Prepare log
/usr/bin/touch /benchmark.log
/usr/bin/cat /dev/null > /benchmark.log

# 1) Disk Read/Write Test
/bin/echo "**************************************************************" >> /benchmark.log
/bin/echo "*          Disk Read/Write Test and Benchmarking             *" >> /benchmark.log
/bin/echo "**************************************************************" >> /benchmark.log
/usr/bin/sysbench fileio --file-total-size=8G --file-num=24 prepare

/usr/bin/clear
/bin/echo "**************************************************************"
/bin/echo "*              Please wait while test is running             *"
/bin/echo "**************************************************************"
/usr/bin/sysbench fileio \
  --file-total-size=8G \
  --file-num=24 \
  --file-test-mode=rndrw \
  --time=60 \
  --file-rw-ratio=1 \
  --threads=16 \
  --max-requests=0 \
  run >> /benchmark.log
/usr/bin/rm -rf test_file*

# 2) CPU speed test
/bin/echo "**************************************************************" >> /benchmark.log
/bin/echo "*                  CPU speed test Benchmarking               *" >> /benchmark.log
/bin/echo "**************************************************************" >> /benchmark.log
cpu=`/usr/bin/nproc`
/usr/bin/sysbench cpu --threads=$cpu run >> /benchmark.log

# 3) MySQL Benchmark
/bin/echo "**************************************************************" >> /benchmark.log
/bin/echo "*                       Mysql Benchmark                      *" >> /benchmark.log
/bin/echo "**************************************************************" >> /benchmark.log
sysbench --db-driver=mysql --mysql-user=root --mysql-db=test \
  --tables=16 \
  --table-size=100000 \
  /usr/share/sysbench/oltp_read_write.lua prepare

/usr/bin/clear
/bin/echo "**************************************************************"
/bin/echo "*              Please wait while test is running             *"
/bin/echo "**************************************************************"
sysbench --db-driver=mysql --mysql-user=root --mysql-db=test \
  --tables=16 \
  --table-size=100000 \
  --threads=8 \
  --time=60 \
  --events=0 \
  --rate=2000 \
  /usr/share/sysbench/oltp_read_write.lua run >> /benchmark.log

/usr/bin/mysql -u root -e "drop database test"

# 4) RAM Memory Test (NEW)
/bin/echo "**************************************************************" >> /benchmark.log
/bin/echo "*                     RAM Memory Test                        *" >> /benchmark.log
/bin/echo "**************************************************************" >> /benchmark.log
sysbench memory --threads=$cpu --time=10 run >> /benchmark.log

# Final Display in Boxed Format
/usr/bin/clear

#########################################################################
# We enforce a fixed width of 62 characters total for each box line:     #
#   - Top line: 62 underscores                                          #
#   - Content lines: "|     %-50s     |"                                #
#   - Bottom line: "|____________________________________________________________|" #
#########################################################################

# 1) Disk Results
/bin/echo "______________________________________________________________"
printf "|     %-50s     |\n" "Disk Read/Write Test and Benchmarking"
printf "|     %-50s     |\n" ""
/usr/bin/cat /benchmark.log | grep -i  -e "MiB/s" |grep -i -e read -e written | while read -r line; do
    printf "|     %-50s     |\n" "$line"
done
/bin/echo "|____________________________________________________________|"
echo

# 2) CPU Results
/bin/echo "______________________________________________________________"
printf "|     %-50s     |\n" "CPU speed test Benchmarking"
printf "|     %-50s     |\n" ""
/usr/bin/cat /benchmark.log | grep "events per second" | while read -r line; do
    printf "|     %-50s     |\n" "$line"
done
/bin/echo "|____________________________________________________________|"
echo

# 3) MySQL Results
/bin/echo "______________________________________________________________"
printf "|     %-50s     |\n" "Mysql Benchmark 60 Second"
printf "|     %-50s     |\n" ""
/usr/bin/cat /benchmark.log | grep -A 30 "Mysql" | grep "total number of events" | while read -r line; do
    printf "|     %-50s     |\n" "$line"
done
/usr/bin/cat /benchmark.log | grep "FATAL: The event queue is full" | while read -r line; do
    printf "|     %-50s     |\n" "$line"
done
/bin/echo "|____________________________________________________________|"
echo

# 4) RAM Memory Test Results
/bin/echo "______________________________________________________________"
printf "|     %-50s     |\n" "RAM Memory Test"
printf "|     %-50s     |\n" ""
/usr/bin/cat /benchmark.log \
  | grep -A30 "RAM Memory Test" \
  | grep -E "MiB|transferred" \
  | sed 's/^[[:space:]]*//' \
  | while read -r line
do
    printf "|     %-50s     |\n" "$line"
done
/bin/echo "|____________________________________________________________|"
echo

# Final message
/bin/echo "**************************************************************"
/bin/echo "* To review the full Benchmark report check  /benchmark.log  *"
/bin/echo "**************************************************************"

