一般情况下,在企业中我们申请了一台数据库服务器,不是直接开发使用,而是先进行压测,这样就能心里有数,这台数据库服务器的负载压力如何,性能表现如何,每秒能抗住多少个请求。
1、测试机器
2 vCPU 4 GiB (I/O优化) ecs.t6-c1m2.large]
2、数据库压测工具
sysbench是非常好用的数据库压测工具,这个工具可以自动帮你在数据库里构造出来大量的数据,你想要多少数据,他就自动给你构造出来多少条数据。
然后这个工具接着可以模拟几千个线程并发的访问你的数据库,模拟使用各种各样的SQL语句来访问你的数据库,包括模拟出来各种事务提交到你的数据库里去,甚至可以模拟出几十万的TPS去压测你的数据库。
3、在Linux上安装sysbench
curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash
sudo yum -y install sysbench
sysbench --version
sysbench 1.0.19
4、建立测试库
create database test_db
character set utf8mb4 collate utf8mb4_bin;
5、准备测试表
sysbench
//基于mysql的驱动去连接mysql数据库
--db-driver=mysql
//连续访问300秒
--time=300
//10个线程模拟并发访问
--threads=10
//每隔1秒输出一下压测情况
--report-interval=1
//本机
--mysql-host=127.0.0.1
//端口号:3306
--mysql-port=3306
//测试用户
--mysql-user=root
//测试密码
--mysql-password=*******
//测试数据库
--mysql-db=test_db
//模拟新建20个表
--tables=20
//100万条数据 执行oltp数据库的读写测试
--table_size=1000000 oltp_read_write
//参照这个命令的设置去构造出来我们需要的数据库里的数据
//自动创建20个测试表,每个表里创建100万条测试数据
--db-ps-mode=disable prepare
执行结果:
Initializing worker threads...
Creating table 'sbtest2'...
Creating table 'sbtest3'...
Creating table 'sbtest7'...
Creating table 'sbtest5'...
Creating table 'sbtest9'...Creating table 'sbtest6'...
Creating table 'sbtest1'...
Creating table 'sbtest4'...
Creating table 'sbtest8'...
Creating table 'sbtest10'...
Inserting 1000000 records into 'sbtest1'
Inserting 1000000 records into 'sbtest8'
Inserting 1000000 records into 'sbtest3'
Inserting 1000000 records into 'sbtest6'
Inserting 1000000 records into 'sbtest5'
Inserting 1000000 records into 'sbtest9'
Inserting 1000000 records into 'sbtest2'
Inserting 1000000 records into 'sbtest7'
Inserting 1000000 records into 'sbtest10'
Inserting 1000000 records into 'sbtest4'
Creating a secondary index on 'sbtest8'...
Creating a secondary index on 'sbtest9'...
Creating a secondary index on 'sbtest7'...
Creating a secondary index on 'sbtest5'...
Creating a secondary index on 'sbtest4'...
Creating a secondary index on 'sbtest1'...
Creating a secondary index on 'sbtest6'...
Creating a secondary index on 'sbtest10'...
Creating a secondary index on 'sbtest2'...
Creating a secondary index on 'sbtest3'...
Creating table 'sbtest18'...
Inserting 1000000 records into 'sbtest18'
Creating table 'sbtest19'...
Inserting 1000000 records into 'sbtest19'
Creating table 'sbtest17'...
Inserting 1000000 records into 'sbtest17'
Creating table 'sbtest15'...
Inserting 1000000 records into 'sbtest15'
Creating table 'sbtest12'...
Creating table 'sbtest14'...
Inserting 1000000 records into 'sbtest12'
Inserting 1000000 records into 'sbtest14'
Creating table 'sbtest11'.