MySQL remains one of the most popular relational databases due to its robustness, scalability, and ease of use. However, optimizing MySQL for better performance becomes crucial as your database grows and your system demands increase. Using Sysbench for MySQL benchmarking is a great way to evaluate the performance of your MySQL database under various workloads. Here’s a step-by-step guide to help you get started.

Step 1: Install Sysbench

To get started, you need to install Sysbench and MySQL on your system. Sysbench can be easily installed using package managers like apt, yum, and dnf.

For RHEL/CentOS:

$ sudo yum install epel-release
$ sudo yum install sysbench

For Ubuntu/Debian:

$ sudo apt update
$ sudo apt install sysbench

Once installed, create a test database and user in MySQL to be used by sysbench for benchmarking purposes. Grant the user the necessary permission to perform the required operations.

Step 2: Pick a Test 

Sysbench offers several types of tests to evaluate different aspects of MySQL performance.

Here are the main categories of tests you can run:

OLTP Tests

These simulate online transaction processing workloads and include:

  • OLTP Read-Only: Measures the performance of read operations.
  • OLTP Read-Write: Measures performance with both read and write operations.
  • OLTP Write-Only: Focuses solely on write operations.
  • OLTP Point Select: Tests the performance of point-select queries.
  • OLTP Mixed: A combination of various read and write operations.

File I/O Tests

These measure file input/output performance, which is crucial for understanding how well the underlying storage system performs:

  • File I/O: Simulates various read and write operations on files. You can customize the test to simulate sequential or random access patterns.

CPU Tests

These focus on the CPU performance and can help identify CPU bottlenecks:

  • CPU Benchmark: Measures the performance of CPU-bound tasks. It runs mathematical calculations to assess CPU throughput.

Memory Tests

These tests evaluate memory allocation and access performance:

  • Memory Benchmark: Measures how efficiently memory is allocated and accessed, which can help tune database configurations.

Below is a comparison table summarizing the different Sysbench MySQL tests, their focus areas, and typical use cases:

Test TypeFocus AreaUse Case
OLTP Read-OnlyRead PerformanceEvaluating performance for applications primarily reading data.
OLTP Read-WriteOverall Transaction LoadTesting real-world scenarios where both reads and writes occur.
OLTP Write-OnlyWrite PerformanceAssessing the write capability of the database under load.
OLTP Point SelectQuery PerformanceMeasuring how quickly single records can be retrieved.
OLTP MixedMixed WorkloadsSimulating complex workloads typical in multi-user environments.
File I/OI/O PerformanceEvaluating the performance of the underlying storage system.
CPU BenchmarkCPU PerformanceIdentifying CPU bottlenecks during heavy processing tasks.
Memory BenchmarkMemory PerformanceTesting the efficiency of memory operations in the database.

Step 3: Run the Benchmark

Most tests have these basic steps:

  1. Prepare: configuring the test per desired parameters. Each test has its unique preparation actions.
  2. Run: running the specified test by specifying the test as an option. The syntax below is an example of running a cpu test.
  3. Cleanup: remove all the temporary files and data produced by the test. 

Example Commands for Each Test Type:

OLTP Write-Only:

$ sysbench oltp_write_only --threads=50 --mysql-db=db_name --mysql-user=user --mysql-password=pass prepare

$ sysbench oltp_write_only --threads=50 --mysql-db=db_name --mysql-user=user --mysql-password=pass run

$ sysbench oltp_write_only --threads=50 --mysql-db=db_name --mysql-user=user --mysql-password=pass cleanup

File I/O:

$ sysbench fileio --file-total-size=1G prepare

$ sysbench fileio --file-test-mode=rndrd --file-total-size=1G --time=60 --threads=4 run

$ sysbench fileio --file-test-mode=rndrd --file-total-size=1G --time=60 --threads=4 cleanup

CPU Benchmark:

$ sysbench cpu --cpu-max-prime=20000 run

Memory Benchmark:

$ sysbench memory --memory-block-size=1K --memory-total-size=10G run

For more information on each test, run the help command:

sysbench <test_name> help

Conclusion

Sysbench is a powerful tool for benchmarking MySQL performance. You can customize tests to simulate different workloads and configurations to find bottlenecks and optimize performance. Always analyze the results carefully to make informed decisions based on your collected data.

Contact us for any questions.