Sysbench使用指南

PolarDB-X 1.0性能测试使用Sysbench作为压测工具,本文介绍Sysbench的使用方法。

安装

测试使用的是Sysbench 0.5版本,安装方法如下:

git clone https://github.com/akopytov/sysbench.git
cd sysbench
git checkout 0.5

yum -y install make automake libtool pkgconfig libaio-devel
yum -y install mariadb-devel


./autogen.sh
./configure
make -j
make install
            

以上是在压测ECS上的安装方法,如果需要安装到其他操作系统,参考Sysbench 官方文档

安装完毕后,所有自带压测脚本都在/usr/local/share/sysbench目录下,PolarDB-X 1.0性能测试将使用该目录下的脚本。除此之外,也可以在源码目录sysbench/sysbench/tests/db下找到对应的压测脚本。

使用简介

常用测试模型

Sysbench通过脚本定义了若干常用的压测模型,以下简单介绍几个常用模型:

压测模型

描述

bulk_insert.lua

批量插入数据

insert.lua

单值插入数据

delete.lua

删除数据

oltp.lua

混合读写测试,读写比例14:4

select.lua

简单的主键查询

表结构

PolarDB-X 1.0进行测试时,对Sysbench的测试表稍作改造,将其修改为分库分表的形式,测试表的建表语句如下:

CREATE TABLE `sbtest1` (
  `id` int(10) unsigned NOT NULL,
  `k` int(10) unsigned NOT NULL DEFAULT '0',
  `c` char(120) NOT NULL DEFAULT '',
  `pad` char(60) NOT NULL DEFAULT '',
  KEY `xid` (`id`),
  KEY `k_1` (`k`)
) dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 4
            

以上建表语句中,分库分表数可自行调整,该表会在准备数据阶段自动生成,无需手动创建。

测试命令及参数

使用Sysbench进行压测,通常分为三个步骤:

  • prepare:准备数据;

  • run:运行测试模型;

  • cleanup:清理测试数据。

通常仅需准备一次数据,在此数据基础上测试各种模型即可。

常用参数

Sysbench中常用的参数如下:

  • --oltp-tables-count=1:表数量。

  • --oltp-table-size=10000000:每个表产生的记录行数。

  • --oltp-read-only=off:是否生成只读SQL,默认off,如果设置为on,则oltp模型不会生成update, delete,insertSQL语句。

  • --oltp-skip-trx=[on|off]:省略BEGIN/COMMIT语句。默认是off。

  • --rand-init=on:是否随机初始化数据,如果不随机化那么初始好的数据每行内容除了主键不同外其他完全相同。

  • --num-threads=12: 并发线程数,可以理解为模拟的客户端并发连接数。

  • --report-interval=10:表示每10s输出一次性能数据。

  • --max-requests=0:压力测试产生请求的总数,如果以下面的max-time来记,这个值设为0即可。

  • --max-time=120:测试的持续时间。

  • --oltp_auto_inc=off:ID是否为自增列。

  • --oltp_secondary=on:将ID设置为非主键防止主键冲突。

  • --oltp_range_size=5: 连续取值5个,必定走到5个分片。

  • --mysql_table_options='dbpartition by hash(id) tbpartition by hash(id) tbpartitions 2'PolarDB-X 1.0支持拆分表,在建表的时候需要指定拆分方式。

示例命令

您可以使用如下命令进行对应操作:

说明

在执行命令之前您需要先手动创建测试数据库,再根据实际的实例信息对以下参数进行替换:

  • <user_name>:账号名称。

  • <password>:账号密码。

  • <host>:内网或公网地址。

  • <port>:内网或公网地址对应的端口号。

  • <db_name>:数据库名称。

准备数据:

sysbench --test='/usr/local/share/sysbench/oltp.lua' --oltp-tables-count=1 --report-interval=10 --oltp-table-size=10000000  --mysql-user=<user_name> --mysql-password=<password> --mysql-table-engine=innodb  --rand-init=on  --mysql-host=<host> --mysql-port=<port> --mysql-db=<db_name> --max-time=300 --max-requests=0   --oltp_skip_trx=on --oltp_auto_inc=off  --oltp_secondary=on --oltp_range_size=5 --mysql_table_options='dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 2' --num-threads=200   prepare

执行测试:

sysbench --test='/usr/local/share/sysbench/oltp.lua' --oltp-tables-count=1 --report-interval=10 --oltp-table-size=10000000  --mysql-user=<user_name> --mysql-password=<password> --mysql-table-engine=innodb  --rand-init=on  --mysql-host=<host> --mysql-port=<port> --mysql-db=<db_name> --max-time=300 --max-requests=0   --oltp_skip_trx=on --oltp_auto_inc=off --oltp_secondary --oltp_range_size=5 --mysql_table_options='dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 2' --num-threads=200 run

清理环境:

sysbench --test='/usr/local/share/sysbench/oltp.lua' --oltp-tables-count=1 --report-interval=10 --oltp-table-size=10000000  --mysql-user=<user_name> --mysql-password=<password> --mysql-table-engine=innodb  --rand-init=on  --mysql-host=<host> --mysql-port=<port> --mysql-db=<db_name> --max-time=300 --max-requests=0   --oltp_skip_trx=on --oltp_auto_inc=off --oltp_secondary --oltp_range_size=5 --mysql_table_options='dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 2' --num-threads=200 cleanup