sysbench is a scriptable multi-threaded benchmark tool based on LuaJIT.
On macOS, using Homebrew:
# Add --with-postgresql if you need PostgreSQL support
$ brew install sysbenchDebian/Ubuntu
$ curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.deb.sh | sudo bash
sudo apt -y install sysbenchNote: If Using sysbench benchmark for TiDB, you must install mysql-client v5.7.
$ brew install mysql-client@5.7This is an example of the Sysbench configuration file:
mysql-host={DB_HOST}
mysql-port=n
mysql-user=user
mysql-password=password
mysql-db=sbtest
time=600
threads={8, 16, 32, 64, 128, 256}
report-interval=10
db-driver=mysql
Example:
mysql-host=localhost
mysql-port=4000
mysql-user=root
mysql-password=password
mysql-db=sbtest
time=600
threads=16
report-interval=10
db-driver=mysql- If using PostgreSQL, replacing mysql to pgsql.
- See full config: https://github.com/akopytov/sysbench#general-command-line-options or run cmd sysbench --help.
- Define oltp_common.lua to contains shared scripts for other script. For each test we will define a script .lua.
- Lifecycle run benchmark:
-
Phases:
- Prepare: init database, table, rows...
- Warnup: run statment such as ANALYZE TABLE, SUMM, AVG, ... to warmup.
- Run: run benchmark.
- Clean: clean data.
-
Each phase, we define script .lua to run.
- Create database:
- Restart MySQL client and execute the following SQL statement to create a database sbtest:
$ mysql> create database sbtest;- Writing script oltp_prepare.lua to prepare data.
- Then, using cmd (stay on server) with
rangefollows a format (start_id, end_id). Suppose we want to prepare data on range (0,10000). That means it creates a table with size 10000:
#sysbench --config-file=config --test=./lua/oltp_prepare.lua --tables=$TABLE --table-size=$TABLE_SIZE prepare
$ cd ./tidb//benchmark
$ /run.sh -pp 0,10000Note:
- tables: number of tables will be created in database. It is the same with
TABLEflag inrun.shscript. - table-size: number of rows will be inserted in each table.
rangeparam will override thetable-sizeorTABLE_SIZEdefined inrun.sh.
- Writing script oltp_warmup.lua to warmup DB.
- Then, using cmd:
#sysbench --config-file=config --test=./lua/oltp_warmup.lua --tables=$TABLE --table-size=$TABLE_SIZE prewarm
$ cd ./tidb//benchmark
$ ./run.sh -wu- In Each test case, we need to write script .lua to benchmark. Example oltp_test.lua:
--oltp_test.lua
require("oltp_common")
function prepare_statements()
print("Call prepare_statements")
end
function event()
print("Call event")
end- Lifecyle call func when runing:
- function thread_init(): called by sysbench one time to initialize this script.
- Init global variable, thread, connection db, prepare statement,....
- Number of thread defined in file
config. - In function thread_init(), each thread call function prepare_statements().
- function event(): called by sysbench for each execution.
- function thread_done(): called by sysbench when script is done executing.
- Close connection DB,...
- function thread_init(): called by sysbench one time to initialize this script.
- Time to run benchmark defined in file
config.
Example of one config file for TiDB:
db-driver=mysql
mysql-host=localhost
mysql-port=4001
mysql-user=mybenchmark
mysql-password=abczyz
mysql-db=sbtest
time=10
threads=64
report-interval=10For Yugabyte, use pg drive instead:
db-driver=pgsql
pgsql-host=localhost
pgsql-port=5433
pgsql-user=mybenchmark
pgsql-password=abczyz
pgsql-db=sbtest
time=10
threads=64
report-interval=10- Then, using cmd:
#sysbench --config-file=config --test=./lua/$2 --tables=$TABLE --table-size=$TABLE_SIZE run
$ cd ./tidb//benchmark
$ ./run.sh -r $num $rangeWith num is the number_id of the oltp test script. Using -h option to check:
./run.sh -h
--prepare, -pp Prepare data
--prewarm, -pw Warm up database
--run, -r Run benchmark
--clear, -cl Clear data
=========================
map file [number] - [file_name]
1. oltp_read_only
2. oltp_write_only
3. oltp_read_write
4. oltp_point_select
5. oltp_insert
6. oltp_update_index
7. oltp_update_non_index
8. oltp_deleteFor example, run oltp_point_select:
./run.sh -r 4range is provided when we want to run write test script on multiple nodes to avoid write conflict. For example, run oltp_write_only with id range from 0 to 100000:
./run.sh -r 2 0,100000- See more .lua test scripts: sysbench lua
- After benchmark, we can clean data.
- Writing script oltp_clean.lua to clean data.
- Then, using cmd (stay on server):
#sysbench --config-file=config --test=./lua/oltp_clean.lua --tables=$TABLE --table-size=$TABLE_SIZE cleanup
$ cd ./tidb//benchmark
$ ./run.sh -clWe define config in file config-run.txt with a format:
$HOST:$RUN_DIRECTORY_ON_SERVER:$RANGE
For example, we want to run benchmark on two servers parallelly within specific range:
10.20.11.57:/myserver/benchmark/sysbench:0,100000
10.20.11.58:/myserver/benchmark/sysbench:100000,200000Run this cmd:
./tidb//bench.sh -ppRun this cmd:
./tidb//bench.sh -r $numWith num is the number_id of the oltp test script.
Run this cmd:
./tidb//bench.sh -clSometimes, we want to automatically run next oltp when current oltp finished.
To do that, we first locate bench-continuous.sh on the directory of benchmark script in the loadtest server. Here is the content of the bench-continuous.sh script:
#!/bin/bash
for test in 1 2 3 4; do
t=8
while [ $t -le 64 ]
do
nohup ./run.sh -r $test $1 $t 2>&1 >>sysbench.log
sleep 15
t=$(( $t * 2 ))
done
doneIn the above script, the oltp script with id 1, 2, 3, 4 will run respectively.
The parameter which represents id of oltp script is test (ex: 1, 2, 3, 4, ...), and t is the number of threads. We can see in the script, t doubles for every benchmark and when one test done, it sleeps for 15s.
$1 is the range mentioned earlier which is passed by another script.
Then, on the client, run bench-continuous-client.sh script.
Note that, when running oltp script continuously, we have to remove threads flag in the config file.
