zoukankan      html  css  js  c++  java
  • MySQL性能测试初试(1)--sysbench

    1.简介和安装

    SysBench是一个模块化的、跨平台、多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况。它主要包括以下几种方式的测试:

    1. cpu性能
    2. 磁盘io性能
    3. 调度程序性能
    4. 内存分配及传输速度
    5. POSIX线程性能
    6. 数据库性能(OLTP基准测试)

    源码下载:https://github.com/akopytov/sysbench/releases/tag/1.0.20
    rpm包下载:https://packagecloud.io/akopytov/sysbench/packages/el/7/sysbench-1.0.20-1.el7.x86_64.rpm:

    1.1 编译安装

    1.1.1 安装依赖:
    yum -y install make automake libtool pkgconfig libaio-devel mariadb-devel openssl-devel
    

    解压源码:

    tar -xf sysbench-1.0.20.tar.gz -C /opt/software/
    

    进入目录:

    cd /opt/software/sysbench-1.0.20/
    

    编译安装:

    ./autogen.sh
    ./configure
    make -j
    make install
    

    检查安装:

    sysbench --version
    
    1.1.2 如果sysbench抛出如下MySQL链接库的错误,字面意思就是打不开共享库文件libmysqlclient.so.20
    sysbench: error while loading shared libraries: libmysqlclient.so.20: cannot open shared object file: No such file or directory
    

    先查找一下本地有没有这个文件:

    find / -name libmysqlclient.so.20
    

    我们发现mysql软件目录下有这个文件,我们先建立软连接到/usr/lib

    ln -s /opt/mysql/lib/libmysqlclient.so.20 /usr/lib
    

    如果没有生效,接着在/etc/ld.so.cnf中加入/usr/lib这一行

    echo '/usr/lib' >> /etc/ld.so.conf
    

    执行ldconfig更新下配置即可生效

    ldconfig 
    

    1.2 脚本快速安装

    curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash
    sudo yum -y install sysbench
    
    [root@mysql8 ~]# sysbench --version
    sysbench 1.0.20
    

    2.sysbench使用方法

    使用help命令打印帮助信息

    [root@mysql8 ~]# sysbench --help
    Usage:
      sysbench [options]... [testname] [command]
    
    Commands implemented by most tests: prepare run cleanup help
    
    General options:
      --threads=N                     指定线程数 [1]
      --events=N                      限制事物最大请求数,0表示不限制 [0]
      --time=N                        限制总执行时间,以秒为单位,0表示不限制 [10]
      --forced-shutdown=STRING        达到总执行时间后还需等待多久关闭sysbench,off表示禁用该功能 [off]
      --thread-stack-size=SIZE        每个线程使用的栈空间大小 [64K]
      --rate=N                        平均事物处理速率,0表示不限制 [0]
      --report-interval=N             每隔几秒报告一次结果,0表示禁用间隔报告 [0]
      --report-checkpoints=[LIST,...] 在指定的时间点转储完整的统计和重置所有计数器。参数是一个用逗号分隔的值列表,表示从测试开始到必须执行报告检查点所经过的秒数。报告检查点在默认情况下是关闭的。 []
      --debug[=on|off]                是否打印更多调试信息 [off]
      --validate[=on|off]             在可能的地方执行验证检查 [off]
      --help[=on|off]                 打印help并退出 [off]
      --version[=on|off]              打印版本并退出 [off]
      --config-file=FILENAME          从文件中读取命令行选项
      --tx-rate=N                     已废弃,是--rate的别名 [0]
      --max-requests=N                已废弃,是--events的别名 [0]
      --max-time=N                    已废弃,是--time的别名 [0]
      --num-threads=N                 已废弃,是--threads的别名 [1]
    
    Pseudo-Random Numbers Generator options:    随机数生成器选项:
      --rand-type=STRING 字符串随机数分布 {uniform,gaussian,special,pareto} [special]
      --rand-spec-iter=N 用数字生成的迭代次数 [12]
      --rand-spec-pct=N  'special' 值将落入特殊分布的整个范围的百分比(用于特殊分配) [1]
      --rand-spec-res=N  要使用的'special' 值的百分比(用于特殊分配) [75]
      --rand-seed=N      用于随机数生成器。当为0时,将使用当前时间作为RNG种子 当为0时,将使用当前时间作为RNG种子[0]
      --rand-pareto-h=N  pareto 分布的形状参数 [0.2]
    
    Log options:    日志选项:
      --verbosity=N 详细级别{5-调试,0-仅关键消息}  [3]
    
      --percentile=N       百分位数,用于计算延迟统计信息(1-100)。使用特殊值0禁用百分位计算 [95]
      --histogram[=on|off] 印等待时间直方图 [off]
    
    General database options:   常规数据库选项:
    
      --db-driver=STRING  指定要使用的数据库驱动程序(“帮助”以获取可用驱动程序列表) [mysql]
      --db-ps-mode=STRING 预备语句用法模式 {auto, disable} [auto]
      --db-debug[=on|off] 打印特定于数据库的调试信息 [off]
    
    
    Compiled-in database drivers:   内置数据库驱动程序:
      mysql - MySQL driver
      pgsql - PostgreSQL driver
    
    mysql options:
      --mysql-host=[LIST,...]          MySQL server host [localhost]
      --mysql-port=[LIST,...]          MySQL server port [3306]
      --mysql-socket=[LIST,...]        MySQL socket
      --mysql-user=STRING              MySQL user [sbtest]
      --mysql-password=STRING          MySQL password []
      --mysql-db=STRING                MySQL database name [sbtest]
      --mysql-ssl[=on|off]             use SSL connections, if available in the client library [off]
      --mysql-ssl-cipher=STRING        use specific cipher for SSL connections []
      --mysql-compression[=on|off]     use compression, if available in the client library [off]
      --mysql-debug[=on|off]           trace all client library calls [off]
      --mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205]
      --mysql-dry-run[=on|off]         Dry run, pretend that all MySQL client API calls are successful without executing them [off]
    
    pgsql options:
      --pgsql-host=STRING     PostgreSQL server host [localhost]
      --pgsql-port=N          PostgreSQL server port [5432]
      --pgsql-user=STRING     PostgreSQL user [sbtest]
      --pgsql-password=STRING PostgreSQL password []
      --pgsql-db=STRING       PostgreSQL database name [sbtest]
    
    Compiled-in tests:
      fileio - File I/O test
      cpu - CPU performance test
      memory - Memory functions speed test
      threads - Threads subsystem performance test
      mutex - Mutex performance test
    
    See 'sysbench <testname> help' for a list of options for each test.
    
    Usage:
      sysbench [options]... [test_lua] [lua_options] [command]
    
    Commands implemented by most tests: prepare run cleanup help
    
    • [options]:为sysbench参数选项
    • [test_lua]:如果是rpm包安装的sysbench,则这些脚本都是/usr/share/sysbench目录下。对于一般的数据库测试,只需使用和oltp有关的lua脚本就足够。
    • [lua_options]:是lua脚本的参数选项

    例如,要查看oltp_common.lua的用法,可以:

    sysbench /usr/share/sysbench/oltp_common.lua help
    如果找不到脚本,可以find查找一下在哪个位置
    
    • [command]:主要有4个选项prepare run cleanuphelp
    1. prepare:准备数据的命令。例如,在sysbench压力测试之前,需要先准备好测试库、测试表以及测试表中的数据。具体用法见后文。
    2. run:表示进行压力测试。
    3. cleanup:清除测试时产生的数据。
    4. help:输出给定lua脚本的帮助信息。

    3.准备测试数据

    首先创建sysbench所需数据库sbtest(这是sysbench默认使用的库名,必须创建测试库)。
    然后,准备测试所用的表,这些测试表放在测试库sbtest中。这里使用的lua脚本为/usr/share/sysbench/oltp_common.lua

    sysbench --mysql-host=192.168.80.54 --mysql-port=21021 --mysql-user=root --mysql-password=123 /usr/share/sysbench/oltp_common.lua --tables=10 --table_size=100000 prepare
    

    其中--tables=10表示创建10个测试表,--table_size=100000表示每个表中插入10W行数据,prepare表示这是准备数的过程。

    mysql> show tables from sbtest;
    +------------------+
    | Tables_in_sbtest |
    +------------------+
    | sbtest1          |
    | sbtest10         |
    | sbtest2          |
    | sbtest3          |
    | sbtest4          |
    | sbtest5          |
    | sbtest6          |
    | sbtest7          |
    | sbtest8          |
    | sbtest9          |
    +------------------+
    
    mysql> select count(*) from sbtest.sbtest1;
    +----------+
    | count(*) |
    +----------+
    |   100000 |
    +----------+
    

    如果想要清除这10个表,可使用cleanup命令。

    sysbench --mysql-host=192.168.80.54 --mysql-port=21021 --mysql-user=root --mysql-password=123 /usr/share/sysbench/oltp_common.lua --tables=10 --table_size=100000 cleanup
    

    4.数据库测试和结果分析

    稍微修改下之前准备数据的语句,就可以拿来测试了。

    需要注意的是,之前使用的lua脚本为oltp_common.lua,它是一个通用脚本,是被其它lua脚本调用的,它不能直接拿来测试。

    所以,我这里用oltp_read_write.lua脚本来做读、写测试。还有很多其它类型的测试,比如只读测试、只写测试、删除测试、大批量插入测试等等。可找到对应的lua脚本进行调用即可。

    sysbench --threads=4 --time=20 --report-interval=5 --mysql-host=192.168.80.54 --mysql-port=6033 --mysql-user=root --mysql-password=123 /usr/share/sysbench/oltp_read_write.lua --tables=10 --table_size=100000 run
    

    以下是测试返回的结果:

    Running the test with following options:
    Number of threads: 4
    Report intermediate results every 5 second(s)
    Initializing random number generator from current time
    
    
    Initializing worker threads...
    
    Threads started!
    
    
    ####以下是每5秒返回一次的结果,统计的指标包括:
    #### 线程数、tps(每秒事务数)、qps(每秒查询数)、
    #### 每秒的读/写/其它次数、延迟、每秒错误数、每秒重连次数
    [ 5s ] thds: 4 tps: 13.79 qps: 285.29 (r/w/o: 201.32/55.58/28.39) lat (ms,95%): 802.05 err/s: 0.00 reconn/s: 0.00
    [ 10s ] thds: 4 tps: 11.60 qps: 237.01 (r/w/o: 165.41/48.40/23.20) lat (ms,95%): 682.06 err/s: 0.00 reconn/s: 0.00
    [ 15s ] thds: 4 tps: 10.40 qps: 203.20 (r/w/o: 143.20/39.20/20.80) lat (ms,95%): 1069.86 err/s: 0.00 reconn/s: 0.00
    [ 20s ] thds: 4 tps: 9.73 qps: 196.48 (r/w/o: 136.48/40.53/19.47) lat (ms,95%): 977.74 err/s: 0.00 reconn/s: 0.00
    SQL statistics:
        queries performed:
            read:                            3248   # 执行的读操作数量
            write:                           928   # 执行的写操作数量
            other:                           464    # 执行的其它操作数量
            total:                           4640
        transactions:                        232    (11.47 per sec.)    # 执行事务的平均速率
        queries:                             4640   (229.41 per sec.)   # 平均每秒能执行多少次查询
        ignored errors:                      0      (0.00 per sec.)
        reconnects:                          0      (0.00 per sec.)
    
    General statistics:
        total time:                          20.2251s     # 总消耗时间
        total number of events:              232         # 总请求数量(读、写、其它)
    
    Latency (ms):
             min:                                   10.72
             avg:                                  345.92
             max:                                 1392.39
             95th percentile:                      977.74    # 采样计算的平均延迟
             sum:                                80252.98
    
    Threads fairness:
        events (avg/stddev):           58.0000/2.74
        execution time (avg/stddev):   20.0632/0.03
    

    5.io测试

    sysbench内置了几个测试指标。

    Compiled-in tests:
      fileio - File I/O test
      cpu - CPU performance test
      memory - Memory functions speed test
      threads - Threads subsystem performance test
      mutex - Mutex performance test
    

    可直接help输出测试方法。例如,fileio测试。

    [root@mysql8 tests]# sysbench fileio help
    sysbench 1.0.20 (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-async-backlog=N        number of asynchronous operatons to queue per thread [128]
      --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]
    

    例如,创建5个文件,总共1G,每个文件大概200M。

    sysbench fileio --file-num=5 --file-total-size=1G prepare
    
    [root@mysql8 ~]# ll -h test*
    -rw------- 1 root root 205M 10月 17 18:41 test_file.0
    -rw------- 1 root root 205M 10月 17 18:41 test_file.1
    -rw------- 1 root root 205M 10月 17 18:41 test_file.2
    -rw------- 1 root root 205M 10月 17 18:41 test_file.3
    -rw------- 1 root root 205M 10月 17 18:41 test_file.4
    

    然后,运行测试。

    sysbench --events=5000 --threads=16 fileio --file-num=5 --file-total-size=1G --file-test-mode=rndrw --file-fsync-freq=0 --file-block-size=16384 run
    

    结果:

    File operations:
        reads/s:                      93.08
        writes/s:                     63.09
        fsyncs/s:                     7.14
    
    Throughput:     # 吞吐量
        read, MiB/s:                  1.45  # 表示读的带宽
        written, MiB/s:               0.99  # 表示写的带宽
    
    General statistics:
        total time:                          11.2048s
        total number of events:              1750
    
    Latency (ms):
             min:                                    0.00
             avg:                                   96.56
             max:                                 1306.96
             95th percentile:                      390.30
             sum:                               168979.76
    
    Threads fairness:
        events (avg/stddev):           109.3750/13.19
        execution time (avg/stddev):   10.5612/0.45
    
    

    6.cpu性能测试:

    [root@mysql8 ~]# sysbench cpu help
    sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)
    
    cpu options:
      --cpu-max-prime=N upper limit for primes generator [10000]
      可以看到重要的关键字`prime`,即质数,比如查找小于50000的最大的质数
    
    sysbench cpu --threads=40 --events=10000 --cpu-max-prime=50000 run
    开启40个线程,最大请求10000
    
    
    CPU speed:
        events per second:   289.61
    
    General statistics:
        total time:                          10.0643s
        total number of events:              2915
    
    Latency (ms):
             min:                                    6.72
             avg:                                  135.21
             max:                                 1070.41
             95th percentile:                      427.07
             sum:                               394142.29
    
    Threads fairness:
        events (avg/stddev):           72.8750/3.87
        execution time (avg/stddev):   9.8536/0.12
    

    7.内存测试

    [root@mysql8 ~]# sysbench memory help
    sysbench 1.0.20 (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]
    

    下面我们测试32G内存,并发线程数是10个,最大请求数是100。分别从读和写两种测试来做。

    • 内存读测试
    sysbench memory --threads=10 --events=100 --memory-block-size=16K --memory-total-size=32G --memory-oper=read run
    
    Total operations: 2097150 (5218958.16 per second)
    
    32767.97 MiB transferred (81546.22 MiB/sec)
    
    
    General statistics:
        total time:                          0.4006s
        total number of events:              2097150
    
    Latency (ms):
             min:                                    0.00
             avg:                                    0.00
             max:                                   97.83
             95th percentile:                        0.00
             sum:                                 2345.57
    
    Threads fairness:
        events (avg/stddev):           209715.0000/0.00
        execution time (avg/stddev):   0.2346/0.07
    
    • 内存写测试
    sysbench memory --threads=10 --events=100 --memory-block-size=16K --memory-total-size=32G --memory-oper=write run
    

    因为有悔,所以披星戴月;因为有梦,所以奋不顾身! 个人博客首发:easydb.net 微信公众号:easydb 关注我,不走丢!

  • 相关阅读:
    react深入学习(资料,案例)
    match.exec深入学习
    实用插件表格行列隐藏显示
    下拉选项插件的实现
    表格操作eventTable
    [CentOS7] 挂载iso镜像文件到/media目录下
    [CentOS7] 设置开机启动方式(图形界面或命令行)
    [CentOS7] 磁盘分区(gdisk, fdisk)
    [CentOS7] minimal安装后 出现 没有ifconfig 无法ping 无法yum could not retrieve mirrorlist http://mirrorlist.centos.org/
    [C++]C,C++中使用可变参数
  • 原文地址:https://www.cnblogs.com/easydb/p/13832289.html
Copyright © 2011-2022 走看看