ienki 发表于 2018-9-29 10:39:25

使用Sysbench 进行Mysql 压力测试

  安装压力测试工具:sysbench
  1> 安装依赖环境包:
  yum install -y bzr
  yum install -y libtool
  下载主程序包:
  bzr branch lp:sysbench   //下载到当前目录,文件名为: sysbench
  开始安装:
  cd sysbench
  ./autogen.sh
  ./configure --with-mysql-includes=/usr/local/mysql/include --with-mysql-libs=/usr/local/mysql/lib
  make
  make install
  拷贝执行文件到环境变更下面:
  cp bin/sysbench /usr/local/bin/
  查询帮助命令:
  # sysbench --help
  数据测试参数说明:
  测试工具:sysbench 0.5
  测试流程:通过对每种规格的实例进行压测,得出每种规格的RDS实例的性能表现
  测试模型:采用OLTP模型(读写比例为7:3)和InnoDB引擎。大致命令如下:
  常用命令参数:
  /usr/local/sysbench-0.5/bin/sysbench
  --mysql-host=test.mysql.rds.aliyuncs.com         #数据库host
  --mysql-port=3306                                              #数据库端口
  --mysql-user=your_username                           #数据库用户名
  --mysql-password=your_password                      #数据库密码
  --mysql-db=your_db_for_test                              #数据库名
  --oltp-tables-count=10                        #模拟的表的个数,规格越高该值越大
  --oltp-table-size=6000000                  #模拟的每张表的行数,规格越高该值越大
  --num-threads=50                              #模拟的并发数量,规格越高该值越大
  --max-requests=100000000               #最大请求次数
  --max-time=20                           #最大测试时间(与--max-requests只要有一个超过,则退出)
  --report-interval=1                     #每1秒打印一次当前的QPS等值
  --test=/tmp/sysbench-0.5/sysbench/tests/db/oltp.lua    #选用的测试脚本(lua),此脚本可以从sysbench-0.5源代码文件目录下找
           #prepare准备数据,run执行测试,cleanup清理数据
  --oltp_table_count=1:指定测试过程中表的个数,0.5新增,0.4整个测试过程只有一个表。
  --oltp-table-size=:指定表的大小,如果指定1000,那么它会往表里初始化1000条数据
  --rand-init=on:是否随机初始化数据,如果不随机化那么初始好的数据每行内容除了主键不同外其他完全相同。
  --num-threads=:测试过程中并发线程数,看测试要求来定并发压力。
  --otlp-read-only=off:知否只读测试
  --report-interval=10:每隔多久打印一次统计信息,单位秒,0.5新增
  参考页面:http://help.aliyun.com/view/11108238_13440406.html
  测试初始化数据:
  # sysbench--test=tests/db/oltp.lua--mysql-table-engine=innodb --oltp-table-size=1000000--max-requests=5 --max-time=300 --num-threads=16 --oltp-tables-count=10
  --report-interval=10--mysql-host=localhost --mysql-port='3306' --mysql-user='xiong'--mysql-password='123456'--mysql-db='test' prepare
  #导数据 耗时较长
  ./sysbench –test=tests/db/oltp.lua –max-time=7200 –oltp-dist-type=uniform –max-requests=0 \
  –mysql-user=root –mysql-table-engine=innodb –oltp-table-size=5000000 –oltp-tables-count=48 \
  –oltp-range-size=40 –oltp-point-selects=10 –oltp-simple-ranges=1 –oltp-sum-ranges=1 \
  –oltp-order-ranges=1 –oltp-distinct-ranges=1 –oltp-index-updates=1 –oltp-non-index-updates=1 \
  –num-threads=200 –mysql-socket=/u01/mysql/run/mysql.sock \
  –oltp-read-only=on perpare
  #开始压测 混合读写
  ./sysbench –test=tests/db/oltp.lua –max-time=7200 –oltp-dist-type=uniform –max-requests=0–mysql-user=root –mysql-table-engine=innodb –oltp-table-size=5000000 –oltp-tables-count=48 \
  –oltp-range-size=40 –oltp-point-selects=10 –oltp-simple-ranges=1 –oltp-sum-ranges=1 –oltp-order-ranges=1 –oltp-distinct-ranges=1 –oltp-index-updates=1 –oltp-non-index-updates=1 \
  –num-threads=200 –mysql-socket=/u01/mysql/run/mysql.sockrun
  #开始压测 只读
  ./sysbench –test=tests/db/oltp.lua –max-time=7200 –oltp-dist-type=uniform –max-requests=0–mysql-user=root –mysql-table-engine=innodb –oltp-table-size=5000000 –oltp-tables-count=48 \
  –oltp-range-size=40 –oltp-point-selects=10 –oltp-simple-ranges=1 –oltp-sum-ranges=1–oltp-order-ranges=1 –oltp-distinct-ranges=1 –oltp-index-updates=1 –oltp-non-index-updates=1 \
  –num-threads=200 –mysql-socket=/u01/mysql/run/mysql.sock–oltp-read-only=on run
  操作系统参数测试:
  CPU性能测试
  根据官网的介绍可知:CPU测试使用64位整数,测试计算素数直到某个最大值所需要的时间。
  # sysbench --test=cpu --cpu-max-prime=20000 run
  sysbench 0.5:multi-threaded system evaluation benchmark
  Running the test with following options:
  Number of threads: 1
  Random number generator seed is 0 and will be ignored
  Primer numbers limit: 20000
  Threads started!
  General statistics:
  total time:                        34.3813s
  total number of events:            10000
  total time taken by event execution: 34.3759s
  response time:
  min:                                  3.40ms
  avg:                                  3.44ms
  max:                                  8.00ms
  approx.95 percentile:               3.45ms
  Threads fairness:
  events (avg/stddev):         10000.0000/0.00
  execution time (avg/stddev):   34.3759/0.00
  线程(thread)测试
  测试线程调度器的性能。对于高负载情况下测试线程调度器的行为非常有用:
  # sysbench --test=threads --num-threads=64 run
  sysbench 0.5:multi-threaded system evaluation benchmark
  Running the test with following options:
  Number of threads: 64
  Random number generator seed is 0 and will be ignored
  Threads started!
  General statistics:
  total time:                        2.3363s
  total number of events:            10000
  total time taken by event execution: 148.9687s
  response time:
  min:                                  0.25ms
  avg:                                 14.90ms
  max:                              201.12ms
  approx.95 percentile:            67.48ms
  Threads fairness:
  events (avg/stddev):         156.2500/16.95
  execution time (avg/stddev):   2.3276/0.00
  互斥锁(mutex)
  测试互斥锁的性能,方式是模拟所有线程在同一时刻并发运行,并都短暂请求互斥锁
  # sysbench --test=mutex --num-threads=16 --mutex-num=2048 --mutex-locks=1000000 --mutex-loops=5000 run
  sysbench 0.5:multi-threaded system evaluation benchmark
  Running the test with following options:
  Number of threads: 16
  Random number generator seed is 0 and will be ignored
  Threads started!
  General statistics:
  total time:                        7.7748s
  total number of events:            16
  total time taken by event execution: 123.2084s
  response time:
  min:                               7550.27ms
  avg:                               7700.52ms
  max:                               7774.71ms
  approx.95 percentile:            7773.72ms
  Threads fairness:
  events (avg/stddev):         1.0000/0.00
  execution time (avg/stddev):   7.7005/0.07
  内存测试测试了内存的连续读写性能
  上面这条语句指定了整个测试过程中,传输2G的数据量,每个block的大小为8K(大写的K)。 测试结果如下所示,我们最关心的是吞吐量(8030.45MB/sec),和后面的磁盘io 测试结果比较可知,内存的连续读写比磁盘的连续读写快十几倍
  # sysbench --test=memory --memory-block-size=8K --memory-total-size=2G--num-threads=16 run
  sysbench 0.5:multi-threaded system evaluation benchmark
  Running the test with following options:
  Number of threads: 16
  Random number generator seed is 0 and will be ignored
  Threads started!
  Operations performed: 262144 (695055.19 ops/sec)
  2048.00 MB transferred (5430.12 MB/sec)
  General statistics:
  total time:                        0.3772s
  total number of events:            262144
  total time taken by event execution: 3.2142s
  response time:
  min:                                  0.00ms
  avg:                                  0.01ms
  max:                                  2.28ms
  approx.95 percentile:               0.05ms
  Threads fairness:
  events (avg/stddev):         16384.0000/578.01
  execution time (avg/stddev):   0.2009/0.00
  文件IO基准测试
  文件IO(fileio)基准测试可以测试系统在不同IO负载下的性能。这对于比较不同的硬盘驱动器,不同的RAID 卡,不同的RAID 模式,都很有帮助。可以根据测试结果调整IO子系统。文件IO基准测试模拟了很多InnoDB 的IO特性。
  测试的第一步是准备(Prepare)阶段,生成测试用到的数据文件,生成的数据文件至少要比内存大。 如果文件中的数据能完全放入内存中,则操作系统 缓存大部分的数据,导致测试结果无法体现IO密集型的工作负载。首先通过下面的命令创建一个数据集:
  sysbench --test=fileio --file-total-size=40G prepare
  这个命令会在当前工作目录下创建测试文件,后续的运行(run)阶段将通过读写这些文件进行测试。 第二步就是运行(run)阶段,针对不同的IO 类型有不同的测试选项:
  seqwr 顺序写入
  seqrewr 顺序重写
  seqrd 顺序读取
  rndrd 随机读取
  rndwr 随机写入
  rndrw 混合随机读/写
  下面的命令运行文件I/O混合随机读/写基准测试:
  sysbench --test=fileio --file-total-size=40G --file-test-mode=rndrw\
  --init-rng=on --max-time=300 --max-requests=0 run
  simple 与 --oltp-read-only 的区别
  simple模式和在complex模式下开启read-only选项都只包含select语句。但是 simple 模式只包含最简单的select语句,相反地,complex 模式中,如果我们开启read-only 选项,即--oltp-read-only=on,则会包含复杂的SQL语句。如:

  SELECT SUM(K) FROM sbtest WHERE>
  SELECT DISTINCT c FROM sbtest WHERE>  测试自有的存储引擎
  测试自有的存储引擎需要告诉sysbench,这个存储引擎是否支持事务。
  如下所示:
  准备
  sysbench --test=oltp --mysql-table-engine=tnt --mysql-engine-trx=yes \
  --oltp-table-size=100000 --mysql-user=root --mysql-db=test \
  --mysql-socket=/data/ntse/lmx/sysbench/var/mysqld.sock \
  prepare
  测试
  sysbench --test=oltp --mysql-table-engine=tnt --mysql-engine-trx=yes \
  --oltp-table-size=100000 --mysql-user=root --mysql-db=test \
  --mysql-socket=/data/ntse/lmx/sysbench/var/mysqld.sock \
  --oltp-test-mode=complex --num-threads=16 --max-time=720 \
  --max-requests=0 run
  清除
  sysbench --test=oltp --mysql-table-engine=tnt --mysql-engine-trx=yes \
  --oltp-table-size=100000 --mysql-user=root --mysql-db=test \
  --mysql-socket=/data/ntse/lmx/sysbench/var/mysqld.sock \
  clean
  4. sysbench 0.5

页: [1]
查看完整版本: 使用Sysbench 进行Mysql 压力测试