Sysbench测试工具详解

Sysbench

Sysbench 是一个常用的多线程性能测试工具,可用于评估数据库系统和硬件的性能。它支持多种基准测试,包括 OLTP (Online Transaction Processing)、CPU、文件 I/O、内存等。

安装

去官方网站下mysql的repo文件
https://dev.mysql.com/downloads/repo/yum/

[root@localhost ~]# rpm -ivh mysql80-community-release-el7-8.noarch.rpm 
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo       CentOS-Media.repo          mysql-community-debuginfo.repo
CentOS-CR.repo         CentOS-Sources.repo        mysql-community.repo
CentOS-Debuginfo.repo  CentOS-Vault.repo          mysql-community-source.repo
CentOS-fasttrack.repo  CentOS-x86_64-kernel.repo

出现问题:

即使下载了官网的repo源,也不行

        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

解决方法:

  1. 尝试其他镜像源:如果DNS配置正确,但 mirrorlist.centos.org 仍然无法解析,可以尝试使用其他镜像源。在 /etc/yum.repos.d/ 目录下,有一个名为 CentOS-Base.repo 的文件,尝试编辑这个文件,将 mirrorlist 相关的行注释掉,然后启用 baseurl 行,并使用一个可用的镜像URL,例如:
rubyCopy code
baseurl=http://mirror.centos.org/centos/7/os/x86_64/

注意:这只是一个示例URL,您可以根据需要使用其他可用的镜像源。

  1. 清除YUM缓存:运行以下命令可以尝试清除YUM缓存并重新生成:
cssCopy code
yum clean all

编译安装下载

yum -y install mysql-community-devel
yum -y install automake  libtool
wget https://github.com/akopytov/sysbench/archive/1.0.15.tar.gz
tar xf 1.0.15.tar.gz
cd sysbench-1.0.15
./autogen.sh
./configure
make -j
make install

