sysbench MySQL 基准测试

定义

  • sysbench是一个开源的、模块化的、跨平台的多线程性能测试工具
  • 可以用来进行CPU、内存、磁盘I/O、线程、数据库的性能测试。
  • 目前支持的数据库有MySQL、Oracle和PostgreSQL。

当前功能允许测试的系统参数有:

file I/O performance                            (文件I / O性能)
scheduler performance                           (调度性能)
memory allocation and transfer speed            (内存分配和传输速度)
POSIX threads implementation performance        (POSIX线程执行绩效)
database server performance (OLTP benchmark)    (数据库服务器性能)

安装

  1. 准备工作,现安装依赖什么的
# 没有mysql则先安装下
yum install mysql mysql-server mysql-devel

# 安装依赖,否则会在编译阶段报错

# centos6用:
 yum install MySQL-shared  
 yum install MySQL-devel

# centos7用:

yum install -y Percona-XtraDB-Cluster-shared-56.x86_64 
yum install  -y Percona-XtraDB-Cluster-devel-56
  1. 下载并压缩软件包
wget https://github.com/akopytov/sysbench/archive/1.0.zip 
unzip 1.0.zip
cd sysbench-1.0
  1. 编译
./autogen.sh
./configure && make && make install

# 另外指定安装目录则用下面这个
--prefix=/usr/sysbench/                    # 指定sysbench的安装目录。
--with-mysql-includes=/usr/include/mysql/  # 指定安装mysql时候的includes目录。
--with-mysql-libs=/usr/lib/mysql/          # 指定装mysql时候的lib目录。
--with-mysql                               #sysbench默认支持mysql,如果需要测试oracle或者pgsql则需要制定–with-oracle或者–with-pgsql。 

make && make install
  1. 测试运行:
sysbench --version
sysbench cpu --cpu-max-prime=50 run

sysbench支持以下模式:

1、CPU运算性能                    [CPU]
2、磁盘IO性能                     [FILEIO]
3、调度程序性能                   [mutex ]
4、内存分配及传输速度             [memory ]
5、POSIX线程性能                  [threads ]
6、数据库性能(OLTP基准测试)

参考命令参数:

  • sysbench –test=fileio help
  • sysbench –test=cpu help
  • sysbench –test=memory help
  • sysbench –test=threads help
  • sysbench –test=mutex help

目前sysbench主要支持 mysql,drizzle,pgsql,oracle 等几种数据库。

通用命令说明

root@db2:~# sysbench 
Missing required command argument.
Usage: #使用方法
  sysbench [general-options]... --test=<test-name> [test-options]... command

General options: #通用选项
  --num-threads=N            number of threads to use [1] #创建测试线程的数目。默认为1.
  --max-requests=N           limit for total number of requests [10000] #请求的最大数目。默认为10000,0代表不限制。
  --max-time=N               limit for total execution time in seconds [0] #最大执行时间,单位是s。默认是0,不限制。
  --forced-shutdown=STRING   amount of time to wait after --max-time before forcing shutdown [off] #超过max-time强制中断。默认是off。
  --thread-stack-size=SIZE   size of stack per thread [32K] #每个线程的堆栈大小。默认是32K。
  --init-rng=[on|off]        initialize random number generator [off] #在测试开始时是否初始化随机数发生器。默认是off。
  --test=STRING              test to run #指定测试项目名称。
  --debug=[on|off]           print more debugging info [off] #是否显示更多的调试信息。默认是off。
  --validate=[on|off]        perform validation checks where possible [off] #在可能情况下执行验证检查。默认是off。
  --help=[on|off]            print help and exit #帮助信息。
  --version=[on|off]         print version and exit #版本信息。

Compiled-in tests: #测试项目
  fileio - File I/O test #IO
  cpu - CPU performance test #CPU
  memory - Memory functions speed test #内存
  threads - Threads subsystem performance test #线程
  mutex - Mutex performance test #互斥性能测试
  oltp - OLTP test # 数据库,事务处理

Commands: prepare:测试前准备工作; run:正式测试 cleanup:测试后删掉测试数据 help version

