前言:
什么是sysbench?
- Oracle官方也使用该工具对MySQL进行测试
- 根据互联网应用特点进行测试
- 可以根据某个具体操作进行测试
- 还支持Oracle、PostgreSQL的测试
- 1.1.0 版本与0.5版本的脚本命名差异较大
安装sysbench
机器行有5.6的安装包的时候可以使用这种方式安装
shell> https://github.com/akopytov/sysbench.git # 通过git clone得到源码
shell> cd sysbench
shell> ./autogen.sh
shell> ./configure --with-mysql-includes=/usr/local/mysql56/include/ --with-mysql-libs=/usr/local/mysql56/lib/ # 关联mysql的头文件和库
##
## 注意,如果我这里使用mysql5.7.9 的include和lib ,提示我 /usr/bin/ld: cannot find -lmysqlclient_r
##
shell> make -j 2 # -j 2 表示用几个cpu核心进行编译
shell> make install # 默认安装到 /usr/local/bin , 如果有自定义目录,configure增加参数 --prefix=自定义目录
shell> echo "export LD_LIBRARY_PATH=/usr/local/mysql56/lib/:$LD_LIBRARY_PATH" >> ~/.bashrc # 添加LD_LIBRARY_PATH
shell> source ~/.bashrc
shell> sysbench --version
sysbench 1.1.0-e4e6d67
机器行有5.7的安装包的时候可以使用这种方式安装
shell> https://github.com/akopytov/sysbench.git # 通过git clone得到源码
shell> cd sysbench
shell> ./autogen.sh
shell> ./configure --with-mysql-includes=/usr/local/mysql57/include/ --with-mysql-libs=/usr/local/mysql57/lib/ # 关联mysql的头文件和库
shell> make -j 2 # -j 2 表示用几个cpu核心进行编译
##
## 注意,如果我这里使用mysql5.7.9 的include和lib ,提示我 /usr/bin/ld: cannot find -lmysqlclient_r
##
解决办法:
find / -name "*mysqlclient_r*"
/usr/lib64/mysql/libmysqlclient_r.so.18
/usr/lib64/mysql/libmysqlclient_r.so.18.1.0
库文件是有的,不过带个数字后缀,给库文件建立软链接
ln -s /usr/lib64/mysql/libmysqlclient_r.so.18 /usr/lib64/mysql/libmysqlclient_r.so
重新make
shell> make -j 2 # -j 2
shell> make install # 默认安装到 /usr/local/bin , 如果有自定义目录,configure增加参数 --prefix=自定义目录
shell> echo "export LD_LIBRARY_PATH=/usr/local/mysql56/lib/:$LD_LIBRARY_PATH" >> ~/.bashrc # 添加LD_LIBRARY_PATH
shell> source ~/.bashrc
shell> sysbench --version
sysbench 1.1.0-e4e6d67
性能测试
首先连接数据库创建sbtest库,create database sbtest;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| demo01 |
| employees |
| mysql |
| performance_schema |
| sbtest |
| sys |
+--------------------+
prepare模式(准备数据),注意要进入oltp_read_write.lua脚本所在位置,我的目录在[root@centos7 sysbench]# pwd
/usr/local/share/sysbench目录下
[root@centos7 sysbench]# sysbench oltp_read_write.lua --mysql-host=192.168.56.138 --mysql-port=3307 --mysql-db=sbtest --mysql-user=root --mysql-password=xsy123 --table_size=20000 --tables=1 --threads=500 --events=500000 --report-interval=10 --time=0 prepare
sysbench 1.1.0-e4e6d67 (using bundled LuaJIT 2.1.0-beta3)
Initializing worker threads...
Creating table 'sbtest1'...
Inserting 20000 records into 'sbtest1'
Creating a secondary index on 'sbtest1'...
run模式(开始跑数据)
[root@centos7 sysbench]# sysbench oltp_read_write.lua --mysql-host=192.168.56.138 --mysql-port=3307 --mysql-db=sbtest --mysql-user=root --mysql-password=xsy123 --table_size=20000 --tables=1 --threads=500 --events=500000 --report-interval=10 --time=0 run
sysbench 1.1.0-e4e6d67 (using bundled LuaJIT 2.1.0-beta3)
Running the test with following options:
Number of threads: 500
Report intermediate results every 10 second(s)
Initializing random number generator from current time
Initializing worker threads...
Threads started!
[ 10s ] thds: 500 tps: 149.85 qps: 5775.29 (r/w/o: 4513.79/789.20/472.31) lat (ms,95%): 5918.87 err/s: 122.92 reconn/s: 0.00
[ 20s ] thds: 500 tps: 154.22 qps: 5494.48 (r/w/o: 4229.45/808.59/456.45) lat (ms,95%): 8484.79 err/s: 148.02 reconn/s: 0.00
[ 30s ] thds: 500 tps: 167.88 qps: 6034.89 (r/w/o: 4647.75/887.30/499.84) lat (ms,95%): 8333.38 err/s: 164.08 reconn/s: 0.00
[ 40s ] thds: 500 tps: 179.98 qps: 6308.20 (r/w/o: 4847.86/934.20/526.14) lat (ms,95%): 7346.49 err/s: 166.18 reconn/s: 0.00
[ 50s ] thds: 500 tps: 164.04 qps: 5808.25 (r/w/o: 4465.76/859.38/483.10) lat (ms,95%): 7754.26 err/s: 155.03 reconn/s: 0.00
cleanup模式(清除上面步骤产生的数据)
[root@centos7 sysbench]# sysbench oltp_read_write.lua --mysql-host=192.168.56.138 --mysql-port=3307 --mysql-db=sbtest --mysql-user=root --mysql-password=xsy123 --table_size=20000 --tables=1 --threads=500 --events=500000 --report-interval=10 --time=0 cleanup
sysbench 1.1.0-e4e6d67 (using bundled LuaJIT 2.1.0-beta3)
Dropping table 'sbtest1'...
参数介绍
--mysql-host IP
--mysql-port 端口号
--mysql-db 希望链接的数据库
--mysql-user 用户名
--mysql-password 密码
--table_size 每张表初始化的数据数量
--tables 初始化表的数量
--threads 启动的线程
--time 运行时间设为0表示不限制时间
--report-interval 运行期间日志,单位为秒
--events 最大请求数量,定义数量后可以不需要--time选项
每一次进行测试,都应该按照:prepare---> run --- cheanup的顺序来测试
在上面的语句后面加上 prepare,执行
在上面的语句后面加上 run,执行
在上面的语句后面加上 cleanup,执行
prepare用于准备测试需要的数据,准备完后执行run来测试,测试完成后不要忘记执行cleanup来清除测试数据
测试结果
[ 10s ] thds: 500 tps: 149.85 qps: 5775.29 (r/w/o: 4513.79/789.20/472.31) lat (ms,95%): 5918.87 err/s: 122.92 reconn/s: 0.00
[ 20s ] thds: 500 tps: 154.22 qps: 5494.48 (r/w/o: 4229.45/808.59/456.45) lat (ms,95%): 8484.79 err/s: 148.02 reconn/s: 0.00
[ 30s ] thds: 500 tps: 167.88 qps: 6034.89 (r/w/o: 4647.75/887.30/499.84) lat (ms,95%): 8333.38 err/s: 164.08 reconn/s: 0.00
[ 40s ] thds: 500 tps: 179.98 qps: 6308.20 (r/w/o: 4847.86/934.20/526.14) lat (ms,95%): 7346.49 err/s: 166.18 reconn/s: 0.00
[ 50s ] thds: 500 tps: 164.04 qps: 5808.25 (r/w/o: 4465.76/859.38/483.10) lat (ms,95%): 7754.26 err/s: 155.03 reconn/s: 0.00
[ 60s ] thds: 500 tps: 156.75 qps: 5744.49 (r/w/o: 4437.72/833.12/473.64) lat (ms,95%): 8333.38 err/s: 160.15 reconn/s: 0.00
[ 70s ] thds: 500 tps: 166.15 qps: 6030.68 (r/w/o: 4654.63/877.39/498.66) lat (ms,95%): 8484.79 err/s: 166.35 reconn/s: 0.00
[ 80s ] thds: 500 tps: 166.00 qps: 5869.22 (r/w/o: 4512.84/867.99/488.39) lat (ms,95%): 8038.61 err/s: 156.40 reconn/s: 0.00
[ 90s ] thds: 500 tps: 161.10 qps: 5768.05 (r/w/o: 4439.32/850.62/478.11) lat (ms,95%): 8333.38 err/s: 155.90 reconn/s: 0.00
[ 100s ] thds: 500 tps: 163.90 qps: 5814.28 (r/w/o: 4472.91/857.98/483.39) lat (ms,95%): 7754.26 err/s: 155.60 reconn/s: 0.00
[ 110s ] thds: 500 tps: 163.70 qps: 5934.95 (r/w/o: 4571.86/872.69/490.40) lat (ms,95%): 8184.67 err/s: 163.00 reconn/s: 0.00
[ 120s ] thds: 500 tps: 176.69 qps: 6164.16 (r/w/o: 4734.81/914.56/514.78) lat (ms,95%): 8184.67 err/s: 161.39 reconn/s: 0.00
thds 500 表示线程数,tps表示每秒的事物数(一个事物中可能包含多条sql语句),qps 表示每秒执行的查询能力(一秒钟执行多少条select)
遇到问题汇总
# 如果执行的时候提示FATAL: Cannot find benchmark 'oltp_read_write.lua': no such built-in test, file or module
切换到sysbench的源码目录(sysbench.tar.gz解压路径)
find ./ -name oltp_read_write.lua
./src/lua/oltp_read_write.lua
接着切换到src/lua 目录再执行语句
#如果执行的时候命令行提示“Can not connect to MySQL server. Too many connections”-mysql 1040错误:
shell>mysql -uroot -p****
mysql>show variables like 'max_connections';(查看当前的最大连接数)
mysql>set global max_connections=1000;(设置最大连接数为1000,可以再次查看是否设置成功)
mysql>show variables like 'max_connections';(查看当前的最大连接数)
mysql>exit