zoukankan      html  css  js  c++  java
  • sysbench安装、对Mysql压力测试、结果解读及mysql数据库跟踪优化

    sysbench是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试。


    sysbench支持以下几种测试模式:
    
    1、CPU运算性能
    2、磁盘IO性能
    3、调度程序性能
    4、内存分配及传输速度
    5、POSIX线程性能
    6、数据库性能(OLTP基准测试)
    目前sysbench主要支持 mysql,drizzle,pgsql,oracle 等几种数据库。

    本次测试服务器配置如下:


    一、编译安装

    编译非常简单,可参考 README 文档,简单步骤如下:

    项目地址https://launchpad.net/sysbench


    # tar xf sysbench-0.4.12-1.1.tgz 
    cd sysbench-0.4.12-1.1
    # chmod +x autogen.sh 
    [root@centossz008 sysbench-0.4.12-1.1]# ./autogen.sh 
    automake 1.10.x (aclocal) wasn't found, exiting
    # yum install -y automake

    # ./autogen.sh 
    libtoolize 1.4+ wasn't found, exiting

    # yum install -y libtool

    安装
    # ./autogen.sh 
    # ./configure --with-mysql-includes=/usr/local/mysql/include --with-mysql-libs=/usr/local/mysql/lib
    # make && make install


    如果 make 没有报错,就会在 /root/sysbench-0.4.12-1.1/sysbench 目录下生成二进制命令行工具 sysbench
    # ls -l sysbench
    -rwxr-xr-x. 1 root root 3281264 Apr  3 13:18 sysbench


    二、OLTP测试前准备

    初始化测试库环境(总共10个测试表,每个表 100000 条记录,填充随机生成的数据):


    # cd /root/sysbench-0.4.12-1.1/sysbench
    # mysqladmin -uroot -pyourpassword create sbtest


    # ./sysbench --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password=yourpassword

     --test=tests/db/oltp.lua --oltp_tables_count=10 --oltp-table-size=5000000 --rand-init=on prepare


    关于这几个参数的解释:


    --test=tests/db/oltp.lua 表示调用 tests/db/oltp.lua 脚本进行 oltp 模式测试
    --oltp_tables_count=10 表示会生成 10 个测试表
    --oltp-table-size=5000000 表示每个测试表填充数据量为 5000000 
    --rand-init=on 表示每个测试表都是用随机数据来填充的
    如果在本机,也可以使用 –mysql-socket 指定 socket 文件来连接。加载测试数据时长视数据量而定,若过程比较久需要稍加耐心等待。


    真实测试场景中,数据表建议不低于10个,单表数据量不低于500万行,当然了,要视服务器硬件配置而定。如果是配备了SSD或者PCIE SSD这种高IOPS设备的话,则建议单表数据量最少不低于1亿行。

    三、进行OLTP测试


    在上面初始化数据参数的基础上,再增加一些参数,即可开始进行测试了:

    此次测试启动了256个线程,10个测试表,每个表中灌入500W条数据,测试时间1个小时

    ./sysbench --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password=yourpassword --test=tests/db/oltp.lua --oltp_tables_count=10 --oltp-table-size=5000000 --num-threads=256 --oltp-read-only=off --report-interval=10 --rand-type=uniform --max-time=3600  --max-requests=1000000 --percentile=99 run >> ./log/sysbench_oltpX_256_20160403.log


    在当前目录创建存放日志的目录
    # mkdir log
    # ./sysbench --mysql-host=localhost --mysql-port=3306 --mysql-user=root
    --mysql-password=myoa888.com --test=tests/db/oltp.lua --oltp_tables_count=10
    --oltp-table-size=10000000 --num-threads=8 --oltp-read-only=off
    --report-interval=10 --rand-type=uniform --max-time=3600
     --max-requests=0 --percentile=99 run >> ./log/sysbench_oltpX_8_20160403.log


    几个选项稍微解释下


    --num-threads=8 表示发起 8个并发连接
    --oltp-read-only=off 表示不要进行只读测试,也就是会采用读写混合模式测试
    --report-interval=10 表示每10秒输出一次测试进度报告
    --rand-type=uniform 表示随机类型为固定模式,其他几个可选随机模式:uniform(固定),gaussian(高斯),special(特定的),pareto(帕累托)
    --max-time=3600 表示最大执行时长为 3600秒
    --max-requests=0 表示总请求数为 0,因为上面已经定义了总执行时长,所以总请求数可以设定为 0;也可以只设定总请求数,不设定最大执行时长
    --percentile=99 表示设定采样比例,默认是 95%,即丢弃1%的长请求,在剩余的99%里取最大值
    即:模拟 对10个表并发OLTP测试,每个表1000万行记录,持续压测时间为 1小时。


    真实测试场景中,建议持续压测时长不小于30分钟,否则测试数据可能不具参考意义。

    四、测试结果解读:


    测试结果解读如下:

    sysbench 0.5:  multi-threaded system evaluation benchmark
    
    Running the test with following options:
    Number of threads: 256
    Report intermediate results every 10 second(s)
    Random number generator seed is 0 and will be ignored
    
    -- 线程启动
    Threads started!
    
    -- 每10秒钟报告一次测试结果,tps、每秒读、每秒写、99%以上的响应时长统计
    [  10s] threads: 256, tps: 524.19, reads/s: 7697.05, writes/s: 2143.56, response time: 1879.46ms (99%)
    [  20s] threads: 256, tps: 96.50, reads/s: 1351.01, writes/s: 373.30, response time: 9853.49ms (99%)
    [  30s] threads: 256, tps: 235.50, reads/s: 3297.01, writes/s: 946.90, response time: 2150.47ms (99%)
    [  40s] threads: 256, tps: 115.50, reads/s: 1617.00, writes/s: 491.40, response time: 4562.75ms (99%)
    [  50s] threads: 256, tps: 262.10, reads/s: 3669.41, writes/s: 1016.10, response time: 2049.90ms (99%)
    [  60s] threads: 256, tps: 121.50, reads/s: 1701.00, writes/s: 499.10, response time: 3666.03ms (99%)
    [  70s] threads: 256, tps: 201.40, reads/s: 2735.10, writes/s: 769.50, response time: 3867.82ms (99%)
    [  80s] threads: 256, tps: 204.70, reads/s: 2950.29, writes/s: 838.10, response time: 2724.99ms (99%)
    [  90s] threads: 256, tps: 118.40, reads/s: 1657.61, writes/s: 490.00, response time: 3835.53ms (99%)
    
    
    OLTP test statistics:
        queries performed:
            read:                            8823206	-- 读总数
            write:                           2520916	-- 写总数
            other:                           1260458	-- 其他操作总数(SELECT、INSERT、UPDATE、DELETE之外的操作,例如COMMIT等)
            total:                           12604580	-- 全部总数
        transactions:                        630229 (174.94 per sec.)	-- 总事务数(每秒事务数)
        deadlocks:                           0      (0.00 per sec.)		-- 发生死锁总数
        read/write requests:                 11344122 (3148.86 per sec.)	-- 读写总数(每秒读写次数)
        other operations:                    1260458 (349.87 per sec.)	-- 其他操作总数(每秒其他操作次数)
    
    
    General statistics:		-- 一些统计结果		
        total time:                          3602.6152s	-- 总耗时
        total number of events:              630229		-- 共发生多少事务数
        total time taken by event execution: 921887.7227s	-- 所有事务耗时相加(不考虑并行因素)
        response time:					-- 响应时间
             min:                                  6.52ms	-- 最小耗时
             avg:                               1462.78ms	-- 平均耗时
             max:                               9918.51ms	-- 最长耗时
             approx.  99 percentile:            3265.01ms	-- 超过99%平均耗时
    
    
    Threads fairness:				-- 线程的稳定性
        events (avg/stddev):           2461.8320/34.60  	-- 事件(平均值/偏差)
        execution time (avg/stddev):   3601.1239/0.63	-- 执行时间(平均值/偏差)
    

    关于测试后的性能优化:

    1、善后清理工作:

    进mysql删除sbtest库

    mysql> drop database sbtest;

    可以运行./mysqltuner.pl程序,根据建议对数据库进行调优后,再使用sysbench对OLTP进行测试,看看TPS是不是会有所提高。 
    注意:sysbench的测试只是基准测试,并不能代表实际企业环境下的性能指标。


    2、运行优化建议工具
    # ./mysqltuner.pl
    建议:
    General recommendations:
        Run OPTIMIZE TABLE to defragment tables for better performance
        MySQL started within last 24 hours - recommendations may be inaccurate
        Reduce your overall MySQL memory footprint for system stability
        Enable the slow query log to troubleshoot bad queries
        Reduce or eliminate unclosed connections and network issues
    Variables to adjust:
      *** MySQL's maximum memory usage is dangerously high ***
      *** Add RAM before increasing MySQL buffer variables ***
        query_cache_type (=0)
        innodb_buffer_pool_size (>= 33G) if possible.
        innodb_buffer_pool_instances(=27)


    3、修改mysql参数
    # vim /etc/my.cnf

    innodb_buffer_pool_instances = 27
    innodb_buffer_pool_size = 3600M


    每轮测试完成后,都重启mysqld实例,并且用下面的方法删除系统cache,释放swap(如果用到了swap的话),甚至可以重启整个OS。

    # sync  -- 将脏数据刷新到磁盘
    # sync  -- 将脏数据刷新到磁盘
    # echo 3 > /proc/sys/vm/drop_caches  -- 清除OS Cache
    # swapoff -a && swapon -a     -- 清除OS swap缓存

    4、再次启动测试流程

  • 相关阅读:
    Java: Regular Expressions
    Java: Checked & Unchecked Exceptions
    二叉树的构建和层级打印
    [leetcode] 1032: Stream of Characters: Tries&AC自动机
    [leetcode] 1503: Previous Permutation With One Swap
    robot moving on the surface of a square
    killing rabbits
    Find the longest route with the smallest starting point
    [leetcode] Minimum Number of K Consecutive Bit Flips
    检测设备横屏 || 竖屏的状态
  • 原文地址:https://www.cnblogs.com/reblue520/p/6239827.html
Copyright © 2011-2022 走看看