MYSQL——压测

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、MYSQL压测

常用的mysql压测工具有两个

1、Mysql自带的压力测试工具——Mysqlslap

2、第三方工具sysbench进行压力测试

二、MySQL自带的压力测试工具——Mysqlslap

mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新,给出了性能测试数据而且提供了多种引擎的性能比较。mysqlslap为mysql性能优化前后提供了直观的验证依据,系统运维和DBA人员应该掌握一些常见的压力测试工具,才能准确的掌握线上数据库支撑的用户流量上限及其抗压性等问题。

(一)、更改其默认的最大连接数

再对mysql进行压力测试之前,需要更改其默认的最大连接数,

vim /etc/my.cnf
在这里插入图片描述
systemctl restart mysqld.service
进入mysql中查看最大连接数

在这里插入图片描述

进行压力测试

注意:mysqlslap好像会与default-character-set=utf8冲突
建议测试前先my.cnf文件中的default-character-set=utf8注释掉

在这里插入图片描述

在这里插入图片描述

mysqlslap --defaults-file=/etc/my.cnf --concurrency=100,200 --iterations=1 --number-int-cols=20 --number-char-cols=30 --auto-generate-sql --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed --engine=myisam,innodb --number-of-queries=2000 -uroot -pabc123 --verbose

#模拟测试两次读写并发,第一次100,第二次200,自动生成SQL脚本,测试表包含20个init字段,30个char字段,每次执行2000查询请求。测试引擎分别是myisam,innodb。

–defaults-file=/etc/my.cnf 从/etc/my.cnf文件中读取默认选项
–concurrency=100,200 测试并发的线程数/客户端数,第一次100,第二次200
–iterations=1 指定测试重复次数1次
–number-int-cols=20 指定测试表中int列的数量
–number-char-cols=30 指定测试表中varchar列的数量
–auto-generate-sql 自动生成 SQL 语句
–auto-generate-sql-add-autoincrement 在自动生成的表中添加自增列
–auto-generate-sql-load-type=mixed 测试的负载模型,包括 mixed, update, write, key,read,默认是 mix
–engine=myisam,innodb 指定建表时的存储引擎
–number-of-queries=2000 指定每个线程执行的 SQL 语句数量上限(不精确)
–verbose 更详细的输出

在这里插入图片描述

Average number of seconds to run all queries #运行所有查询的平均秒数

Minimum number of seconds to run all queries #运行所有查询所需的最小秒数

Maximum number of seconds to run all queries #运行所有查询的最大秒数

Number of clients running queries #运行查询的客户端数量

Average number of queries per client #每个客户端的平均查询数

mysqlslap --defaults-file=/etc/my.cnf --concurrency=200,400 --iterations=1 --number-int-cols=50 --number-char-cols=60 --auto-generate-sql --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed --engine=myisam,innodb --number-of-queries=80000 -uroot -pabc123 --verbose

压力调大测试,发现明显现在查询时间明显变慢了

在这里插入图片描述

压力大一点
在这里插入图片描述

(二)、使用第三方工具sysbench进行压力测试

1、简介

系统平台/自述文件在硕士 ·阿科皮托夫/系统台 ·吉特哈布 (github.com)
https://github.com/akopytov/sysbench/blob/master/README.md#general-command-line-options
sysbench]是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试。数据库目前支持MySQL/Oracle/PostgreSQL。本文只是简单演示一下几种测试的用法,后续准备利用sysbench来对MySQL进行一系列的测试。具体的一些参数设置,需要根据不同的测试要求来进行调整。
CPU 运算性能测试
磁盘、IO、性能测试
调度程序性能测试
内存分配及传输速度测试
POSIX 线程性能测试
数据库性能测试(OLTP 基准测试,需要通过 /usr/share/sysbench/ 目录中的 Lua 脚本执行,例如 oltp_read_only.lua 脚本执行只读测试)
sysbench 还可以通过运行命令时指定自己的 Lua 脚本来自定义测试。

2、默认支持mysql

yum -y install epel-release #安装第三方epel源
yum -y install sysbench #安装sysbench工具
sysbench --version #确定工具已安装
sysbench 1.0.17

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

