欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

mysql 基准测试

最编程 2024-06-11 13:56:32
...
QPS

query per second

mysql> show global status like '%question%';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| Questions     | 165041361 |
+---------------+-----------+
1 row in set (0.00 sec)
TPS

transaction per second

mysql> show global status like '%uptime%';
+---------------------------+---------+
| Variable_name             | Value   |
+---------------------------+---------+
| Uptime                    | 1749762 |
| Uptime_since_flush_status | 1749762 |
+---------------------------+---------+
2 rows in set (0.00 sec)

==MySQL一般只记录QPS==

基准测试

基准测试主要用来对功能和对参数调整测试

sysbench

  • 简单高效的基准测试工具
  • Oracle官方也使用该工具对MySQL进行测试
  • 根据互联网应用特点进行测试
  • 可以根据某个具体操作进行测试
  • 还支持Oracle,PostgreSQL的测试
  • https://github.com/akopytov/sysbench
sysbench测试架构
db driver : MySQL PostgreSQL Oracle
test script : oltp.lua select.lua update_index.lua update_non_index.lua ....
test tables : sbtest1 sbtest2 sbtest3 sbtest4 ...
sysbench 参数
参数 说明 默认值
--oltp-table-name 测试的表名 sbtest
--oltp-tables-count 测试的表的数量 1
--oltp-tables-size 每张测试表的记录数量 10000
--oltp-dist-type 热点数据的分布,可选值有:uniform gaussian special special
--oltp-dist-pct 百分之多少的数据视为热点数据 1
--oltp-dist-res 热点数据库的访问频率 75
--mysql-host/password/port/user 连接MySQL的各项参数
--mysql-db 默认的测试数据库名 sbtest
安装

建议安装sysbenh-0.5的版本

1.shell> https://github.com/akopytov/sysbench.git # 通过git clone得到源码
2.shell> cd sysbench
3.shell> ./autogen.sh
4.shell> ./configure  --with-mysql-includes=/usr/local/mysql56/include/  --with-mysql-libs=/usr/local/mysql56/lib/  # 关联mysql的头文件和库
5.##
6.## 注意,如果我这里使用mysql5.7.9 的include和lib ,提示我 /usr/bin/ld: cannot find -lmysqlclient_r
7.##
8.
9.shell> make -j 2 # -j 2 表示用几个cpu核心进行编译
10.shell> make install # 默认安装到 /usr/local/bin , 如果有自定义目录,configure增加参数 --prefix=自定义目录
11.shell> echo "export LD_LIBRARY_PATH=/usr/local/mysql56/lib/:$LD_LIBRARY_PATH" >> ~/.bashrc # 添加LD_LIBRARY_PATH
12.shell> source ~/.bashrc
13.shell> sysbench --version
14.sysbench 0.5
测试
MySQL测试
sysbench    --test=./sysbench/tests/db/select.lua \         #指定使用测试脚本是哪个
            --oltp-table-size=100000 \                      #测试表的数据多少
            --oltp-tables-count=1 \                         #测试有几张表
            --mysql-user=root \                             #MySQL用户名
            --mysql-password=123 \                          #MySQL密码
            --mysql-host=127.0.0.1 \                        #MySQLIP路径
            --mysql-port=3306 \                             #MySQL端口号
            --num-threads=128 \                             #测试线程数
            --max-requests=0 \                              #一共发起多少请求,0表示任意
            --max-time=120 \                                #测试120秒
            --report-interval=3                             #每3秒钟打印出结果
            prepare #  run # cleanup                        # prepare:生成文件
                                                            # run:开始测试
                                                            # cleanup:删除测试文件