建议实验epel下载sysbench(yum安装

yum install epel-release -y
yum install sysbench -y

准备测试数据

首先创建sysbench所需数据库sbtest(这是sysbench默认使用的库名,必须创建测试库)。

mysqladmin -h127.0.0.1 -uroot -p123456 -P3306 create sbtest;

这是yum安装对应的

sysbench --mysql-host=192.168.2.25 --mysql-port=7002 --mysql-user=write --mysql-password='123456' /usr/share/sysbench/oltp_common.lua --tables=10 --table_size=10000 prepare

这是编译安装对应的

sysbench --mysql-host=192.168.2.25 --mysql-port=7002 --mysql-user=write --mysql-password='123456' /root/sysbench-1.0.15/src/lua
/oltp_common.lua  --tables=10 --table_size=10000 prepare

使用yum安装的路径进行测试

[root@localhost bin]# sysbench --mysql-host=192.168.2.25 --mysql-port=7002 --mysql-user=write --mysql-password='123456' /usr/share/sysbench/oltp_common.lua --tables=10 --table_size=10000 prepare
sysbench 1.0.17 (using system LuaJIT 2.0.4)

Creating table 'sbtest1'...
Inserting 10000 records into 'sbtest1'
Creating a secondary index on 'sbtest1'...
Creating table 'sbtest2'...
Inserting 10000 records into 'sbtest2'
Creating a secondary index on 'sbtest2'...
Creating table 'sbtest3'...
Inserting 10000 records into 'sbtest3'
Creating a secondary index on 'sbtest3'...
Creating table 'sbtest4'...
Inserting 10000 records into 'sbtest4'
Creating a secondary index on 'sbtest4'...
Creating table 'sbtest5'...
Inserting 10000 records into 'sbtest5'
Creating a secondary index on 'sbtest5'...
Creating table 'sbtest6'...
Inserting 10000 records into 'sbtest6'
Creating a secondary index on 'sbtest6'...
Creating table 'sbtest7'...
Inserting 10000 records into 'sbtest7'
Creating a secondary index on 'sbtest7'...
Creating table 'sbtest8'...
Inserting 10000 records into 'sbtest8'
Creating a secondary index on 'sbtest8'...
Creating table 'sbtest9'...
Inserting 10000 records into 'sbtest9'
Creating a secondary index on 'sbtest9'...
Creating table 'sbtest10'...
Inserting 10000 records into 'sbtest10'
Creating a secondary index on 'sbtest10'...

root@sbtest 11:32 mysql>show tables;
+------------------+
| Tables_in_sbtest |
+------------------+
| sbtest1          |
| sbtest10         |
| sbtest2          |
| sbtest3          |
| sbtest4          |
| sbtest5          |
| sbtest6          |
| sbtest7          |
| sbtest8          |
| sbtest9          |
+------------------+
10 rows in set (0.00 sec)

测试结果

基准测试命令: 使用 Sysbench 运行 MySQL 基准测试时,常见的命令格式如下:

cssCopy code
sysbench [options] --threads=[num_threads] --mysql-host=[mysql_host] --mysql-port=[mysql_port] --mysql-user=[mysql_user] --mysql-password=[mysql_password] --mysql-db=[mysql_database] [test_name]
  • [options]:可选的 Sysbench 选项,用于配置测试的参数,例如运行时间、请求率等。
  • --threads=[num_threads]:指定并发线程数,即同时运行的线程数。
  • --mysql-host=[mysql_host]:指定 MySQL 服务器的主机名或 IP 地址。
  • --mysql-port=[mysql_port]:指定 MySQL 服务器的端口号。
  • --mysql-user=[mysql_user]:指定连接 MySQL 服务器所使用的用户名。
  • --mysql-password=[mysql_password]:指定连接 MySQL 服务器所使用的密码。
  • --mysql-db=[mysql_database]:指定要进行测试的数据库名称。
  • [test_name]:指定要运行的具体测试,例如 OLTP、CPU、文件 I/O 等。
sysbench --threads=4 --time=20 --report-interval=5 --mysql-host=192.168.2.25 --mysql-port=7001 --mysql-user=write --mysql-password=123456 /usr/share/sysbench/oltp_read_write.lua --tables=10 --table-size=100000 run
[root@localhost sysbench]# sysbench --threads=4 --time=20 --report-interval=5 --mysql-host=192.168.2.25 --mysql-port=7001 --mysql-user=write --mysql-password=123456 /usr/share/sysbench/oltp_read_write.lua --tables=10 --table-size=100000 run
sysbench 1.0.17 (using system LuaJIT 2.0.4)

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!

[ 5s ] thds: 4 tps: 191.49 qps: 3850.57 (r/w/o: 2698.03/201.67/950.86) lat (ms,95%): 31.37 err/s: 0.60 reconn/s: 0.00
[ 10s ] thds: 4 tps: 209.08 qps: 4181.87 (r/w/o: 2927.17/242.93/1011.77) lat (ms,95%): 26.68 err/s: 0.00 reconn/s: 0.00
[ 15s ] thds: 4 tps: 198.05 qps: 3960.59 (r/w/o: 2772.09/254.12/934.37) lat (ms,95%): 30.81 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 4 tps: 192.85 qps: 3858.26 (r/w/o: 2701.95/262.43/893.88) lat (ms,95%): 33.72 err/s: 0.00 reconn/s: 0.00
SQL statistics:
    queries performed:
        read:                            55510
        write:                           4812
        other:                           18972
        total:                           79294
    transactions:                        3962   (197.75 per sec.)
    queries:                             79294  (3957.64 per sec.)
    ignored errors:                      3      (0.15 per sec.)
    reconnects:                          0      (0.00 per sec.)

General statistics:
    total time:                          20.0346s
    total number of events:              3962

Latency (ms):
         min:                                    9.20
         avg:                                   20.20
         max:                                 1010.51
         95th percentile:                       30.81
         sum:                                80041.23

Threads fairness:
    events (avg/stddev):           990.5000/34.51
    execution time (avg/stddev):   20.0103/0.01


根据sysbench的输出,这是一次OLTP读写测试的结果。让我对输出进行解析,以便您更好地了解测试的性能表现:

  1. 系统信息:
  • sysbench版本:1.0.17
  • 使用的LuaJIT版本:2.0.4
  1. 测试参数:
  • 线程数:4
  • 报告中间结果的间隔:每5秒报告一次
  • 使用的MySQL主机:192.168.2.25
  • 使用的MySQL端口:7001
  • 使用的MySQL用户:write
  • 使用的MySQL密码:123456
  • 测试脚本路径:/usr/share/sysbench/oltp_read_write.lua
  • 使用的表数:10
  • 每个表的大小:100,000行
  1. 测试过程:
  • 初始化随机数生成器。
  • 初始化工作线程。
  • 启动4个线程进行测试。
  1. 测试结果:
    在测试进行了20秒后,以下是测试的结果统计:
  • 在第5秒时,每秒处理事务数(tps)约为191.49,每秒查询数(qps)约为3850.57。
  • 在第10秒时,tps约为209.08,qps约为4181.87。
  • 在第15秒时,tps约为198.05,qps约为3960.59。
  • 在第20秒时,tps约为192.85,qps约为3858.26。
  1. SQL统计:
  • 读取查询数量:55510
  • 写入查询数量:4812
  • 其他查询数量:18972
  • 总查询数量:79294
  • 完成的事务数量:3962 (平均每秒197.75个事务)
  • 总查询数:79294 (平均每秒3957.64个查询)
  • 忽略的错误数:3 (平均每秒0.15个错误)
  • 重新连接数:0 (平均每秒0.00个重新连接)
  1. 总体统计:
  • 总耗时:20.0346秒
  • 总事件数:3962
  1. 延迟(以毫秒为单位):
  • 最小延迟:9.20ms
  • 平均延迟:20.20ms
  • 最大延迟:1010.51ms
  • 第95百分位延迟:30.81ms
  • 总延迟:80041.23ms
  1. 线程公平性:
  • 事件数量(平均/标准差):990.5000/34.51
  • 执行时间(平均/标准差):20.0103/0.01

解析结果表明,测试运行了20秒,使用4个线程,在给定的配置下,每秒处理的事务数在190到210之间浮动。请注意,性能结果还受到MySQL服务器和系统硬件等因素的影响,因此,如果要获得更精确的性能指标,可能需要在多个不同配置下运行测试,并进行更详细的分析。

清除建立的测试表

sysbench --mysql-host=192.168.2.25 \
         --mysql-port=7002 \
         --mysql-user=write \
         --mysql-password='123456' \
         /usr/share/sysbench/oltp_common.lua \
         --tables=10 \
         cleanup

效果

| 59741 | 50095 | 94054036382-30666175964-93983866440-82198879802-79178088086-626860264174919 |
| 60074 | 50328 | 71234724885-22289811426-29663594026-43624298282-65444509716-502104939320899 |
| 63215 | 49852 | 69814563728-47986046934-75862954779-06559782038-51838414177-222236416566077 |
| 65881 | 49778 | 74995573852-31949172824-75320100772-28469702486-62705248269-933810682682159 |
+-------+-------+-----------------------------------------------------------------------------+
10177 rows in set (0.13 sec)

root@sbtest 11:42 mysql>select * from sbtest1;
ERROR 1146 (42S02): Table 'sbtest.sbtest1' doesn't exist

删除成功了

当然也可以在master服务器上手动删除

出现问题,删除后如果我新建再次会出现数据库存在的问题

[root@localhost sysbench]# mysqladmin -h127.0.0.1 -uroot -p123456 -P3306 create sbtest;
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqladmin: CREATE DATABASE failed; error: 'Can't create database 'sbtest'; database exists'

原因:

有其他MySQL连接正在使用sbtest数据库,导致你无法删除。可以用show processlist;命令查看是否有进程在占用sbtest数据库。

*************************** 4. row ***************************
     Id: 12
   User: write
   Host: 192.168.2.25:35132
     db: sbtest
Command: Sleep
   Time: 1704
  State: 
   Info: NULL
4 rows in set (0.00 sec)

ERROR: 
No query specified

root@(none) 11:57 mysql>

解决:

最简单的方法是:在master服务器上重新创建一个sbtest数据库

  • 识别占用连接的线程id,然后使用kill命令终止该连接线程:
kill [线程id];
  • 如果不确定是哪个连接占用了sbtest数据库,可以直接杀死所有连接线程:
kill connection for database sbtest;
  • 使用disconnect命令 disconnect指定的线程:
disconnect [线程id];
  • 修改该连接的用户权限,禁用其访问sbtest数据库的权限:
revoke all privileges on sbtest.* from [用户名]@[主机名]; 
  • 设置sbtest数据库为只读模式,禁止写入操作:
flush tables with read lock;
  • 直接重启MySQL服务,关闭所有连接线程。
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
sysbench是一个多线程基准测试工具,可用于评估计算机系统的CPU、内存、磁盘和数据库等性能。它可以模拟多种工作负载,并提供测试结果和系统性能指标。 下面简要介绍一些sysbench的常用命令: 1. CPU性能测试命令: ``` sysbench cpu --cpu-max-prime=20000 --threads=4 run ``` --cpu-max-prime参数指定质数的最大值,--threads参数指定线程数。 2. 内存性能测试命令: ``` sysbench memory --memory-block-size=1K --memory-total-size=10G --threads=4 run ``` --memory-block-size参数指定内存块的大小,--memory-total-size参数指定总共测试的内存大小,--threads参数指定线程数。 3. 文件IO性能测试命令: ``` sysbench fileio --file-total-size=10G --file-test-mode=rndrw --max-time=300 --max-requests=0 run ``` --file-total-size参数指定测试文件的大小,--file-test-mode参数指定测试模式(顺序读/写、随机读/写等),--max-time参数指定测试时间,--max-requests参数指定测试请求数量。 4. 磁盘性能测试命令: ``` sysbench disk --test=fileio --file-total-size=10G --file-test-mode=rndrw --max-time=300 --max-requests=0 run ``` --test参数指定测试类型,这里是fileio,其余参数同文件IO性能测试。 5. 数据库性能测试命令: ``` sysbench oltp_read_write --mysql-db=test --mysql-user=root --mysql-password=password --mysql-host=localhost --oltp-table-size=1000000 --num-threads=4 --max-requests=0 --max-time=300 run ``` --mysql-db参数指定测试的数据库名,--mysql-user参数指定数据库用户名,--mysql-password参数指定数据库密码,--mysql-host参数指定数据库主机名,--oltp-table-size参数指定表大小,--num-threads参数指定线程数,--max-requests参数指定请求数量,--max-time参数指定测试时间。 以上是sysbench的一些常用命令,更多命令和参数可以查看官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈密猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值