3、查看sysbench工具的帮助选项

  • testname 是内置测试的可选名称(例如 ,,等),或捆绑的 Lua 脚本之一的名称(例如),或自定义 Lua 脚本的路径。如果在命令行上没有指定测试名称(因此,也没有命令,因为在这种情况下,它将被解析为测试名称),或者测试名称是破折号(“”),则 sysbench 期望 Lua 脚本在其标准输入上执行。

  • command 是一个可选参数,它将通过系统平台传递到使用测试名称指定的内置测试或脚本。命令定义测试必须执行的操作。可用命令的列表取决于特定的测试。某些测试还实现自己的自定义命令。

以下是对典型测试命令及其用途的描述:

`prepare`:为那些需要它们的测试执行准备操作,例如,在磁盘上为测试创建必要的文件,或为数据库基准测试填充测试数据库。
`run`:运行使用测试名称参数指定的实际测试。此命令由所有测试提供。
`cleanup`:在创建临时数据的测试中,删除测试运行后的临时数据。
`help`:显示使用测试名参数指定的测试的用法信息。这包括测试提供的命令的完整列表,因此应使用它来获取可用命令。

  • optiond是以 开头的零个或多个命令行选项的列表。与命令一样,该命令应用于描述特定测试提供的可用选项。
有关 sysbench 本身提供的常规选项的说明,请参阅常规命令行选项。
#可用于显示常规命令行语法和选项。sysbench --help
sysbench --help
Usage:
  sysbench [options]... [testname] [command]

Commands implemented by most tests: prepare run cleanup help

General options:  #普通功能
  --threads=N                     要使用的线程数,默认 1[1]
  --events=N                      最大允许的事件个数 [0]
  --time=N                        最大的总执行时间,以秒为单位 [10]
  --forced-shutdown=STRING        在 --time 时间限制到达后,强制关闭之前等待的秒数,默认“off”禁用
  --thread-stack-size=SIZE        每个线程的堆栈大小 [64K]
  --rate=N                        平均传输速率。0 则无限制 [0]
  --report-interval=N             以秒为单位定期报告具有指定间隔的中间统计信息 0 禁用中间报告 [0]
  --report-checkpoints=[LIST,...] 转储完整的统计信息并在指定的时间点重置所有计数器。参数是一个逗号分隔的值列表,表示从测试开始经过这个时间量时必须执行报告检查点(以秒为单位)。报告检查点默认关闭。 []
  --debug[=on|off]                打印更多 debug 信息 [off]
  --validate[=on|off]             尽可能执行验证检查 [off]
  --help[=on|off]                 显示帮助信息并退出 [off]
  --version[=on|off]              显示版本信息并退出 [off]
  --config-file=FILENAME          包含命令行选项的文件
  --tx-rate=N                     已弃用——rate [0]的别名
  --max-requests=N                已弃用 --events [0]的别名
  --max-time=N                    已弃用 --time [0]的别名
  --num-threads=N                 已弃用 --threads [1]的别名

Pseudo-Random Numbers Generator options: #伪随机数生成器选项
  --rand-type=STRING 随机数分布 {uniform,gaussian,special,pareto} 默认使用[special]
  --rand-spec-iter=N 生成数字的迭代次数[12]
  --rand-spec-pct=N  被视为“特殊”(用于特殊分布)的值的百分比[1]
  --rand-spec-res=N  使用的“特殊”值的百分比(用于特殊分布)[75]
  --rand-seed=N      随机数生成器的种子。0时,当前时间作为RNG的种子。[0]
  --rand-pareto-h=N  pareto分布的参数h [0.2]

Log options:  #日志选项
  --verbosity=N verbosity level {5 - debug, 0 - only critical messages} [3]

  --percentile=N       percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95]
  --histogram[=on|off] print latency histogram in report [off]

General database options:

  --db-driver=STRING  specifies database driver to use ('help' to get list of available drivers) [mysql]
  --db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]
  --db-debug[=on|off] print database-specific debug information [off]


Compiled-in database drivers:
  mysql - MySQL driver
  pgsql - PostgreSQL driver