磁盘io测试
1.#
2.# 生成测试文件
3.#
4.shell> sysbench --test=fileio \                # File IO测试
5.                --file-num=4 \                 # 测试文件数是4个
6.                --file-block-size=8K \         # block size是8K
7.                --file-total-size=1G \         # 4个文件的总大小是1G
8.                --file-test-mode=rndrd \       # 测试方法是随机读
9.                --file-extra-flags=direct \    # direct io,跳过缓存
10.                --max-requests=0 \            # 一共发起多少请求,0表示任意
11.                --max-time=3600 \             # 测试3600s 
12.                --num-threads=4 \             # 使用4个线程
13.                prepare #  run # cleanup      # prepare:生成文件
14.                                              # run:开始测试
15.                                              # cleanup:删除测试文件
16.
17.## 其他说明  sysbench --test=fileio help
18.  # --file-num=N                  创建文件数
19.  # --file-block-size=N           block size大小
20.  # --file-total-size=SIZE        文件数的大小总和
21.  # --file-test-mode=STRING       测试模式 {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw} (顺序写,顺序读写,顺序读,随机读,随机写,随机读写)
22.  # --file-io-mode=STRING         文件操作方式 {sync,async,mmap} 
23.  # --file-extra-flags=STRING     打开文件的额外标志 {sync,dsync,direct} []
24.  # --file-fsync-freq=N           多少请求后执行fsync。默认是0,不执行
25.  # --file-fsync-all=[on|off]     是否每次操作后都执行fsync
26.  # --file-fsync-end=[on|off]     测完成后执行fsync,默认是on
27.  # --file-fsync-mode=STRING      同步的方法 {fsync, fdatasync}默认是 [fsync]
28.  # --file-merged-requests=N      最多多少IO请求被合并,默认为0,不合并
29.  # --file-rw-ratio=N             读写比例默认是 [1.5],即 3:2
30.
1.#
2.# 开始测试
3.#
4.shell> sysbench --test=fileio \               
5.                --file-num=4 \               
6.                --file-block-size=8K \       
7.                --file-total-size=1G \        
8.                --file-test-mode=rndrd \    
9.                --file-extra-flags=direct \   
10.                --max-requests=0 \            
11.                --max-time=30 \             # 简单测试,测试30秒
12.                --num-threads=4 \             
13.                --report-interval=3 \       # 每3秒产生报告
14.                run
15.sysbench 0.5:  multi-threaded system evaluation benchmark
16.
17.Running the test with following options:
18.Number of threads: 4
19.Report intermediate results every 3 second(s)
20.Random number generator seed is 0 and will be ignored
21.
22.
23.Extra file open flags: 3
24.4 files, 256Mb each
25.1Gb total file size
26.Block size 8Kb
27.Number of IO requests: 0
28.Read/Write ratio for combined random IO test: 1.50
29.Periodic FSYNC enabled, calling fsync() each 100 requests.
30.Calling fsync() at the end of test, Enabled.
31.Using synchronous I/O mode
32.Doing random read test
33.Threads started!
34.
35.[   3s] reads: 1.70 MB/s writes: 0.00 MB/s fsyncs: 0.00/s response time: 54.416ms (95%)
36.[   6s] reads: 1.78 MB/s writes: 0.00 MB/s fsyncs: 0.00/s response time: 55.469ms (95%)
37.[   9s] reads: 1.75 MB/s writes: 0.00 MB/s fsyncs: 0.00/s response time: 55.253ms (95%)
38.[  12s] reads: 1.66 MB/s writes: 0.00 MB/s fsyncs: 0.00/s response time: 52.120ms (95%)
39.[  15s] reads: 1.76 MB/s writes: 0.00 MB/s fsyncs: 0.00/s response time: 51.840ms (95%)
40.[  18s] reads: 1.79 MB/s writes: 0.00 MB/s fsyncs: 0.00/s response time: 50.933ms (95%)
41.[  21s] reads: 1.78 MB/s writes: 0.00 MB/s fsyncs: 0.00/s response time: 54.858ms (95%)
42.[  24s] reads: 1.88 MB/s writes: 0.00 MB/s fsyncs: 0.00/s response time: 50.857ms (95%)
43.[  27s] reads: 1.75 MB/s writes: 0.00 MB/s fsyncs: 0.00/s response time: 56.238ms (95%)
44.[  30s] reads: 1.61 MB/s writes: 0.00 MB/s fsyncs: 0.00/s response time: 64.097ms (95%)
45.Operations performed:  6709 reads, 0 writes, 0 Other = 6709 Total
46.Read 52.414Mb  Written 0b  Total transferred 52.414Mb  (1.7462Mb/sec)
47.  223.51 Requests/sec executed  # 这个就是IOPS
48.
49.General statistics:
50.    total time:                          30.0160s
51.    total number of events:              6709
52.    total time taken by event execution: 120.0223s
53.    response time:
54.         min:                                  0.13ms
55.         avg:                                 17.89ms
56.         max:                                254.62ms
57.         approx.  95 percentile:              54.97ms
58.
59.Threads fairness:
60.    events (avg/stddev):           1677.2500/28.16
61.    execution time (avg/stddev):   30.0056/0.01
62.
63.##
64.## 上述测试随机读的速度在1.7MB/s左右, 
65.## (1.7MB/s * 1024 / 8KB =217)换算后得到的值就是IOPS,约等于上面的223。
66.##
67.

