Sysbench Tutorial: MySQL and I/O Benchmark

Sysbench is a system benchmark tool which can run Lua scripts and measure MySQL and file I/O performance. A number of Lua scripts are included by default after installing. You can run the scripts using sysbench to evaluate system performance such as database and storage I/O speed.

Creator of sysbench and the repository: https://github.com/akopytov/sysbench

#1) Review Sysbench Prerequisites

Check MySQL Status

To get started, make sure MySQL is up and running.

systemctl status mariadb

Check mariadb status
Check mariadb status

You can install mariadb if you haven’t installed MySQL.

systemctl install mariadb-server

Remember to run mysql_secure_installation and confirm that you can login the database after installing MySQL.

mysql_secure_installation

mysql_secure_installation
Perform mysql_secure_installation

mysql -u “user” -p

Check mysql database through login
Login and check MySQL database

To allow sysbench to insert records in the database, create a database titled “test” after logging in the database.

Create a database named test for sysbench
Create a database named test for sysbench

The database preparation task has been completed.

Install Sysbench

Installing sysbench is as easy as running the standard apt / yum command:

Ubuntu: apt install sysbench

CentOS / RHEL: yum install sysbench

#2) Measure MySQL Performance using Sysbench

There are three major steps to run sysbench, namely prepare, run and cleanup.

Step 1: Prepare

Firstly, sysbench will need to create a table and insert records into the table through the “prepare” command. The content inside the table will be used for system performance evaluation later – by performing database queries such as read and write.

sysbench oltp_read_write –table-size=1000000 –db-driver=mysql –mysql-db=test –mysql-user=root –mysql-password=P@ssw0rd prepare

Sysbench oltp_read_write prepare
sysbench oltp_read_write prepare

After running the sysbench preparation script, a table named “sbtest1” will be created inside the “test” database.

Sysbench database structure
sysbench database structure

Since we have specified the table size to be 1000000, the records will be generated in the database table.

Sysbench database number of records
sysbench database number of records

Looking into the table, you can find the content created by the script, which will be used by sysbench to perform read and write queries for database performance measurement.

Sample sysbench database table records
Sample sysbench database table records

Step 2: Run

Run the sysbench script and the result will then be generated on the console output.

sysbench oltp_read_write –table-size=1000000 –db-driver=mysql –mysql-db=test –mysql-user=root –mysql-password=P@ssw0rd run

Sysbench oltp_read_write run
sysbench oltp_read_write run (10s)

Note that the total execution time of the script is 10s by default, unless the “–time” parameter is specified.

sysbench oltp_read_write –time=20 –table-size=1000000 –db-driver=mysql –mysql-db=test –mysql-user=root –mysql-password=P@ssw0rd run

Sysbench oltp_read_write run (20s)
sysbench oltp_read_write run (20s)

Note that the CPU usage of mysqld and sysbench process should be high when the benchmark is running.

CPU Usage when running sysbench
CPU Usage when running sysbench

Step 3: Clean up

After getting the benchmark result, you can run the built-in cleanup command to drop the table(s) created by sysbench.

sysbench oltp_read_write –db-driver=mysql –mysql-db=test –mysql-user=root –mysql-password=P@ssw0rd cleanup

Sysbench oltp_read_write cleanup
sysbench oltp_read_write cleanup

#3) Measure I/O Performance using Sysbench

Likewise, you can measure system I/O performance using sysbench by following the three steps procedure: prepare, run and cleanup.

Step 1: Prepare

First, we create 10 files, each of 10GB.

sysbench fileio –file-num=10 –file-total-size=10G prepare

Create 10 files each of 10GB using sysbench
Create 10 files each of 10GB using sysbench
A total of 10 files will be created. Inspect the files before running the test.
A total of 10 files will be created. Inspect the files before running the test.

For more information on the test cases available regarding sysbench file I/O performance test, please refer to the manual by entering “sysbench fileio help“.

Sysbench fileio manual
Sysbench fileio manual

Step 2: Run

Run the file I/O test after confirming that files are created. Sysbench will use the files created for I/O performance measurement. Remember to specify the “file-test-mode” parameter.

sysbench fileio –file-num=10 –file-total-size=10G –file-test-mode=rndrw run

A sample result of running the fileio test using sysbench
A sample result of running the fileio test using sysbench

Step 3: Cleanup

Upon the completion of the test case, you can delete the files created by sysbench easily by using the built-in cleanup feature.

sysbench fileio –file-num=10 –file-total-size=10G –file-test-mode=rndrw cleanup

Sysbench fileio cleanup
sysbench fileio cleanup and the cleanup result