什么是基准测试
定义
基准测试是一种测量和评估软件性能指标的活动,用于建立某个时刻的性能基准,以便当系统发生软硬件变化时重新进行基准测试以评估变化对性能的影响
基准测试是针对系统设置的一种压力测试,但是和压力测试还是有区别的
- 基准测试:直接、简单,易于比较,用于评估服务器的处理能力
- 基准测试:可能不关心业务逻辑,所使用的查询和业务的真实性可以和业务环境没有关系
- 压力测试:对真实的业务数据进行测试,获得真实系统所能承受的压力
- 压力测试:需要针对不同的应用场景,所使用的数据和查询也是真实用到的
如何进行基准测试
基准测试的目的
- 建立Mysql服务器的性能基准线
- 模拟比当前系统更高的负载,以找出系统的扩张瓶颈,增加数据库的并发,观察QPS、TPS变化,确定并发量与性能最优的关系
- 测试不同硬件、软件、操作系统配置(mysql版本,磁盘配置等)
- 证明新的硬件设备是否配置正确
如何进行基准测试
对整个系统进行基准测试
- 优点:能够测试整个系统性能,包括web服务器缓存、数据库等,能反映出系统中各个组件接口间的性能问题,体现真实性能状况
- 缺点:测试设计复杂,消耗时间长
- 从系统入口进行测试(如网站web前端,手机APP前端)
单独对Mysql进行基准测试
- 优点:测试设计简单,所需耗费时间短
- 缺点:无法全面了解整个系统的性能基准线
mysql基准测试的性能指标
- 单位时间内所处理的事务数(TPS)
- 单位时间内所处理的查询数(QPS)
- 响应时间(平均响应时间,最小响应时间,最大响应时间,各时间所占百分比)
- 并发量:同时处理的查询请求的数量(并发量不等于连接数)
基准测试的步骤
计划和设计基准测试
- 对整个系统还是某一组件
- 使用什么样的数据(比如使用生产环境的数据库备份)
- 准备基准测试和数据收集脚本(CPU使用率、IO、网络流量、状态和计数器信息等)
- 运行基准测试
- 保存和分析基准测试结果
基准测试中容易忽略的问题
- 使用生产环境数据时,只使用了部分数据(应使用全部备份数据)
- 在多用户场景中,只做了单用户的测试
- 在单服务器上测试分布式应用(推荐使用相同架构进行测试)
- 反复执行同一查询(容易缓存命中,无法反应真实查询性能)
常用的基准测试工具介绍
Mysql基准测试工具之mysqlslap
mysql5.1版本后自带的基准测试工具,随Mysql一起安装
特点:
- 可以模拟服务器负载,并输出相关统计信息
- 可以指定也可以自动生成查询语句
# mysqlslap --help 查看帮助信息
--auto-generate-sql #由系统自动生成SQL脚本进行测试
--auto-generate-sql-add-autoincrement #在生成的表中增加自增的ID
--auto-generate-sql-load-type #指定测试中使用的查询类型
--auto-generate-sql-write-number #指定初始化数据时生成的数据量
--concurrency #指定并发线程的数量
--engine #指定测试表的存储引擎,可以用逗号分隔多个表引擎
--no-drop #指定不清理测试数据
--iterations #指定测试运行的次数
--number-of-queries #指定每一个线程执行的查询数量
-number-int-cols #指定测试表中包含int类型列的数量
--number-char-cols #指定测试表包含varchar类型列的数量
--create-schema #指定了用于测试的数据库的名字
--query #用于指定自定义SQL的脚本
--only-print #并不运行测试脚本,而是把生成的脚本打印出来
#开始测试
mysqlslap --concurrency=1,50,100,200 --iterations=3 --number-int-cols=5 --number-char-cols=10 --auto-generate-sql --auto-generate-sql-add-autoincrement --engine=myisam,innodb --number-of-queries=10 --create-schema=sqltest -p
#上述命令基准测试结果,可以看出innodb在并发时比myisam快很多倍
Benchmark
Running for engine myisam
Average number of seconds to run all queries: 0.015 seconds
Minimum number of seconds to run all queries: 0.015 seconds
Maximum number of seconds to run all queries: 0.016 seconds
Number of clients running queries: 1
Average number of queries per client: 10
Benchmark
Running for engine myisam
Average number of seconds to run all queries: 1.237 seconds
Minimum number of seconds to run all queries: 1.061 seconds
Maximum number of seconds to run all queries: 1.533 seconds
Number of clients running queries: 50
Average number of queries per client: 0
Benchmark
Running for engine myisam
Average number of seconds to run all queries: 2.559 seconds
Minimum number of seconds to run all queries: 2.525 seconds
Maximum number of seconds to run all queries: 2.598 seconds
Number of clients running queries: 100
Average number of queries per client: 0
Benchmark
Running for engine myisam
Average number of seconds to run all queries: 5.552 seconds
Minimum number of seconds to run all queries: 5.510 seconds
Maximum number of seconds to run all queries: 5.619 seconds
Number of clients running queries: 200
Average number of queries per client: 0
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.026 seconds
Minimum number of seconds to run all queries: 0.026 seconds
Maximum number of seconds to run all queries: 0.028 seconds
Number of clients running queries: 1
Average number of queries per client: 10
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.124 seconds
Minimum number of seconds to run all queries: 0.107 seconds
Maximum number of seconds to run all queries: 0.155 seconds
Number of clients running queries: 50
Average number of queries per client: 0
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.136 seconds
Minimum number of seconds to run all queries: 0.122 seconds
Maximum number of seconds to run all queries: 0.153 seconds
Number of clients running queries: 100
Average number of queries per client: 0
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.279 seconds
Minimum number of seconds to run all queries: 0.259 seconds
Maximum number of seconds to run all queries: 0.313 seconds
Number of clients running queries: 200
Average number of queries per client: 0
Mysql基准测试工具之sysbench
sysbench的安装
#源码安装的方式
wget https://github.com/akopytov/sysbench/archive/0.5.zip
unzip 0.5.zip
cd sysbench-0.5/
#指定mysql的include目录和libs目录,这里需要根据实际环境。
#可以通过whereis mysql查看
#[root@fu020 mysql]# whereis mysql
#mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql #/usr/share/man/man1/mysql.1.gz
./configure --with-mysql-includes=/usr/include/mysql --with-mysql-libs=/usr/lib64/mysql
make && make install
sysbench --help
#centos可以用yum
yum install sysbench
sysbench常用参数
--test #用于指定所要执行的测试类型,支持以下参数
Fileio #文件系统I/O性能测试
cpu #cpu性能测试
memory #内存性能测试
oltp #测试要指定具体的lua脚本
#lua脚本位于 sysbench-0.5/sysbench/tests/db
--mysql-db #用于指定执行基准测试的数据库名
--mysql-table-engine #用于指定所使用的存储引擎
--oltp-tables-count #执行测试的表数量
--oltp-table-size #指定每个表中的数据行数
--max-time #指定最大的测试时间
--report-interval #指定间隔多长时间输出一次统计信息
--mysql-user #指定执行测试的mysql用户
--mysql-password #指定执行测试的mysql用户的密码
prepare #用于准备测试数据
run #用于实际执行测试
cleanup #用于清理测试数据
sysbench测试
1.测试CPU
sysbench --test=cpu --cpu-max-prime=10000 run #只计算了单核
2.测试I/O
#生成测试I/O的文件,这里是生成了2G
cd /tmp
sysbench --test=fileio --file-total-size=2G prepare
#执行I/O测试
#以下是采用20个进程,随机读写,1秒生成测试报告
sysbench --test=fileio --num-threads=20 --init-rng=on --file-total-size=2G --file-test-mode=rndrw --report-interval=1 run
#I/O测试数据
[root@fu020 tmp]# sysbench --test=fileio --num-threads=20 --init-rng=on --file-total-size=2G --file-test-mode=rndrw --report-interval=1 run
sysbench 0.5: multi-threaded system evaluation benchmark
Running the test with following options:
Number of threads: 20
Report intermediate results every 1 second(s)
Random number generator seed is 0 and will be ignored
Extra file open flags: 0
128 files, 16Mb each
2Gb total file size
Block size 16Kb
Number of IO requests: 10000
Read/Write ratio for combined random IO test: 1.50
Periodic FSYNC enabled, calling fsync() each 100 requests.
Calling fsync() at the end of test, Enabled.
Using synchronous I/O mode
Doing random r/w test
Initializing worker threads...
Threads started!
[ 1s] reads: 17.94 MB/s writes: 11.97 MB/s fsyncs: 2413.70/s response time: 16.267ms (95%)
[ 2s] reads: 17.84 MB/s writes: 11.89 MB/s fsyncs: 2434.03/s response time: 16.786ms (95%)
[ 3s] reads: 17.23 MB/s writes: 11.48 MB/s fsyncs: 2320.05/s response time: 19.386ms (95%)
[ 4s] reads: 16.41 MB/s writes: 10.81 MB/s fsyncs: 2204.98/s response time: 16.973ms (95%)
[ 5s] reads: 16.02 MB/s writes: 10.83 MB/s fsyncs: 2272.00/s response time: 20.533ms (95%)
Operations performed: 6008 reads, 3992 writes, 12800 Other = 22800 Total
Read 93.875Mb Written 62.375Mb Total transferred 156.25Mb (28.462Mb/sec)
1821.55 Requests/sec executed
General statistics:
total time: 5.4898s
total number of events: 10000
total time taken by event execution: 58.1353s
response time:
min: 0.00ms
avg: 5.81ms
max: 69.19ms
approx. 95 percentile: 17.93ms
Threads fairness:
events (avg/stddev): 500.0000/34.00
execution time (avg/stddev): 2.9068/0.09
3.测试数据库
测试数据库,需要先建立测试用的数据库,和测试用的用户
mysql> create database sysbenchtest;
#以下可以省略用现有用户
mysql> grant all privileges on *.* to sbtest@'localhost' identified by '$3F4286';
#进入lua脚本测试目录 /sysbench-0.5/sysbench/tests/db
cd /hotdata/soft/sysbench-0.5/sysbench/tests/db
//======================================================================
//下方为sysbench0.4.12的使用方式,这个版本不能一次生成多张表,不能使用--oltp-tables-count参数
#准备数据库测试数据,注意如果是使用yum安装的,需要指定--db-driver,还有就是密码最好用单引号包住
#在sysbenchtest测试库中生成了一个100W条数据的innodb表
sysbench --test=oltp --mysql-table-engine=innodb --mysql-db=sysbenchtest --oltp-table-size=1000000 --mysql-user=root --mysql-password='youpassword' --db-driver=mysql prepare
//======================================================================
//下方为sysbench0.5的使用方式,这个版本可以一次生成多张表,使用--oltp-tables-count参数
//以下生成10张100W数据的表
sysbench --test=./oltp.lua --mysql-table-engine=innodb --mysql-db=sysbenchtest --oltp-table-size=1000000 --mysql-user=root --mysql-password='youpassword' --oltp-tables-count=10 --mysql-socket=/hotdata/database/data/mysql/mysql.sock prepare
#启用数据库信息收集
bash get_test_info.sh #下方有贴出代码
#执行测试
sysbench --test=./oltp.lua --mysql-table-engine=innodb --mysql-db=sysbenchtest --oltp-table-size=1000000 --mysql-user=root --mysql-password='youpassword' --oltp-tables-count=10 --mysql-socket=/hotdata/database/data/mysql/mysql.sock run
#使用下方提供的analysis.sh脚本分析收集的数据
./analysis.sh /hotdata/temp/benchmarks/5-sec-status-2017-01-08_08-status
//基准测试
[root@fu020 db]# sysbench --test=./oltp.lua --mysql-table-engine=innodb --mysql-db=sysbenchtest --oltp-table-size=1000000 --mysql-user=root --mysql-password='youpassword' --oltp-tables-count=10 --mysql-socket=/hotdata/database/data/mysql/mysql.sock 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
Initializing worker threads...
Threads started!
OLTP test statistics:
queries performed:
read: 140000
write: 40000
other: 20000
total: 200000
transactions: 10000 (30.35 per sec.)
read/write requests: 180000 (546.34 per sec.)
other operations: 20000 (60.70 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 329.4640s
total number of events: 10000
total time taken by event execution: 329.4293s
response time:
min: 5.30ms
avg: 32.94ms
max: 769.65ms
approx. 95 percentile: 120.47ms
Threads fairness:
events (avg/stddev): 10000.0000/0.00
execution time (avg/stddev): 329.4293/0.00
#get_test_info.sh
#!/bin/bash
INTERVAL=5 #脚本运行间隔
PREFIX=/hotdata/temp/benchmarks/$INTERVAL-sec-status #状态信息记录到什么位置
RUNFILE=/hotdata/temp/benchmarks/running #运行标识
echo "1" > $RUNFILE #生成运行标识
MYSQL=/usr/bin/mysql #mysql命令的路径
$MYSQL -e "show global variables" >> mysql-variables #mysql的设置信息
while test -e $RUNFILE; do
file=$(date +%F_%I)
sleep=$(date +%s.%N | awk '{print 5 - ($1 % 5)}')
sleep $sleep #脚本的运行时间
ts="$(date +"TS %s.%N %F %T")"
loadavg="$(uptime)" #收集脚本的负载情况
echo "$ts $loadavg" >> $PREFIX-${file}-status #记录到文件中
$MYSQL -e "show global status" >> $PREFIX-${file}-status & #收集mysql全局状态信息并记录到文件
echo "$ts $loadavg" >> $PREFIX-${file}-innodbstatus
$MYSQL -e "show engine innodb status" >> $PREFIX-${file}-innodbstatus & #收集innodb状态信息
echo "$ts $loadavg" >> $PREFIX-${file}-processlist
$MYSQL -e "show full processlist\G" >> $PREFIX-${file}-processlist &
echo $ts
done
echo Exiting because $RUNFILE does not exists
#Analysis.sh
#!/bin/bash
awk '
BEGIN {
printf "#ts date time load QPS";
fmt=" %.2f";
}
/^TS/ {
ts = substr($2,1,index($2,".")-1);
load = NF -2;
diff = ts - prev_ts;
printf "\n%s %s %s %s",ts,$3,$4,substr($load,1,length($load)-1);
prev_ts=ts;
}
/Queries/{
printf fmt,($2-Queries)/diff;
Queries=$2
}
' "$@"
参考链接:
mysqlslap 使用总结
sysbench安装、使用、结果解读
sysbench 安装、使用和测试
mysql benchmark基准测试