我的收藏linux 性能专题

性能测试工具SysBench

2019-09-27  本文已影响0人  我是孟小鱼呀

sysbench是一个模块化的、跨平台、多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况。
它主要包括以下几种方式的测试:
1、cpu性能
2、磁盘io性能
3、mutex性能
4、内存分配及传输速度
5、POSIX线程性能
6、数据库性能(OLTP基准测试)
目前sysbench主要支持 MySQL,pgsql,oracle 这3种数据库。

一、安装

安装环境:CentOS7

1、二进制包安装

curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | bash 
yum -y install sysbench
sysbench --version  # 验证sysbench是否安装成功

2、源码安装

(1)安装依赖包

    yum -y install make automake libtool pkgconfig libaio-devel
    yum -y install mariadb-devel openssl-devel  # 安装MySQL依赖
    yum -y install postgresql-devel # 安装PostgreSQL依赖

(2)下载源码包,解压安装

https://github.com/akopytov/sysbench  # 下载地址
unzip  sysbench-master.zip
cd sysbench-master
./autogen.sh   # 生成configure文件
./configure  
--prefix=/usr/local/sysbench/  #安装路径
--with-mysql  
--with-mysql-includes=/usr/local/mysql/include  # mysql安装在/usr/local/
--with-mysql-libs=/usr/local/mysql/lib 
make
make install

vim /etc/profile  # 添加环境变量
# sysbench path 
PATH=$PATH:/usr/local/sysbench/bin 
export PATH   #添加以上两行,路径改为自己安装的路径位置                                                                                                                                               
source /etc/profile  #生效环境变量

sysbench --version # 测试安装成功
sysbench 1.1.0

二、语法介绍

sysbench [options]... [testname] [command] 

testname 为测试对象,一般是CPU、内存、IO等

command:

options:(下表列出了options的常用选项和默认值)

选项 描述 默认值
--threads 要创建的工作线程总数 1
--events 请求总数的限制。0(默认值)表示没有限制 0
--time 限制总执行时间(以秒为单位)。0表示没有限制 10
--warmup-time 预热时间,默认值为0 0
--rate 平均交易率。该数字指定平均每个线程每秒应执行的事件数。0(默认值)表示无限速率,即事件尽可能快地执行 0
--thread-init-timeout 工作线程初始化的等待时间(以秒为单位) 30
--thread-stack-size 每个线程的堆栈大小 32K
--report-interval 以秒为单位统计定期报告中间信息 。此选项生成的统计信息是按时间间隔而不是累积。0表示禁用中间报告 0
--debug 打印更多调试信息 off
--validate 尽可能验证测试结果 off
--help 有关一般语法或指定测试的帮助 off
--verbosity 详细级别(0 - 仅关键消息,5 - 调试) 4
--percentile sysbench测量所有已处理请求的执行时间,以显示最小,平均和最大执行时间等统计信息。此选项允许指定要计数的查询执行时间的百分比 95
--luajit-cmd 执行LuaJIT控制命令。此选项相当于luajit -j

三、使用方法

1、CPU性能测试

sysbench cpu help  # 查看帮助信息
sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)

cpu options:
  --cpu-max-prime=N upper limit for primes generator [10000]

测试命令 :最大质数发生器数量为2000,线程数为2

sysbench cpu --cpu-max-prime=20000 --threads=2 run
image.png

从执行结果可以看到CPU的运行速度,一些统计数据和延迟大小等信息

2、内存分配及传输速度

sysbench memory help # 查看帮助信息
sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)

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]

测试命令 :测试整个过程是在内存中传输 2G 的数据量,每个 block 大小为 8K。

sysbench memory --memory-block-size=8k --memory-total-size=2G run
image.png

3、磁盘IO性能测试

sysbench fileio help  # 查看帮助信息
sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)

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]
sysbench  fileio --threads=2 --file-total-size=1G --file-test-mode=rndrw prepare
image.png
sysbench  fileio --threads=2 --file-total-size=1G --file-test-mode=rndrw run
image.png
sysbench  fileio --threads=2 --file-total-size=1G --file-test-mode=rndrw cleanup
image.png

