Aldebaran

人生最棒的感觉,就是你做到别人说你做不到的事。

0%

关于sysbench

指原莉乃

介绍

sysbench是基于LuaJIT的可编写脚本的多线程基准测试工具。它最常用于数据库基准测试,但也可以用于创建不涉及数据库服务器的任意复杂的工作负载。

sysbench附带以下基准测试:

  • oltp_*.lua:类似OLTP的数据库基准测试的集合
  • fileio:文件系统级基准
  • cpu:简单的CPU基准测试
  • memory:内存访问基准
  • threads:基于线程的调度程序基准
  • mutex:POSIX互斥基准测试

其中数据库测试默认提供了如下的lua脚本,可以更具自己的业务场景进行修改。

$ find / -name oltp_*.lua
/usr/share/sysbench/oltp_read_only.lua
/usr/share/sysbench/oltp_update_non_index.lua
/usr/share/sysbench/oltp_update_index.lua
/usr/share/sysbench/oltp_write_only.lua
/usr/share/sysbench/oltp_read_write.lua
/usr/share/sysbench/oltp_common.lua
/usr/share/sysbench/oltp_delete.lua
/usr/share/sysbench/oltp_insert.lua
/usr/share/sysbench/tests/include/oltp_legacy/oltp_simple.lua
/usr/share/sysbench/oltp_point_select.lua

# 批量插入数据
/usr/share/sysbench/tests/include/oltp_legacy/bulk_insert.lua
/usr/share/sysbench/tests/include/oltp_legacy/common.lua
# 删除数据
/usr/share/sysbench/tests/include/oltp_legacy/delete.lua
# 单值插入数据
/usr/share/sysbench/tests/include/oltp_legacy/insert.lua
# 混合读写测试,读写比例14:4
/usr/share/sysbench/tests/include/oltp_legacy/oltp.lua
/usr/share/sysbench/tests/include/oltp_legacy/oltp_simple.lua
/usr/share/sysbench/tests/include/oltp_legacy/parallel_prepare.lua
# 简单的主键查询
/usr/share/sysbench/tests/include/oltp_legacy/select.lua
/usr/share/sysbench/tests/include/oltp_legacy/select_random_points.lua
/usr/share/sysbench/tests/include/oltp_legacy/select_random_ranges.lua
/usr/share/sysbench/tests/include/oltp_legacy/update_index.lua
/usr/share/sysbench/tests/include/oltp_legacy/update_non_index.lua

安装

具体的可以参考官方github上的文档,以常用的操作系统为例。

  • Debian/Ubuntu

      $ apt -y install make automake libtool pkg-config libaio-dev
      # For MySQL support
      $ apt -y install libmysqlclient-dev libssl-dev
      # For PostgreSQL support
      $ apt -y install libpq-dev
    
  • RHEL/CentOS

      $ yum -y install make automake libtool pkgconfig libaio-devel
      # For MySQL support, replace with mysql-devel on RHEL/CentOS 5
      $ yum -y install mariadb-devel openssl-devel
      # For PostgreSQL support
      $ yum -y install postgresql-devel
    

用法

sysbench的常规命令行语法为:

sysbench [options]... [testname] [command] 
  • testname是内置测试的可选名称(例如fileio,内存,cpu等),或捆绑的Lua脚本之一的名称(例如oltp_read_only),或者是自定义Lua脚本的路径。如果在命令行上未指定测试名称(因此也没有命令,因为在这种情况下它将被解析为测试名称),或者测试名称是破折号(“-”),则sysbench期望输入Lua脚本在其标准输入上执行。

  • command是一个可选参数,它将由sysbench传递给使用testname指定的内置测试或脚本。命令定义测试必须执行的操作。可用命令的列表取决于特定的测试。一些测试还实现了自己的自定义命令。

    以下是典型测试命令及其用途的说明:

    • prepare:为需要测试的测试执行准备操作,例如在磁盘上创建用于fileio测试的必要文件,或为数据库基准填充测试数据库。
    • run:运行使用testname参数指定的实际测试。所有测试均提供此命令。
    • cleanup:在那些创建一个测试的测试中,在测试运行后删除临时数据。
    • help:显示使用testname参数指定的测试的用法信息。这包括测试提供的命令的完整列表,因此应使用它来获取可用的命令。
  • options是零个或多个以’-‘开头的命令行选项的列表。与命令一样,sysbench testname help命令应用于描述特定测试提供的可用选项。

有关sysbench本身提供的常规选项的说明,请参见常规命令行选项。

您可以使用sysbench –help显示常规命令行语法和选项。