See 'sysbench --test=<name> help' for a list of options for each test. #查看每个测试项目的更多选项列表

简单测试代码

# 测试CPU: 
sysbench --test=cpu --cpu-max-prime=2000 run

# 测试线程:
sysbench  --test=threads --num-threads=500 --thread-yields=100 --thread-locks=4 run

# 测试IO:
# --num-threads 开启的线程     --file-total-size 总的文件大小
# prepare阶段,生成需要的测试文件,完成后会在当前目录下生成很多小文件。
sysbench --test=fileio --num-threads=16 --file-total-size=2G --file-test-mode=rndrw prepare   
# run阶段
sysbench --test=fileio --num-threads=20 --file-total-size=2G --file-test-mode=rndrw run
# 清理测试时生成的文件
sysbench --test=fileio --num-threads=20 --file-total-size=2G --file-test-mode=rndrw cleanup

# 测试内存:
sysbench --test=memory --memory-block-size=8k --memory-total-size=1G run

sysbench 之 MySQL语法

sysbench [options]... [testname] [command]
1- options
  1. MySQL连接信息参数
--mysql-host:      # MySQL服务器主机名,默认localhost;如果在本机上使用localhost报错,提示无法连接MySQL服务器,改成本机的IP地址应该就可以了。
--mysql-port:      # MySQL服务器端口,默认3306
--mysql-user:      # 用户名
--mysql-password:  # 密码
  1. MySQL执行参数
--oltp-test-mode:      # 执行模式,包括simple、nontrx和complex,默认是complex。simple模式下只测试简单的查询;nontrx不仅测试查询,还测试插入更新等,但是不使用事务;complex模式下测试最全面,会测试增删改查,而且会使用事务。可以根据自己的需要选择测试模式。
--oltp-tables-count:   # 测试的表数量,根据实际情况选择
--oltp-table-size:     # 测试的表的大小,根据实际情况选择
--threads:             # 客户端的并发连接数
--time:                # 测试执行的时间,单位是秒,该值不要太短,可以选择120
--report-interval:     # 生成报告的时间间隔,单位是秒,如10
2- testname

testname指定了要进行的测试,在老版本的sysbench中,可以通过–test参数指定测试的脚本;而在新版本中,–test参数已经声明为废弃,可以不使用–test,而是直接指定脚本。

例如,如下两种方法效果是一样的:

sysbench --test=./tests/include/oltp_legacy/oltp.lua

sysbench ./tests/include/oltp_legacy/oltp.lua

测试时使用的脚本为lua脚本,可以使用sysbench自带脚本,也可以自己开发。对于大多数应用,使用sysbench自带的脚本就足够了。不同版本的sysbench中,lua脚本的位置可能不同,可以自己在sysbench路径下使用find命令搜索oltp.lua。

P.S.:大多数数据服务都是oltp类型的,如果你不了解什么是oltp,那么大概率你的数据服务就是oltp类型的。

3- command

command是sysbench要执行的命令,包括prepare、run和cleanup

顾名思义:
- prepare是为测试提前准备数据
- run是执行正式的测试
- cleanup是在测试完成后对数据库进行清理

测试mysql步骤

精简化其实就以下一句语句按默认执行

# sysbench 默认安装在 /usr/local/share/sysbench 里面,里面集成一堆lua可选命令
sysbench  --mysql-user=root --mysql-password=root --mysql-host=localhost --mysql-db=test /usr/local/share/sysbench/oltp_read_write.lua run
  1. 准备数据
sysbench --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=sysbench  --threads=10 --time=120 --report-interval=10  /usr/local/share/sysbench/oltp_read_only.lua prepare
  1. 执行
sysbench --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=sysbench  --threads=10 --time=120 --report-interval=10  /usr/local/share/sysbench/oltp_read_only.lua run

# 结果
Threads started!