测试完成后执行cleanup
如果是真实的测试 max-time设置成一周的时间
run期间可以使用iotop或者iostat进行观察

TPCC

专门针对联机交易系统(OLTP系统)的规范
该系统需要要处理的交易事务主要为以下几种:
  • 新订单(New-Order):客户输入一笔新的订货交易;
  • 支付操作(Payment):更新客户账户余额以反应其支付状况;
  • 发货(Delivery):发货(模拟批处理交易);
  • 订单状态查询(Order-Status):查询客户最近交易的状态;
  • 库存状态查询(Stock-Level):查询仓库库存状态,以便能够及时补货;
衡量单位:tpmC
  • transaction per minute 每分钟的事务

tpcc-mysql

  • 由Percona公司开发的tpcc测试工具
  • 仅针对与MySQL数据库进行测试
  • 官方地址:
    • https://github.com/Percona-Lab/tpcc-mysql
安装tpcc-mysql
git clone https://github.com/Percona-Lab/tpcc-mysql.git
cd tpcc-mysql/src/
make
cd ..
mysql -e"create database tpcc"
mysql tpcc < create_table.sql
mysql tpcc -e"show tables"
+----------------+
| Tables_in_tpcc |
+----------------+
| customer       |
| district       |
| history        |
| item           |
| new_orders     |
| order_line     |
| orders         |
| stock          |
| warehouse      |
+----------------+
mysql tpcc < add_fkey_idx.sql
填充数据
./tpcc_load --help for all options
*************************************
*** TPCC-mysql Data Loader        ***
*************************************
./tpcc_load: invalid option -- '-'
Usage: tpcc_load -h server_host -P port -d database_name -u mysql_user -p mysql_password -w warehouses -l part -m min_wh -n max_wh
* [part]: 1=ITEMS 2=WAREHOUSE 3=CUSTOMER 4=ORDERS
./tpcc_load -h127.0.0.1  -d tpcc -u root -p "123" -w 1000
填充完成
开始测试
./tpcc_start --help for all options
***************************************
*** ###easy### TPC-C Load Generator ***
***************************************
./tpcc_start: invalid option -- '-'
Usage: tpcc_start -h server_host -P port -d database_name -u mysql_user -p mysql_password -w warehouses -c connections -r warmup_time -l running_time -i report_interval -f report_file -t trx_file

mysql tpcc < count.sql
./tpcc_start -h 127.0.0.1 -P 3306 -d tpcc -u root -p 123 -w 10 -c 16 -r 3 -l 20
-w 仓库数 -c 测试线程数 -r 预热时间 -l 测试时间

==报错解决==

./tpcc_load: error while loading shared libraries: libmysqlclient.so.20: cannot open shared object file: No such file or directory
find / -name libmysqlclient.so.20
ln -s /usr/local/mysql-5.7.25-linux-glibc2.12-x86_64/lib/libmysqlclient.so.20 /usr/lib/libmysqlclient.so.20

这时再次测试看看是否报错,如果还报错做如下操作

echo '/usr/local/lib' >> /etc/ld.so.conf
ldconfig -v

mysqlslap

/bin/bash
#自动记录使用mysqlslap测试时系统情况

time="$1"
thd="$2"
f=`date +%Y_%m_%d_%H_%M_%S`

echo "test total requests: " ${time}
echo "output filename: " ${f}

mkdir "${f}"

mysql -e "show variables" > my_cnf
iostat -xm 3 > "${f}"/io_"${thd}".txt &
mysqladmin extended-status -r -i 1 > "${f}"/mysql_"${thd}".txt &
cd ${f}
nmon -f -s 5 -e 999999999999999999999999
cd ..
echo "start testing....."
mysqlslap --query=test.sql --delimiter="//" –concurrency="${thd}" –number-of-queries=9999999999999 &
sleep ${time}
killall iostatus mysqladmin nmon mysqlslap
echo "test finish."