mysql数据库性能测试
Posted OldA
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql数据库性能测试相关的知识,希望对你有一定的参考价值。
1 OLTP性能测试
1.1 测试环境准备
- 不管通过什么方式进行部署的mysql集群或单机mysql。本次测试使用docker启动了单机的mysql进行测试。
docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 mysql:5.7.22
- 测试工具为
sysbench
,docker 镜像获取并启动:
docker pull oldyoutiao/sysbench:latest
docker run -d --name sysbench oldyoutiao/sysbench:latest
- 如果mysql运行在k8s集群内,建议sysbench和mysql运行在同一个namespace下,直接通过mysql的service来访问。
准备测试数据库:
create database testdb;
1.2 OLTP混合读写场景
一下操作均在sysbench容器内进行,需要先进入容器:docker exec -ti sysbench bash
- 准备数据
sysbench --db-driver=mysql --mysql-host=192.168.28.91 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=testdb --table_size=25000 --tables=100 --events=0 --time=300 --threads=10 oltp_read_write prepare
- 运行workload
sysbench --db-driver=mysql --mysql-host=192.168.28.91 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=testdb --table_size=25000 --tables=100 --events=0 --time=300 --threads=10 --percentile=95 --report-interval=0 oltp_read_write run
运行结果展示:
sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)
Running the test with following options:
Number of threads: 10
Initializing random number generator from current time
Initializing worker threads...
Threads started!
SQL statistics:
queries performed:
read: 440776
write: 125936
other: 62968
total: 629680
transactions: 31484 (104.86 per sec.)
queries: 629680 (2097.20 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 300.2371s
total number of events: 31484
Latency (ms):
min: 16.79
avg: 95.27
max: 599.26
95th percentile: 200.47
sum: 2999600.96
Threads fairness:
events (avg/stddev): 3148.4000/19.12
execution time (avg/stddev): 299.9601/0.01
- 清理数据
sysbench --db-driver=mysql --mysql-host=192.168.28.91 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=testdb --table_size=25000 --tables=100 --events=0 --time=300 --threads=10 --percentile=95 oltp_read_write cleanup
1.3 OLTP只读场景
- 准备数据
sysbench --db-driver=mysql --mysql-host=192.168.28.91 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=testdb --table_size=25000 --tables=100 --events=0 --time=300 --threads=10 oltp_read_only prepare
- 运行workload
sysbench --db-driver=mysql --mysql-host=192.168.28.91 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=testdb --table_size=25000 --tables=100 --events=0 --time=300 --threads=10 --percentile=95 --skip-trx=1 --report-interval=0 oltp_read_only run
运行结果
sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)
Running the test with following options:
Number of threads: 10
Initializing random number generator from current time
Initializing worker threads...
Threads started!
SQL statistics:
queries performed:
read: 790216
write: 0
other: 0
total: 790216
transactions: 56444 (188.06 per sec.)
queries: 790216 (2632.78 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 300.1398s
total number of events: 56444
Latency (ms):
min: 8.41
avg: 53.14
max: 401.81
95th percentile: 97.55
sum: 2999394.33
Threads fairness:
events (avg/stddev): 5644.4000/29.20
execution time (avg/stddev): 299.9394/0.03
- 清理数据
sysbench --db-driver=mysql --mysql-host=192.168.28.91 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=testdb --table_size=25000 --tables=100 --events=0 --time=300 --threads=10 --percentile=95 oltp_read_only cleanup
1.4 OLTP只写场景
- 准备数据
sysbench --db-driver=mysql --mysql-host=192.168.28.91 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=testdb --table_size=25000 --tables=100 --events=0 --time=300 --threads=10 oltp_write_only prepare
- 运行workload
sysbench --db-driver=mysql --mysql-host=192.168.28.91 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=testdb --table_size=25000 --tables=100 --events=0 --time=300 --threads=10 --percentile=95 --report-interval=0 oltp_write_only run
运行结果:
sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)
Running the test with following options:
Number of threads: 10
Initializing random number generator from current time
Initializing worker threads...
Threads started!
SQL statistics:
queries performed:
read: 0
write: 272475
other: 136242
total: 408717
transactions: 68117 (226.96 per sec.)
queries: 408717 (1361.81 per sec.)
ignored errors: 8 (0.03 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 300.1183s
total number of events: 68117
Latency (ms):
min: 4.46
avg: 44.02
max: 529.52
95th percentile: 125.52
sum: 2998630.72
Threads fairness:
events (avg/stddev): 6811.7000/38.12
execution time (avg/stddev): 299.8631/0.02
- 清理数据
sysbench --db-driver=mysql --mysql-host=192.168.28.91 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=testdb --table_size=25000 --tables=100 --events=0 --time=300 --threads=10 --percentile=95 oltp_write_only cleanup
2 TPC-C测试
获取tpcc-mysql镜像,并启动tpcc容器
docker pull oldyoutiao/tpcc-mysql:latest
docker run -d --name tpcc oldyoutiao/tpcc-mysql:latest
或者直接运行在k8s集群内,尽量和k8s在同一个namespace下,以保证性能。
2.1 创建表
- 在宿主机上安装mysql客户端:
yum install mariadb -y
- 导入数据
docker run -it --rm oldyoutiao/tpcc-mysql:latest cat create_table.sql add_fkey_idx.sql | mysql -uroot -p123456 -h192.168.28.91 -P3306 testdb
- 添加主键,进入mysql,添加主键
alter table history add constraint pkey_history primary key(h_c_id,h_c_d_id,h_c_w_id,h_d_id,h_w_id,h_date,h_amount);
mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| customer |
| district |
| history |
| item |
| new_orders |
| order_line |
| orders |
| stock |
| warehouse |
+------------------+
9 rows in set (0.00 sec)
mysql> alter table history add constraint pkey_history primary key(h_c_id,h_c_d_id,h_c_w_id,h_d_id,h_w_id,h_date,h_amount);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
2.2 加载数据
进入tpcc容器
docker exec -ti tpcc bash
执行命令加载数据
tpcc_load 192.168.28.91 testdb root \'123456\' 1
参数说明:host,db,user,password,仓库数量,其中仓库数量根据数据库性能可自行调整。
如果修改过默认mysql的3306端口,可能无法加载数据。
2.3 运行tpcc测试
tpcc_start -h192.168.28.91 -P3306 -d testdb -u root -p 123456 -w 1 -c2 -r120 -l300
参数说明:
-w 仓库数量
-c 连接数
-r 预热时间:数据量很大的情况下,预热时间相对的延长,有助于提升测试结果;
-l 测试时间:根据情况设定,生产环境可能在2H以上。
-d 数据库
-h mysql主机
-P 端口号
-u 用户名
-p 密码
测试结果:
<Raw Results>
[0] sc:646 lt:3249 rt:0 fl:0
[1] sc:3828 lt:72 rt:0 fl:0
[2] sc:386 lt:4 rt:0 fl:0
[3] sc:390 lt:0 rt:0 fl:0
[4] sc:3 lt:387 rt:0 fl:0
in 300 sec.
<Raw Results2(sum ver.)>
[0] sc:646 lt:3249 rt:0 fl:0
[1] sc:3828 lt:72 rt:0 fl:0
[2] sc:386 lt:4 rt:0 fl:0
[3] sc:390 lt:0 rt:0 fl:0
[4] sc:3 lt:387 rt:0 fl:0
<Constraint Check> (all must be [OK])
[transaction percentage]
Payment: 43.50% (>=43.0%) [OK]
Order-Status: 4.35% (>= 4.0%) [OK]
Delivery: 4.35% (>= 4.0%) [OK]
Stock-Level: 4.35% (>= 4.0%) [OK]
[response time (at least 90% passed)]
New-Order: 16.59% [NG] *
Payment: 98.15% [OK]
Order-Status: 98.97% [OK]
Delivery: 100.00% [OK]
Stock-Level: 0.77% [NG] *
<TpmC>
779.000 TpmC
3 TPC-H测试
敬请期待。。。
以上是关于mysql数据库性能测试的主要内容,如果未能解决你的问题,请参考以下文章