[ 10s ] thds: 10 tps: 761.80 qps: 12198.84 (r/w/o: 10674.24/0.00/1524.61) lat (ms,95%): 15.83 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 10 tps: 787.62 qps: 12598.19 (r/w/o: 11023.14/0.00/1575.05) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
[ 30s ] thds: 10 tps: 774.37 qps: 12396.98 (r/w/o: 10848.03/0.00/1548.95) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
[ 40s ] thds: 10 tps: 791.95 qps: 12669.26 (r/w/o: 11085.35/0.00/1583.91) lat (ms,95%): 15.00 err/s: 0.00 reconn/s: 0.00
[ 50s ] thds: 10 tps: 799.00 qps: 12784.08 (r/w/o: 11186.08/0.00/1598.00) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
[ 60s ] thds: 10 tps: 751.00 qps: 12017.05 (r/w/o: 10515.04/0.00/1502.01) lat (ms,95%): 17.32 err/s: 0.00 reconn/s: 0.00
[ 70s ] thds: 10 tps: 794.00 qps: 12701.85 (r/w/o: 11113.85/0.00/1587.99) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
[ 80s ] thds: 10 tps: 791.88 qps: 12668.39 (r/w/o: 11084.63/0.00/1583.76) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
[ 90s ] thds: 10 tps: 789.99 qps: 12643.09 (r/w/o: 11063.10/0.00/1579.99) lat (ms,95%): 13.70 err/s: 0.00 reconn/s: 0.00
[ 100s ] thds: 10 tps: 779.02 qps: 12464.30 (r/w/o: 10906.26/0.00/1558.04) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
[ 110s ] thds: 10 tps: 810.92 qps: 12966.54 (r/w/o: 11344.71/0.00/1621.83) lat (ms,95%): 12.98 err/s: 0.00 reconn/s: 0.00
[ 120s ] thds: 10 tps: 781.18 qps: 12507.75 (r/w/o: 10945.49/0.00/1562.26) lat (ms,95%): 16.12 err/s: 0.00 reconn/s: 0.00
SQL statistics:
    queries performed:
        read:                            1317932
        write:                           0
        other:                           188276
        total:                           1506208
    transactions:                        94138  (784.43 per sec.)
    queries:                             1506208 (12550.84 per sec.)
    ignored errors:                      0      (0.00 per sec.)
    reconnects:                          0      (0.00 per sec.)

General statistics:
    total time:                          120.0068s
    total number of events:              94138

Latency (ms):
         min:                                   10.85
         avg:                                   12.74
         max:                                   56.37
         95th percentile:                       14.73
         sum:                              1199736.65

Threads fairness:
    events (avg/stddev):           9413.8000/0.40
    execution time (avg/stddev):   119.9737/0.00
结果分析

对于我们比较重要的信息包括

  • queries:查询总数及qps

  • transactions:事务总数及tps

  • Latency-95th percentile:前95%的请求的最大响应时间

注意事项

  1. 尽量不要在MySQL服务器运行的机器上进行测试,一方面可能无法体现网络(哪怕是局域网)的影响,另一方面,sysbench的运行(尤其是设置的并发数较高时)会影响MySQL服务器的表现。
  2. 可以逐步增加客户端的并发连接数(–thread参数),观察在连接数不同情况下,MySQL服务器的表现;如分别设置为10,20,50,100等。
  3. 一般执行模式选择complex即可,如果需要特别测试服务器只读性能,或不使用事务时的性能,可以选择simple模式或nontrx模式。
  4. 如果连续进行多次测试,注意确保之前测试的数据已经被清理干净。

使用建议

  1. 在开始测试之前,应该首先明确:应采用针对整个系统的基准测试,还是针对MySQL的基准测试,还是二者都需要。
  2. 如果需要针对MySQL的基准测试,那么还需要明确精度方面的要求:是否需要使用生产环境的真实数据,还是使用工具生成也可以;前者实施起来更加繁琐。如果要使用真实数据,尽量使用全部数据,而不是部分数据。
  3. 基准测试要进行多次才有意义。
  4. 测试时需要注意主从同步的状态。
  5. 测试必须模拟多线程的情况,单线程情况不但无法模拟真实的效率,也无法模拟阻塞甚至死锁情况。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值