为什么要基准测试
基准测试是唯一方便有效的、可以学习系统在给丁的工作负载下会发生什么的方法。
一个完善的基准测试可以:
验证基于系统的一些假设,确认该假设是否符合实际情况
重现一些异常行为,有备无患的得到解决方法
获取当前系统性能和运行情况,为系统优化提供先决条件
模拟比当前系统更高的负载,找到系统的扩展瓶颈
规划未来业务增长,包括硬件、网络容量、服务类型、及其他相关资源
测试应用使用可变环境的能力
测试硬件,包括硬件设备是否适合当前服务,配置是否正确等
基准测试策略
基准测试主要有两种策略:
集成式 | 单组件式 |
---|---|
对整个系统 | 单独测试Mysql |
针对整体性能 | 针对mysql本身性能 |
难建立、难基准 | 相对简单 |
测试目标
测试对象 | |
---|---|
吞吐量 | 单位时间内的事务处理 |
响应时间 | 测试任务所需要的时间 |
并发性 | 任意时间内同时发生的并发请求 |
可扩展性 | 业务压力发生变化时,系统工作量增加或者系统资源增加 |
基准测试工具
集成式测试工具
ab
ab是一个Apache HTTP服务器基准测试工具。可以测试HTTP服务器每秒最多处理多少请求。如果测试的是Web服务,可以转换成整个应用每秒满足多少请求。但是用途有限,只能针对单个URL进行尽可能快的压力测试。
用法: ab(选项)(参数) 选项: -A:指定连接服务器的基本的认证凭据; -c:指定一次向服务器发出请求数; -C:添加cookie; -g:将测试结果输出为“gnuolot”文件; -h:显示帮助信息; -H:为请求追加一个额外的头; -i:使用“head”请求方式; -k:激活HTTP中的“keepAlive”特性; -n:指定测试会话使用的请求数; -p:指定包含数据的文件; -q:不显示进度百分比; -T:使用POST数据时,设置内容类型头; -v:设置详细模式等级; -w:以HTML表格方式打印结果; -x:以表格方式输出时,设置表格的属性; -X:使用指定的代理服务器发送请求; -y:以表格方式输出时,设置表格属性。 参数: 主机:被测试主机。
http_load
对web服务器进行测试,比ab更加强大。可以通过输入文件提供多个URL,按照一定规则进行压力测试。
编译安装http_load:
一、安装 1、wget http://www.acme.com/software/http_load/http_load-14aug2014.tar.gz 2、tar zxvf http_load-14aug2014.tar.gz 3、cd http_load-14aug2014 进入http_load目录 4、sudo yum install make 5、sudo yum install gcc 安装gcc编译器 6、mkdir /usr/local/man 创建man目录 7、sudo make 8、sudo make install
http_load几个常用的参数:
参数 | 全称 | 含义 |
---|---|---|
-p | -parallel | 并发的用户进程数。 |
-f | -fetches | 总计的访问次数 |
-r | -rate | 含义是每秒的访问频率 |
-s | -seconds | 连续的访问时间 |
url | 网站连接地址或url文件 |
简单的使用示例
创建一个urls.tx文件,存放一些URL
[root@mysql_master ~]# cat urls.txt http://localhost/test1 http://localhost/test2 http://localhost/test3 http://localhost/
循环请求给定的URL列表,测试程序以最快的速度请求这些URL
[root@mysql_master ~]# http_load -p 1 -s 10 urls.txt 49868 fetches, 1 max parallel, 8.81929e+06 bytes, in 10 seconds 连接数 最大并发进程数 10秒内总传输速率 176.853 mean bytes/connection 每次请求平均传输数据量 4986.79 fetches/sec, 881927 bytes/sec 每秒连接数 每秒传输数据量 msecs/connect: 0.0458587 mean, 7.747 max, 0.021 min 每次连接平均响应时间 0.045毫秒 最大7.47 最小0.021 msecs/first-response: 0.154671 mean, 5.722 max, 0.056 min 每次连接平均返回时间 HTTP response codes: 涉及到的http状态码 code 200 -- 12501 //正常连接 code 301 -- 37367 //永久重订向
模拟五个并发用户进行请求
[root@mysql_master ~]# http_load -p 5 -s 10 urls.txt 73049 fetches, 5 max parallel, 1.29502e+07 bytes, in 10 seconds 177.282 mean bytes/connection 7304.89 fetches/sec, 1.29502e+06 bytes/sec msecs/connect: 0.0888493 mean, 4.913 max, 0.011 min msecs/first-response: 0.499977 mean, 12.307 max, 0.067 min HTTP response codes: code 200 -- 18167 code 301 -- 54882
模拟访问请求率进行压力测试
[root@mysql_master ~]# http_load -r 5 -s 10 urls.txt -r == -rate 每秒访问5次 49 fetches, 1 max parallel, 9807 bytes, in 10 seconds 200.143 mean bytes/connection 4.9 fetches/sec, 980.7 bytes/sec msecs/connect: 0.0881837 mean, 0.191 max, 0.07 min msecs/first-response: 0.250184 mean, 0.35 max, 0.222 min HTTP response codes: code 200 -- 7 code 301 -- 42
Mysql基准测试套件
sql-bench
在Mysql的发行包中提供了自己的基准测试套件,可以用于在不同数据库服务器上进行比较测试。它是单线程的,主要用于测试服务器执行查询的速度。结果会显示那些操作在服务器上执行更快。
sysbench
sysbench是一款多线程系统测压工具(不仅仅是针对mysql),可以根据影响数据库服务器性能的各个因素来评估系统的性能。如:文件IO、操作系统调度器、内存分配、传输线程、POSIX线程、等
sysbench的cpu基准测试
获取PC的cpu配置信息
[root@mysql_master ~]# cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 42 model name : Intel Xeon E312xx (Sandy Bridge) stepping : 1 microcode : 0x1 cpu MHz : 2491.904 cache size : 4096 KB physical id : 0 siblings : 1 core id : 0 cpu cores : 1 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm xsaveopt bogomips : 4983.80 clflush size : 64 cache_alignment : 64 address sizes : 36 bits physical, 48 bits virtual power management: processor : 1 vendor_id : GenuineIntel cpu family : 6 model : 42 model name : Intel Xeon E312xx (Sandy Bridge) stepping : 1 microcode : 0x1 cpu MHz : 2491.904 cache size : 4096 KB physical id : 1 siblings : 1 core id : 0 cpu cores : 1 apicid : 1 initial apicid : 1 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm xsaveopt bogomips : 4983.80 clflush size : 64 cache_alignment : 64 address sizes : 36 bits physical, 48 bits virtual power management:
运行测试
[root@mysql_master ~]# sysbench --test=cpu --cpu-max-prime=2000 run WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options. sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2) Running the test with following options: Number of threads: 1 Initializing random number generator from current time Prime numbers limit: 2000 Initializing worker threads... Threads started! CPU speed: events per second: 6817.21 General statistics: total time: 10.0001s total number of events: 68194 Latency (ms): min: 0.10 avg: 0.15 max: 0.91 95th percentile: 0.21 sum: 9966.51 Threads fairness: events (avg/stddev): 68194.0000/0.00 execution time (avg/stddev): 9.9665/0.00
sysbench的文件IO基准测试
文件IO基准测试可以测试系统在不同IO负载下的性能。这对不同的硬盘驱动器、RAID卡、RAID模式都很有用。
数据准备
生成测试用到的数据文件,原则上数据文件至少比内存大。如果测试数据可以完全放入内存中,则操作系统缓存大部分数据,不能体现IO密集型的工作负载
[root@mysql_master ~]# sysbench --test=fileio --file-total-size=2G prepare WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options. sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2) 128 files, 16384Kb each, 2048Mb total Creating files for the test... Extra file open flags: (none) Creating file test_file.0 Creating file test_file.1 Creating file test_file.2 Creating file test_file.3 Creating file test_file.4 。。。 Creating file test_file.125 Creating file test_file.126 Creating file test_file.127 2147483648 bytes written in 57.44 seconds (35.66 MiB/sec). [root@mysql_master ~]# ls test_file.* test_file.0 test_file.121 test_file.30 test_file.54 test_file.78 test_file.1 test_file.122 test_file.31 test_file.55 test_file.79 test_file.10 test_file.123 test_file.32 test_file.56 test_file.8 test_file.100 test_file.124 test_file.33 test_file.57 test_file.80 test_file.101 test_file.125 test_file.34 test_file.58 test_file.81 test_file.102 test_file.126 test_file.35 test_file.59 test_file.82 test_file.103 test_file.127 test_file.36 test_file.6 test_file.83 test_file.104 test_file.13 test_file.37 test_file.60 test_file.84 test_file.105 test_file.14 test_file.38 test_file.61 test_file.85 test_file.106 test_file.15 test_file.39 test_file.62 test_file.86 test_file.107 test_file.16 test_file.4 test_file.63 test_file.87 test_file.108 test_file.17 test_file.40 test_file.64 test_file.88 test_file.109 test_file.18 test_file.41 test_file.65 test_file.89 test_file.11 test_file.19 test_file.42 test_file.66 test_file.9 test_file.110 test_file.2 test_file.43 test_file.67 test_file.90 test_file.111 test_file.20 test_file.44 test_file.68 test_file.91 test_file.112 test_file.21 test_file.45 test_file.69 test_file.92 test_file.113 test_file.22 test_file.46 test_file.7 test_file.93 test_file.114 test_file.23 test_file.47 test_file.70 test_file.94 test_file.115 test_file.24 test_file.48 test_file.71 test_file.95 test_file.116 test_file.25 test_file.49 test_file.72 test_file.96 test_file.117 test_file.26 test_file.5 test_file.73 test_file.97 test_file.118 test_file.27 test_file.50 test_file.74 test_file.98 test_file.119 test_file.28 test_file.51 test_file.75 test_file.99 test_file.12 test_file.29 test_file.52 test_file.76 test_file.120 test_file.3 test_file.53 test_file.77
运行测试
针对不同的IO类型有不同的测试选项:
IO类型 意义 seqwr 顺序写入 seqrewr 顺序重写 seqrd 顺序读取 rndrd 随机读取 rndwr 随机写入 rndrw 随机读写 [root@mysql_master ~]# sysbench --test=fileio --file-total-size=2G --file-test-mode=rndrw --max-time=300 --max-requests=0 run WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options. WARNING: --max-time is deprecated, use --time instead sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2) Running the test with following options: Number of threads: 1 //线程数 Initializing random number generator from current time Extra file open flags: (none) 128 files, 16MiB each //128个文件,每个文件16M 2GiB total file size //测试总大小2G Block size 16KiB //块大小16K Number of IO requests: 0 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! File operations: //文件操作 reads/s: 41.81 //每秒读取 writes/s: 27.88 //每秒写入 fsyncs/s: 89.35 //每秒同步文件 Throughput: //吞吐量 read, MiB/s: 0.65 //每秒读取量 written, MiB/s: 0.44 //每秒写入量 General statistics: //一般统计 total time: 301.3289s //总共耗时 total number of events: 47796 //事件总数 Latency (ms): min: 0.00 avg: 6.28 max: 531.89 95th percentile: 23.10 sum: 299921.39 Threads fairness: events (avg/stddev): 47796.0000/0.00 execution time (avg/stddev): 299.9214/0.00
可以看出,IO子系统密切相关的包括每秒文件操作和吞吐量。
清除测试文件
[root@mysql_master ~]# sysbench --test=fileio --file-total-size=2G cleanup WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options. sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2) Removing test files... 。。。
关于sysbench的测试对象
cpu
[root@mysql_master test]# sysbench cpu help sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2) cpu options: --cpu-max-prime=N upper limit for primes generator [10000]
使用64为整数,测试计算素数知道某个最大值所需要的时间,时间越短说明运行速度越快。
fleio
[root@mysql_master test]# sysbench fileio help sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2) fileio options: --file-num=N number of files to create [128] --file-block-size=N block size to use in all IO operations [16384] --file-total-size=SIZE total size of files to create [2G] --file-test-mode=STRING test mode {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw} --file-io-mode=STRING file operations mode {sync,async,mmap} [sync] --file-extra-flags=[LIST,...] list of additional flags to use to open files {sync,dsync,direct} [] --file-fsync-freq=N do fsync() after this number of requests (0 - don't use fsync()) [100] --file-fsync-all[=on|off] do fsync() after each write operation [off] --file-fsync-end[=on|off] do fsync() at the end of test [on] --file-fsync-mode=STRING which method to use for synchronization {fsync, fdatasync} [fsync] --file-merged-requests=N merge at most this number of IO requests if possible (0 - don't merge) [0] --file-rw-ratio=N reads/writes ratio for combined test [1.5]
文件IO一般分为两步:
准备数据:生成测试用到的数据文件,生成的数据文件至少要比内存大。如果文件中的数据可以完全放入内存,则操作系统缓存大部分数据,导致测试无法体现IO密集型工作负载。
[root@mysql_master test]# sysbench --test=fileio --file-total-size=4G prepare
运行阶段:针对不同的IO类型可以选择不同的测试选项:test mode {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw}
[root@mysql_master test]# sysbench --test=fileio --file-total-size=2G --file-test-mode=rndrw --max-time=300 --max-requests=0 run
memory
root@mysql_master test]# sysbench memory help sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2) memory options: --memory-block-size=SIZE size of memory block for test [1K] --memory-total-size=SIZE total size of data to transfer [100G] --memory-scope=STRING memory access scope {global,local} [global] --memory-hugetlb[=on|off] allocate memory from HugeTLB pool [off] --memory-oper=STRING type of memory operations {read, write, none} [write] --memory-access-mode=STRING memory access mode {seq,rnd} [seq]
可以对内存的连续读写性能进行测试,如:
[root@mysql_master test]# sysbench --test=memory --memory-block-size=2K --memory-total-size=2G run WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options. sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2) Running the test with following options: Number of threads: 1 Initializing random number generator from current time Running memory speed test with the following options: block size: 2KiB total size: 2048MiB operation: write scope: global Initializing worker threads... Threads started! Total operations: 1048576 (2261130.75 per second) 2048.00 MiB transferred (4416.27 MiB/sec) General statistics: total time: 0.4619s total number of events: 1048576 Latency (ms): min: 0.00 avg: 0.00 max: 0.07 95th percentile: 0.00 sum: 231.04 Threads fairness: events (avg/stddev): 1048576.0000/0.00 execution time (avg/stddev): 0.2310/0.00
mutex
[root@mysql_master test]# sysbench mutex help sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2) mutex options: --mutex-num=N total size of mutex array [4096] --mutex-locks=N number of mutex locks to do per thread [50000] --mutex-loops=N number of empty loops to do outside mutex lock [10000]
模拟所有线程在同一时刻并发运行,并都短暂请求互斥锁。
互斥锁是一种数据结构,用来对某些资源进行排他性访问控制,防止因并发导致的问题
sysbench的OLTP基准测试
OLTP基准测试模拟一个简单的事务处理系统的工作负载。
总结
学会使用一种基准测试工具,至少是sysbench,学习使用oltp和fileio测试。使用一种熟练的绘图工具如gnuplot等。