4、mutex性能测试

sysbench mutex help  # 查看帮助信息
sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)

mutex options:
  --mutex-num=N   total size of mutex array [4096]
  --mutex-locks=N number of mutex locks to do per thread [50000]
  --mutex-loops=N number of empty loops to do outside mutex lock [10000]

命令测试:线程数为2,数组互斥的总大小4096,每个线程互斥锁的数量为50000,内部互斥锁的空循环数量为10000

sysbench mutex --threads=2 --mutex-num=4096 --mutex-locks=50000 --mutex-loops=10000 run
image.png

5、POSXI线程性能

sysbench threads help  # 查看帮助信息
sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)

threads options:
  --thread-yields=N number of yields to do per request [1000]
  --thread-locks=N  number of locks per thread [8]

命令测试:线程数为2,每个请求产生多少个线程为100,每个线程的锁的数量为4

sysbench threads --threads=2 --thread-yields=100 --thread-locks=4 run
image.png

6、数据库性能(OLTP基准测试)

装完sysbench后,/usr/local/sysbench/ share/sysbench下对数据库压力测试的lua文件。 image.png 数据库压力测试通常三个阶段,准备数据、压测数据、清理数据
mysql -uroot -p               #进入数据库
> create database sbtest;     #创建测试数据库

sysbench /usr/local/sysbench/share/sysbench/oltp_read_write.lua  
--mysql-host=localhost   #数据库的IP
--mysql-port=3306    #数据库的端口
--mysql-user=root    #数据库用户名
--mysql-password='test'  #用户密码
--mysql-socket=/data/mysql/mysql.sock   #socket的路径
--mysql-db=sbtest   #数据库名字,默认为sysbench,需要提前创建创好
--db-driver=mysql  #用到的数据库类型 
--tables=10    #生成10张表
--table-size=50000   #每个表的行数
--threads=4    #启动多少个线程,即模拟多少个用户
prepare
image.png 登录数据库检查生成表和数据情况,已经生成了10张表 image.png
情况一:查询
sysbench /usr/local/sysbench/share/sysbench/oltp_read_write.lua  
--mysql-host=localhost  
--mysql-port=3306   
--mysql-user=root  
--mysql-password='test'  
--mysql-socket=/data/mysql/mysql.sock   
--mysql-db=sbtest  
--db-driver=mysql   
--tables=10  
--table-size=50000    
--report-interval=10  
--threads=128  
--time=600  
run
image.png

备注:重要指标
QPS(Query per second) 每秒查询量:639.04
TPS(Transaction per second)每秒事务量 12766.92

情况二:在每个查询的事物里面添加 INSERT/UPDATE/DELDETE 操作
sysbench /usr/local/sysbench/share/sysbench/oltp_read_write.lua  
--mysql-host=localhost  
--mysql-port=3306   
--mysql-user=root  
--mysql-password='Live400.Com'  
--mysql-socket=/data/mysql/mysql.sock   
--mysql-db=sbtest  
--db-driver=mysql   
--tables=10  
--table-size=50000   
--delete_inserts=10  
--index_updates=10  
--non_index_updates=10   
--report-interval=10  
--threads=4  
--time=60  
run 
image.png
sysbench /usr/local/sysbench/share/sysbench/oltp_read_write.lua  
--mysql-host=localhost  
--mysql-port=3306   
--mysql-user=root  
--mysql-password='test'  
--mysql-socket=/data/mysql/mysql.sock   
--mysql-db=sbtest  
--db-driver=mysql   
--tables=10  
--table-size=50000   
--delete_inserts=10  
--index_updates=10  
--non_index_updates=10   
--report-interval=10  
--threads=4  
--time=60   
cleanup
image.png
上一篇下一篇

猜你喜欢

热点阅读