常规命令行选项

PS: 本文内容翻译自sysbench官网,时间为2019-9-28 17:52:24,后续更新请优先访问官网

下表列出了受支持的常用选项,它们的描述和默认值:

Option Description Default value
--threads The total number of worker threads to create 1
--events Limit for total number of requests. 0 (the default) means no limit 0
--time Limit for total execution time in seconds. 0 means no limit 10
--warmup-time Execute events for this many seconds with statistics disabled before the actual benchmark run with statistics enabled. This is useful when you want to exclude the initial period of a benchmark run from statistics. In many benchmarks, the initial period is not representative because CPU/database/page and other caches need some time to warm up 0
--rate Average transactions rate. The number specifies how many events (transactions) per seconds should be executed by all threads on average. 0 (default) means unlimited rate, i.e. events are executed as fast as possible 0
--thread-init-timeout Wait time in seconds for worker threads to initialize 30
--thread-stack-size Size of stack for each thread 32K
--report-interval Periodically report intermediate statistics with a specified interval in seconds. Note that statistics produced by this option is per-interval rather than cumulative. 0 disables intermediate reports 0
--debug Print more debug info off
--validate Perform validation of test results where possible off
--help Print help on general syntax or on a specified test, and exit off
--verbosity Verbosity level (0 - only critical messages, 5 - debug) 4
--percentile sysbench measures execution times for all processed requests to display statistical information like minimal, average and maximum execution time. For most benchmarks it is also useful to know a request execution time value matching some percentile (e.g. 95% percentile means we should drop 5% of the most long requests and choose the maximal value from the remaining ones). This option allows to specify a percentile rank of query execution times to count 95
--luajit-cmd perform a LuaJIT control command. This option is equivalent to luajit -j. See LuaJIT documentation for more information

随机数选项

PS: 本文内容翻译自sysbench官网,时间为2019-9-28 17:52:24,后续更新请优先访问官网

sysbench提供了多种算法来生成随机数,这些随机数根据给定的概率分布进行分配。 下表列出了可用于控制这些算法的选项。

Option Description Default value
--rand-type random numbers distribution {uniform, gaussian, special, pareto, zipfian} to use by default. Benchmark scripts may choose to use either the default distribution, or specify it explictly, i.e. override the default. special
--rand-seed seed for random number generator. When 0, the current time is used as an RNG seed. 0
--rand-spec-iter number of iterations for the special distribution 12
--rand-spec-pct percentage of the entire range where ‘special’ values will fall in the special distribution 1
--rand-spec-res percentage of ‘special’ values to use for the special distribution 75
--rand-pareto-h shape parameter for the Pareto distribution 0.2
--rand-zipfian-exp shape parameter (theta) for the Zipfian distribution 0.8

记一次数据库压测过程

这里压测的是金山云的云数据库,配置为MySQL5.7/32G/100

下面参数请根据实际情况修改。

  • prepare

    首先我们先准备数据(注意在执行命令前,我们需要将数据库创建好)

      $ sysbench /usr/share/sysbench/tests/include/oltp_legacy/oltp.lua \
          --oltp_tables_count=1 \
          --report-interval=10 \
          --oltp-table-size=10000000 \
          --mysql-user=*** \
          --mysql-password=*** \
          --mysql-table-engine=innodb \
          --rand-init=on \
          --mysql-host=**** \
          --mysql-port=*** \
          --mysql-db=*** \
          --time=300 \
          --max-requests=0 \
          --oltp_skip_trx=on \
          --oltp_auto_inc=off \
          --oltp_secondary=on \
          --oltp_range_size=5 \
          --threads=30 \
          --events=5000000 \
          --threads=200 \
          prepare
    
  • run

    执行测试

      $ sysbench /usr/share/sysbench/tests/include/oltp_legacy/oltp.lua \
          --oltp_tables_count=1 \
          --report-interval=10 \
          --oltp-table-size=10000000 \
          --mysql-user=*** \
          --mysql-password=*** \
          --mysql-table-engine=innodb \
          --rand-init=on \
          --mysql-host=**** \
          --mysql-port=*** \
          --mysql-db=*** \
          --time=300 \
          --max-requests=0 \
          --oltp_skip_trx=on \
          --oltp_auto_inc=off \
          --oltp_secondary=on \
          --oltp_range_size=5 \
          --threads=30 \
          --events=5000000 \
          --threads=200 \
          run
    

参考

sysbench官网

https://cloud.tencent.com/developer/article/1068456

https://help.aliyun.com/document_detail/65637.html