|
48 | 48 | - `--only-print` 打印压力测试的时候 mysqlslap 到底做了什么事,通过 sql 语句方式告诉我们。
|
49 | 49 |
|
50 | 50 |
|
| 51 | +## sysbench 工具 |
| 52 | + |
| 53 | +- 工具的官网说明:<https://launchpad.net/sysbench> |
| 54 | +- 开源地址:<https://github.com/akopytov/sysbench> |
| 55 | + |
| 56 | +### 安装 |
| 57 | + |
| 58 | +- 当前(201703)最新版本为:**1.0.3**,下面的操作也都是基于此版本,网络上的资料很多都是 0.4 和 0.5 不支持本文的语法。 |
| 59 | +- 安装编译相关工具包:`yum -y install automake libtool` |
| 60 | +- 下载:<https://github.com/akopytov/sysbench/releases> |
| 61 | +- 假设我这边下载下来的文件名为:`sysbench-1.0.3.zip` |
| 62 | +- 我的 MySQL 安装路径为:`/usr/program/mysql` |
| 63 | + - include 目录位置:`/usr/program/mysql/include` |
| 64 | + - libs 目录位置:`/usr/program/mysql/lib` |
| 65 | +- 设置 MySQL 包路径变量:`export LD_LIBRARY_PATH=/usr/program/mysql/lib/` |
| 66 | +- 解压压缩包:`unzip sysbench-1.0.3.zip` |
| 67 | +- 开始编译安装: |
| 68 | + - `cd sysbench-1.0.3` |
| 69 | + - `./autogen.sh` |
| 70 | + - `./configure --with-mysql-includes=/usr/program/mysql/include --with-mysql-libs=/usr/program/mysql/lib/` |
| 71 | + - `make` |
| 72 | + - `make install` |
| 73 | + - 测试是否安装成功:`sysbench --version` |
| 74 | +- 安装完之后在这个目录下有一些 lua 测试脚本:`cd /usr/local/share/sysbench`,等下测试的时候需要指定这些脚本位置,用这些脚本测试的。 |
| 75 | +- 默认这些脚本生成的数据都是 10000 个,如果你想要更多,需要修改:`vim /usr/local/share/sysbench/oltp_common.lua` 文件。常修改的参数: |
| 76 | + - `tables`,生成多少张表 |
| 77 | + - `table_size`,每张表多少记录数 |
| 78 | + |
| 79 | +### 开始测试 |
| 80 | + |
| 81 | +- 做不同的类型测试之前,最好都重启下 MySQL |
| 82 | +- 创建一个数据库,名字为:`sbtest` |
| 83 | +- select 测试: |
| 84 | + - 准备测试数据:`sysbench /usr/local/share/sysbench/oltp_point_select.lua --threads=15 --report-interval=10 --time=120 --mysql-user=root --mysql-password=123456... --mysql-host=127.0.0.1 --mysql-port=3306 prepare` |
| 85 | + - 开始测试:`sysbench /usr/local/share/sysbench/oltp_point_select.lua --threads=15 --report-interval=10 --time=120 --mysql-user=root --mysql-password=123456... --mysql-host=127.0.0.1 --mysql-port=3306 run` |
| 86 | + - 清除测试数据:`sysbench /usr/local/share/sysbench/oltp_point_select.lua --threads=15 --report-interval=10 --time=120 --mysql-user=root --mysql-password=123456... --mysql-host=127.0.0.1 --mysql-port=3306 cleanup` |
| 87 | +- 读写测试: |
| 88 | + - 读写测试我开了线程比较多,也修改了 oltp_common.lua 内容,有可能会报:`MySQL error: 1461 "Can't create more than max_prepared_stmt_count statements`,那你需要在 MySQL 中执行这句临时设置 SQL:`SET GLOBAL max_prepared_stmt_count=100000;` |
| 89 | + - 准备测试数据:`sysbench /usr/local/share/sysbench/oltp_read_write.lua --threads=200 --report-interval=10 --time=120 --mysql-user=root --mysql-password=123456... --mysql-host=127.0.0.1 --mysql-port=3306 prepare` |
| 90 | + - 开始测试:`sysbench /usr/local/share/sysbench/oltp_read_write.lua --threads=200 --report-interval=10 --time=120 --mysql-user=root --mysql-password=123456... --mysql-host=127.0.0.1 --mysql-port=3306 run` |
| 91 | + - 清除测试数据:`sysbench /usr/local/share/sysbench/oltp_read_write.lua --threads=200 --report-interval=10 --time=120 --mysql-user=root --mysql-password=123456... --mysql-host=127.0.0.1 --mysql-port=3306 cleanup` |
| 92 | + |
| 93 | +- 参数说明: |
| 94 | + - `--threads=15` 表示发起 15 个并发连接 |
| 95 | + - `--report-interval=10` 表示控制台每 10 秒输出一次测试进度报告 |
| 96 | + - `--time=120` 总的测试时长为 120 秒 |
| 97 | + - `--max-requests=0` 表示总请求数为 0,因为上面已经定义了总执行时长,所以总请求数可以设定为 0;也可以只设定总请求数,不设定执行时长 |
| 98 | + - `--percentile=99` 表示设定采样比例,即丢弃1%的长请求,在剩余的99%里取最大值。默认是 95%, |
| 99 | + |
| 100 | + |
| 101 | +### 测试报告 |
| 102 | + |
| 103 | +Running the test with following options: |
| 104 | +Number of threads: 15 |
| 105 | +Report intermediate results every 10 second(s) |
| 106 | +Initializing random number generator from current time |
| 107 | + |
| 108 | + |
| 109 | +Initializing worker threads... |
| 110 | + |
| 111 | +Threads started! |
| 112 | + |
| 113 | +[ 10s ] thds: 15 tps: 337.43 qps: 6773.72 (r/w/o: 4745.03/1351.92/676.76) lat (ms,95%): 73.13 err/s: 0.40 reconn/s: 0.00 |
| 114 | +[ 20s ] thds: 15 tps: 340.12 qps: 6813.82 (r/w/o: 4772.12/1361.06/680.63) lat (ms,95%): 71.83 err/s: 0.40 reconn/s: 0.00 |
| 115 | +[ 30s ] thds: 15 tps: 344.78 qps: 6897.36 (r/w/o: 4828.36/1379.23/689.77) lat (ms,95%): 71.83 err/s: 0.20 reconn/s: 0.00 |
| 116 | +[ 40s ] thds: 15 tps: 343.32 qps: 6876.75 (r/w/o: 4815.15/1374.47/687.14) lat (ms,95%): 71.83 err/s: 0.60 reconn/s: 0.00 |
| 117 | +[ 50s ] thds: 15 tps: 342.80 qps: 6864.76 (r/w/o: 4806.67/1371.89/686.20) lat (ms,95%): 73.13 err/s: 0.50 reconn/s: 0.00 |
| 118 | +[ 60s ] thds: 15 tps: 347.90 qps: 6960.74 (r/w/o: 4873.93/1390.71/696.10) lat (ms,95%): 70.55 err/s: 0.30 reconn/s: 0.00 |
| 119 | +[ 70s ] thds: 15 tps: 346.70 qps: 6942.39 (r/w/o: 4859.29/1389.30/693.80) lat (ms,95%): 70.55 err/s: 0.40 reconn/s: 0.00 |
| 120 | +[ 80s ] thds: 15 tps: 345.60 qps: 6914.88 (r/w/o: 4841.48/1382.00/691.40) lat (ms,95%): 70.55 err/s: 0.20 reconn/s: 0.00 |
| 121 | +[ 90s ] thds: 15 tps: 341.10 qps: 6830.31 (r/w/o: 4782.20/1365.40/682.70) lat (ms,95%): 74.46 err/s: 0.50 reconn/s: 0.00 |
| 122 | +[ 100s ] thds: 15 tps: 341.20 qps: 6829.33 (r/w/o: 4782.12/1364.41/682.80) lat (ms,95%): 74.46 err/s: 0.40 reconn/s: 0.00 |
| 123 | +[ 110s ] thds: 15 tps: 343.40 qps: 6875.79 (r/w/o: 4812.29/1376.50/687.00) lat (ms,95%): 71.83 err/s: 0.20 reconn/s: 0.00 |
| 124 | +[ 120s ] thds: 15 tps: 347.00 qps: 6943.51 (r/w/o: 4862.40/1386.70/694.40) lat (ms,95%): 71.83 err/s: 0.40 reconn/s: 0.00 |
| 125 | +SQL statistics: |
| 126 | + queries performed: |
| 127 | + read: 577836 --读总数 |
| 128 | + write: 164978 --写总数 |
| 129 | + other: 82503 --其他操作(CURD之外的操作,例如COMMIT) |
| 130 | + total: 825317 --全部总数 |
| 131 | + transactions: 41229 (343.51 per sec.) --总事务数(每秒事务数,这个每秒事务数也就是:TPS 吞吐量) |
| 132 | + queries: 825317 (6876.33 per sec.) --查询总数(查询次数) |
| 133 | + ignored errors: 45 (0.37 per sec.) --总忽略错误总数(每秒忽略错误次数) |
| 134 | + reconnects: 0 (0.00 per sec.) --重连总数(每秒重连次数) |
| 135 | + |
| 136 | +General statistics: |
| 137 | + total time: 120.0214s --总耗时 |
| 138 | + total number of events: 41229 --共发生多少事务数 |
| 139 | + |
| 140 | +Latency (ms): |
| 141 | + min: 7.19 --最小耗时 |
| 142 | + avg: 43.66 --平均耗时 |
| 143 | + max: 162.82 --最长耗时 |
| 144 | + 95th percentile: 71.83 --超过95%平均耗时 |
| 145 | + sum: 1799860.45 |
| 146 | + |
| 147 | +Threads fairness: |
| 148 | + events (avg/stddev): 2748.6000/132.71 --总处理事件数/标准偏差 |
| 149 | + execution time (avg/stddev): 119.9907/0.00 --总执行时间/标准偏差 |
| 150 | + |
| 151 | +## QPS 和 TPS 和说明 |
| 152 | + |
| 153 | +### 基本概念 |
| 154 | + |
| 155 | +- QPS:Queries Per Second意思是“每秒查询率”,是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准。 |
| 156 | +- TPS是TransactionsPerSecond的缩写,也就是事务数/秒。它是软件测试结果的测量单位。一个事务是指一个客户机向服务器发送请求然后服务器做出反应的过程。客户机在发送请求时开始计时,收到服务器响应后结束计时,以此来计算使用的时间和完成的事务个数,最终利用这些信息来估计得分。客户机使用加权协函数平均方法来计算客户机的得分,测试软件就是利用客户机的这些信息使用加权协函数平均方法来计算服务器端的整体TPS得分。 |
| 157 | +- QPS(TPS)= 并发数/平均响应时间 或者 并发数 = QPS平均响应时间 **这里响应时间的单位是秒* |
| 158 | +- 举例,我们一个HTTP请求的响应时间是20ms,在10个并发的情况下,QPS就是 QPS=10*1000/20=500。 |
| 159 | +- 这里有个关键的点就是QPS一定是跟并发数联系在一起的,离开并发数谈QPS是没意义的。 |
| 160 | + |
| 161 | + |
| 162 | +### QPS、TPS和性能的关系 |
| 163 | + |
| 164 | +- 一个系统吞吐量通常由QPS(TPS)、并发数两个因素决定,每套系统这两个值都有一个相对极限值,在应用场景访问压力下,只要某一项达到系统最高值,系统的吞吐量就上不去了,如果压力继续增大,系统的吞吐量反而会下降,原因是系统超负荷工作,上下文切换、内存等等其它消耗导致系统性能下降。 |
| 165 | +- 开始,系统只有一个用户,CPU工作肯定是不饱合的。一方面该服务器可能有多个cpu,但是只处理单个进程,另一方面,在处理一个进程中,有些阶段可能是IO阶段,这个时候会造成CPU等待,但是有没有其他请 求进程可以被处理)。随着并发用户数的增加,CPU利用率上升,QPS相应也增加(公式为QPS=并发用户数/平均响应时间。)随着并发用户数的增加,平均响应时间也在增加,而且平均响应时间的增加是一个指数增加曲线。而当并发数增加到很大时,每秒钟都会有很多请求需要处理,会造成进程(线程)频繁切换,反正真正用于处理请求的时间变少,每秒能够处 理的请求数反而变少,同时用户的请求等待时间也会变大,甚至超过用户的心理底线。 ) |
| 166 | + |
| 167 | + |
| 168 | +### 结论 |
| 169 | + |
| 170 | +- 我们对单台服务器进行压测有了性能测试数据以后,我们可以根据业务上能接受最大客户响应时间对应到相应的QPS数,从而计算出需要的服务器的数量。举例来说,响应时间10ms和1000ms对通过浏览器的客户是没有明显体验差别的,基于1000ms估算服务器的数量我们的成本会降低很多。 |
| 171 | +- 每天300wPV的在单台机器上,这台机器需要多少QPS?对于这样的问题,假设每天80%的访问集中在20%的时间里,这20%时间叫做峰值时间。( 3000000 * 0.8 ) / (3600 * 24 * 0.2 ) = 139 (QPS). |
| 172 | +- 如果一台机器的QPS是58,需要几台机器来支持?答:139 / 58 = 3 |
| 173 | + |
| 174 | + |
51 | 175 |
|
52 | 176 | ## 资料
|
53 | 177 |
|
54 | 178 | - <https://my.oschina.net/moooofly/blog/152547>
|
55 | 179 | - <http://www.techug.com/post/mysql-mysqlslap.html>
|
56 | 180 | - <http://jixiuf.github.io/blog/mysql%E5%8E%8B%E5%8A%9B%E6%B5%8B%E8%AF%95/>
|
57 | 181 | - <http://blog.chinaunix.net/uid-25723371-id-3498970.html>
|
| 182 | +- <http://nsimple.top/archives/mysql-sysbench-tool.html> |
| 183 | +- <https://dearhwj.gitbooks.io/itbook/content/test/performance_test_qps_tps.html> |
0 commit comments