mysql options:
  --mysql-host=[LIST,...]          MySQL server host [localhost]
  --mysql-port=[LIST,...]          MySQL server port [3306]
  --mysql-socket=[LIST,...]        MySQL socket
  --mysql-user=STRING              MySQL user [sbtest]
  --mysql-password=STRING          MySQL password []
  --mysql-db=STRING                MySQL database name [sbtest]
  --mysql-ssl[=on|off]             use SSL connections, if available in the client library [off]
  --mysql-ssl-cipher=STRING        use specific cipher for SSL connections []
  --mysql-compression[=on|off]     use compression, if available in the client library [off]
  --mysql-debug[=on|off]           trace all client library calls [off]
  --mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205]
  --mysql-dry-run[=on|off]         Dry run, pretend that all MySQL client API calls are successful without executing them [off]

pgsql options:
  --pgsql-host=STRING     PostgreSQL server host [localhost]
  --pgsql-port=N          PostgreSQL server port [5432]
  --pgsql-user=STRING     PostgreSQL user [sbtest]
  --pgsql-password=STRING PostgreSQL password []
  --pgsql-db=STRING       PostgreSQL database name [sbtest]

Compiled-in tests:
  fileio - File I/O test
  cpu - CPU performance test
  memory - Memory functions speed test
  threads - Threads subsystem performance test
  mutex - Mutex performance test

See 'sysbench <testname> help' for a list of options for each test.
mysql options:
–mysql-host=[LIST,…] MySQL server host [localhost] #MySQL服务器主机
–mysql-port=[LIST,…] MySQL server port [3306] #MySQL服务器端口
–mysql-socket=[LIST,…] MySQL socket #MySQL的套接字
–mysql-user=STRING MySQL user [sbtest] #MySQL用户
–mysql-password=STRING MySQL password [] #MySQL密码
–mysql-db=STRING MySQL database name [sbtest] #MySQL数据库名称
–mysql-ssl[=on|off] use SSL connections, if available in the client library [off] #是否开启ssl连接
–mysql-ssl-cipher=STRING use specific cipher for SSL connections [] #对SSL连接使用特定的密码
–mysql-compression[=on|off] use compression, if available in the client library [off] #是否开启压缩功能
–mysql-debug[=on|off] trace all client library calls [off] #是否跟踪所有客户端库调用
–mysql-ignore-errors=[LIST,…] list of errors to ignore, or “all” [1213,1020,1205] #要忽略或“全部”的错误列表 --mysql-dry-run[=on|off] Dry run, pretend that all MySQL client API calls are successful without executing them [off] #模拟运行,假设所有的MySQL客户端API调用都是成功的,但没有执行它们

4、sysbench测试MySQL数据库性能

(1)、准备测试数据
#查看sysbench自带的lua脚本使用方法
sysbench /usr/share/sysbench/oltp_common.lua help

#必须创建sbtest库,sbtest事sysbench默认使用的库名
mysqladmin -uroot -pabc123 create sbtest;

#然后,准备测试所用的表,这些测试表放在测试库sbtest中。这里使用的lua脚本为/usr/share/sysbench/oltp_common.lua
sysbench --mysql-host=127.0.0.1 \
--mysql-port=3306 \
--mysql-user=root \
--mysql-password=abc123 \
/usr/share/sysbench/oltp_common.lua \
--tables=10 \
--table_size=100000 \
prepare

#其中--tables=10表示创建10个测试表,
#--table_size=100000表示每个表中插入10W行数据,
#prepare表示这是准备数的过程。

在这里插入图片描述

--auto_inc[=on|off]     id列默认自增,默认[on]
--create_secondary[=on|off]   索引不是主键索引而是二级索引,默认[off]
--delete_inserts=N     每个事务的delete、insert的组合数,默认[1]    
--distinct_ranges=N      单个事务中执行范围distinct查询的数量,默认[1] 
--index_updates=N    单个事务中执行索引更新的操作的数量,默认[1]
--mysql_storage_engine=STRING  mysql的存储引擎,默认是[innodb]
--non_index_updates=N    单个事务中执行索引更新的操作的数量,默认[1]
--order_ranges=N    单个事务中执行范围order by查询的数量,默认[1]
--pgsql_variant=STRING  在运行pgsql的驱动程序时,使用了pgsql的变体,当前仅仅支持的variant是'redshift',当启动时,将自动禁用create_secondary为不可用,并将delete_insert设置为0
--point_selects=N  单个事务中select查询的数量,默认 [10]
--range_selects[=on|off]   启用/禁用所有SELECT查询[on]
--secondary[=on|off]    使用二级索引代替逐渐,默认[off]
--range_size=N    查询范围。默认是100
--simple_ranges=N     每个事务的简单查询范围select,默认[1]
--skip_trx[=on|off]     语句以bigin/commit开始结尾,默认[off]
--sum_ranges=N      每个事务的select sum的查询总量,默认[1]
--table_size=N     指定表的记录大小(行的数量),默认[10000]
--tables=N  创建的表数量,默认[1]

在这里插入图片描述
在这里插入图片描述

sysbench --threads=4 \
--time=20 \
--report-interval=5 \
--mysql-host=127.0.0.1 \
--mysql-port=3306 \
--mysql-user=root \
--mysql-password=abc123 \
/usr/share/sysbench/oltp_read_write.lua \
--tables=100 \
--table_size=1000000 \
run

在这里插入图片描述

sysbench --threads=4 \
--time=20 \
--report-interval=5 \
--mysql-host=127.0.0.1 \
--mysql-port=3306 \
--mysql-user=root \
--mysql-password=abc123 \
/usr/share/sysbench/oltp_read_write.lua \
--tables=100 \
--table_size=1000000 \
run
sysbench 1.0.17 (using system LuaJIT 2.0.4)

Running the test with following options:
Number of threads: 4   #并发数4
Report intermediate results every 5 second(s)   #每5秒报告一次测试结果
Initializing random number generator from current time


Initializing worker threads...

Threads started!
#下面是每5秒返回一次的结果,统计的指针包括:
线程数 thds 4
tps(每秒事务数)
qos(每秒查询数)
每秒的读/写/其他次数、延迟、每秒错误数、每秒重连次数

[ 5s ] thds: 4 tps: 255.65 qps: 5116.01 (r/w/o: 3581.31/1022.60/512.10) lat (ms,95%): 33.12 err/s: 0.00 reconn/s: 0.00
[ 10s ] thds: 4 tps: 288.10 qps: 5770.98 (r/w/o: 4041.79/1153.00/576.20) lat (ms,95%): 33.12 err/s: 0.00 reconn/s: 0.00
[ 15s ] thds: 4 tps: 295.61 qps: 5912.92 (r/w/o: 4138.29/1183.42/591.21) lat (ms,95%): 33.12 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 4 tps: 329.40 qps: 6585.11 (r/w/o: 4610.34/1315.98/658.79) lat (ms,95%): 31.37 err/s: 0.00 reconn/s: 0.00
SQL statistics:
    queries performed:
        read:                            81872  #执行的读操作数量 
        write:                           23392  #执行的写操作数量
        other:                           11696  #执行的其它操作数量
        total:                           116960 #总执行的操作数量
    transactions:                        5848   (292.11 per sec.)   #总事务数(每秒事务数)
    queries:                             116960 (5842.23 per sec.)  #查询总数
    ignored errors:                      0      (0.00 per sec.)     #忽略错误数
    reconnects:                          0      (0.00 per sec.)     #重新连接数


General statistics:
    total time:                          20.0173s  # 总消耗时间
    total number of events:              5848      # 总请求数量(读、写、其它)

Latency (ms):                                           #等待时间
         min:                                    3.86   #最小耗时
         avg:                                   13.68   #平均耗时
         max:                                   73.65   #最长耗时
         95th percentile:                       32.53   #超过99%平均耗时
         sum:                                80011.23   #总时间

Threads fairness:                                       #并发统计
    events (avg/stddev):           1462.0000/3.00       #总处理事件数/标准偏差
    execution time (avg/stddev):   20.0028/0.00         #平均处理事件时间/标准偏差

 

在这里插入图片描述

(2)、测试结果

在这里插入图片描述
在这里插入图片描述

(3)、删除测试数据
sysbench --threads=4 \
--time=20 \
--report-interval=5 \
--mysql-host=127.0.0.1 \
--mysql-port=3306 \
--mysql-user=root \
--mysql-password=abc123 \
/usr/share/sysbench/oltp_read_write.lua \
--tables=100 \
--table_size=100000 \
cleanup #删除刚刚的数据

在这里插入图片描述


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值