sysBench是一个模块化工具,用于测试各种不同系统参数下的OLAP(MySQL/PorsgreSQL/Oracle)数据库负载情况。
仓库地址:https://github.com/akopytov/sysbench
(1)CPU性能 (2)磁盘IO性能 (3)内存分配及传输速度 (4)POSIX线程性能 (5)mutex调度性能 (6)数据库性能(OLTP基准测试)
系统安装
编译、yum都可以,网上很多教程。
指令参数
shellsysbench [general-options]... --test=<test-name> [test-options]... command --threads: 线程数,若设置为2,则sysbench会启动2个线程,同时分别进行素数的计算,默认值为1。 --time: 运行时长,单位秒。若设置为5,则sysbench会在5秒内循环进行素数计算,每完成一轮就是一个event,默认值为10。 --events: event上限次数,若设置为100,则表示当完成100次event后,即使时间富余也停止运行。默认值为0,则表示不限event次数。 --forced-shutdown=STRING:超时强制中断,默认为off。 --thread-stack-size=SIZE:线程栈大小,默认64K --thread-init-timeout=N:线程初始化等待时间,默认为30秒 --rate=N:平均事务率,0表示不限制,默认为0。 --report-interval=N:测试进度报告输出的时间间隔,0表示关闭,默认为0。 --report-checkpoints=[LIST,...]:转储完全统计信息并在指定时间点复位所有计数器,参数是逗号分隔值的列表,表示从必须执行报告检查点的测试开始所经过的时间(以秒为单位)。 默认情况下,报告检查点处于关闭状态[off]。 --debug[=on|off]:打印调试信息,默认为off --validate[=on|off]:尽可能执行验证检查,默认为off。 --help[=on|off]:打印help信息并退出,默认为off --version[=on|off]:打印版本信息并退出,默认为off --config-file=FILENAME:命令行选项文件 --luajit-cmd=STRING:执行LuaJIT控制命令
commond:
shellprepare:测试前准备工作,例如为fileis测试在磁盘上创建必要的文件,为OLTP测试准备测试数据 run:执行完整的测试,必须指定–-test选项 cleanup:测试结束后删除数据 help:帮助
**CPU性能测试 **
shellsysbench --test=cpu help cpu options: --cpu-max-prime=N upper limit for primes generator [10000]
--cpu-max-prime:素数生成数量的上限,若设置为3,则表示2、3、5(要计算1-5共5次),默认值为10000。
shellsysbench cpu --cpu-max-prime=20000 --threads=8 --time=30 run
logPrime numbers limit: 20000,每个线程产生的素数上限均为2000 events per second: 721.63,所有线程每秒完成了721.63次event total time: 30.0056s,共耗时30秒 total number of events: 21654,30秒内所有线程一共完成了21654次event min: 2.70,完成1次event的最少耗时2.70秒 avg: 11.08,所有event的平均耗时11.08毫秒 max: 129.76,完成1次event的最多耗时129.76毫秒 95th percentile: 30.26, 95%次event在30.26秒毫秒内完成 sum: 239822.50,每个线程耗时30秒,8个线程叠加耗时就是240秒 events (avg/stddev): 2706.7500/362.10 // 平均每个线程完成2706次event,标准差为362 execution time (avg/stddev): 29.9778/0.01 // 每个线程平均耗时29秒,标准差为0
内存负载测试
shellsysbench --test=memory help memory options: --memory-block-size=SIZE 测试block块大小 [默认1K] --memory-total-size=SIZE 文件总传输量 [默认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]
开启12个线程,内存块大小为8K,顺序读写
shellsysbench memory --threads=12 --memory-block-size=8K --memory-total-size=100G --memory-access-mode=seq run Running the test with following options: Number of threads: 12 Initializing random number generator from current time Running memory speed test with the following options: block size: 8KiB total size: 102400MiB operation: write scope: global Initializing worker threads... Threads started! Total operations: 13107192 (1684079.88 per second) 102399.94 MiB transferred (13156.87 MiB/sec) General statistics: total time: 7.7816s total number of events: 13107192 Latency (ms): min: 0.00 avg: 0.01 max: 139.09 95th percentile: 0.00 sum: 68748.61 Threads fairness: events (avg/stddev): 1092266.0000/0.00 execution time (avg/stddev): 5.7291/1.08 sysbench memory --threads=12 --memory-block-size=8K --memory-total-size=100G --memory-access-mode=rnd run Running the test with following options: Number of threads: 12 Initializing random number generator from current time Running memory speed test with the following options: block size: 8KiB total size: 102400MiB operation: write scope: global Initializing worker threads... Threads started! Total operations: 3099810 (309929.69 per second) 24217.27 MiB transferred (2421.33 MiB/sec) General statistics: total time: 10.0002s total number of events: 3099810 Latency (ms): min: 0.00 avg: 0.04 max: 95.04 95th percentile: 0.01 sum: 116396.55 Threads fairness: events (avg/stddev): 258317.5000/22378.41 execution time (avg/stddev): 9.6997/0.09
磁盘IO性能基准测试
shellsysbench --test=fileio help fileio options: --file-num=N:生成测试文件的数量,默认为128。 --file-block-size=N:测试时所使用文件块的大小,如果想磁盘针对innodb存储引擎进行测试,可以将其设置为16384(innodb存储引擎页的大小),默认为16384。 --file-total-size=SIZE:创建测试文件的总大小,默认为2G大小。 --file-test-mode=STRING:文件测试模式,seqwr(顺序写)、seqrewr(顺序读写)、seqrd(顺序读)、rndrd(随机读)、rndwr(随机写)、rndrw(随机读写)。 --file-io-mode=STRING:文件操作模式,sync(同步)、async(异步)、fastmmap(快速mmap)、slowmmap(慢速mmap),默认为sync。 --file-async-backlog=N:对应每个线程队列的异步操作数,默认为128。 --file-extra-flags=STRING:打开文件时的选项,与API相关的参数。 --file-fsync-freq=N:执行fsync函数的频率。fsync主要是同步磁盘文件,0代表不使用fsync函数,默认值为100。 --file-fsync-all=[on|off]:每执行完一次写操作,就执行一次fsync。默认为off。 --file-fsync-end=[on|off]:测试结束时执行fsync函数,默认为on。 --file-fsync-mode=STRING:文件同步函数选择,和API相关的参数,由于多个操作系统对于fdatasync支持不同,因此不建议使用fdatasync,默认为fsync。 --file-merged-requests=N:大多情况下,合并可能的IO的请求数,默认为0。 --file-rw-ratio=N:测试时的读写比例,默认时为1.5,即可3:2。
shell#数据准备
sysbench fileio --file-num=16 --file-total-size=2G prepare
#测试
sysbench fileio --file-total-size=2G --file-test-mode=rndrd --time=180 --threads=16 --file-num=16 --file-extra-flags=direct --file-fsync-freq=0 --file-block-size=16384 run
#测试数据清理
sysbench fileio --file-num=16 --file-total-size=2G cleanup
logRunning the test with following options: Number of threads: 16 Initializing random number generator from current time Extra file open flags: directio 16 files, 128MiB each 2GiB total file size Block size 16KiB Number of IO requests: 0 Read/Write ratio for combined random IO test: 1.50 Calling fsync() at the end of test, Enabled. Using synchronous I/O mode Doing random read test Initializing worker threads... Threads started! File operations: reads/s: 2291.53 writes/s: 0.00 fsyncs/s: 0.00 Throughput: read, MiB/s: 35.81 written, MiB/s: 0.00 General statistics: total time: 180.0079s total number of events: 412498 Latency (ms): min: 0.10 avg: 6.98 max: 49.85 95th percentile: 9.91 sum: 2879915.11 Threads fairness: events (avg/stddev): 25781.1250/46.71 execution time (avg/stddev): 179.9947/0.00
多线程调度基准测试
shellsysbench --test=mutex help 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]
shellsysbench mutex --threads=8 --mutex-num=2000 --mutex-locks=10000 --mutex-loops=5000 run Running the test with following options: Number of threads: 8 Initializing random number generator from current time Initializing worker threads... Threads started! General statistics: total time: 0.1319s total number of events: 8 Latency (ms): min: 33.03 avg: 98.79 max: 128.91 95th percentile: 127.81 sum: 790.31 Threads fairness: events (avg/stddev): 1.0000/0.00 execution time (avg/stddev): 0.0988/0.03
POST多线程基准测试
shellsysbench --test=threads help threads options: --threads=N 开启线程数量 --thread-yields=N number of yields to do per request [1000] --thread-locks=N number of locks per thread [8]
shellsysbench threads --threads=8 --thread-yields=200 --thread-locks=2 run Running the test with following options: Number of threads: 8 Initializing random number generator from current time Initializing worker threads... Threads started! General statistics: total time: 10.0018s total number of events: 30114 Latency (ms): min: 0.16 avg: 2.66 max: 239.52 95th percentile: 7.56 sum: 79992.02 Threads fairness: events (avg/stddev): 3764.2500/200.20 execution time (avg/stddev): 9.9990/0.00
OLTP基准测试
shell#参数解析
#连接信息
--mysql-host:MySQL服务器主机名,默认localhost;如果在本机上使用localhost报错,提示无法连接MySQL服务器,改成本机的IP地址应该就可以了。
--mysql-port:MySQL服务器端口,默认3306
--mysql-user:用户名
--mysql-password:密码
#=执行参数
--oltp-test-mode:执行模式,包括simple、nontrx和complex,默认是complex。simple模式下只测试简单的查询;nontrx不仅测试查询,还测试插入更新等,但是不使用事务;complex模式下测试最全面,会测试增删改查,而且会使用事务。可以根据自己的需要选择测试模式。
--oltp-tables-count:测试的表数量,根据实际情况选择
--oltp-table-size:测试的表的大小,根据实际情况选择
--threads:客户端的并发连接数
--time:测试执行的时间,单位是秒,该值不要太短,可以选择120
Lua脚本用途
脚本名称 | 脚本作用 |
---|---|
bulk_insert | 批量插入性能 |
oltp_common | 初始化脚本,表结构初始化,数据填充方式等 |
oltp_delete | 事务性删除性能 |
oltp_insert | 事务性插入性能 |
oltp_point_select | 点查性能 |
oltp_read_only | 读性能 |
oltp_read_write | 读写性能 |
oltp_update_index | 有索引情况下更新性能 |
oltp_update_non_index | 无索引情况下更新性能 |
oltp_write_only | 写性能 |
select_random_points | 随机点查 |
select_random_ranges | 随机范围查询 |
Lua脚本解析
oltp_common.lua
大部分的脚本都会底层调用oltp_common.lua
脚本里的函数stmt_defs
的变量
sql#point_selects:主键等值查询
SELECT c FROM sbtest%u WHERE id=?
#simple_ranges:简单范围查询(主键range)
SELECT c FROM sbtest%u WHERE id BETWEEN ? AND ?
#sum_ranges:范围求和(简单范围查询的基础上做sum)
SELECT SUM(k) FROM sbtest%u WHERE id BETWEEN ? AND ?
#order_ranges:简单范围查询(主键range的基础上做排序)
SELECT c FROM sbtest%u WHERE id BETWEEN ? AND ? ORDER BY c
#distinct_ranges:去重范围查询(主键range + 排序 + 去重)
SELECT DISTINCT c FROM sbtest%u WHERE id BETWEEN ? AND ? ORDER BY c
#index_updates:索引更新操作
UPDATE sbtest%u SET k=k+1 WHERE id=?
#non_index_updates:非索引更新
UPDATE sbtest%u SET c=? WHERE id=?
#deletes:删除操作
DELETE FROM sbtest%u WHERE id=?
#inserts:插入操作
INSERT INTO sbtest%u (id, k, c, pad) VALUES (?, ?, ?, ?)
oltp_common.lua
这个脚本相当于一个公共的函数库
lua-- 初始化
function init()
-- 创建表结构,获取c,pad字段随机字符串,插入数据,建立二级索引
function create_table(drv, con, table_num)
-- 创建表结构,表结构如下
-- CREATE TABLE `sbtest1` (
-- `id` int(11) NOT NULL AUTO_INCREMENT, # 主键
-- `k` int(11) NOT NULL DEFAULT '0', # 二级索引
-- `c` char(120) NOT NULL DEFAULT '',
-- `pad` char(60) NOT NULL DEFAULT '',
-- PRIMARY KEY (`id`),
-- KEY `k_1` (`k`)
-- ) ENGINE=InnoDB AUTO_INCREMENT=1000001 DEFAULT CHARSET=latin1
-- get_c_value() 获取字段c的值
-- get_pad_value() 获取字段pad的值
-- INSERT 语句插入到相关表,直到达到指定的table-size
-- 建立二级索引k
-- 获取字段c的值,返回一个随机字符串
function get_c_value()
-- 获取字段pad的值,返回一个随机字符串
function get_pad_value()
-- 初始化线程
function thread_init()
-- 关闭线程连接
function thread_done()
-- Close prepared statements
function close_statements()
-- 如果我们已经重新连接了,那么重新准备语句,
function sysbench.hooks.before_restart_event(errdesc)
-- close_statements() //Close prepared statements
-- prepare_statements() // This function is a 'callback' defined by individual benchmark scripts
-- 构造语句
function prepare_for_each_table(key)
-- 对应prepare命令
function cmd_prepare()
-- 对应prewarm命令
function cmd_prewarm()
-- 清理数据,把相关表都DROP掉
function cleanup()
-- 启动事务 BEGIN
function prepare_begin() //构造 BEGIN 语句
function begin() //执行 BEGIN 语句
-- 提交事务 COMMIT
function prepare_commit() //构造 BEGIN 语句
function commit() //执行 COMMIT 语句
-- 主键等值查询 SELECT c FROM sbtest? WHERE id = ? function prepare_point_selects() // 调用 prepare_for_each_table() 函数生成ponit_selects语句 function execute_point_selects() // 执行 prepare_point_selects() 生成的语句
-- 主键范围查询 SELECT c FROM sbtest? WHERE id BETWEEN ? AND ?
function prepare_simple_ranges() // 调用 prepare_for_each_table() 函数生成simple_ranges语句 function execute_simple_ranges() // 执行 prepare_point_selects() 生成的语句
-- 主键范围查询 + 求和 SELECT SUM(k) FROM sbtest? WHERE id BETWEEN ? AND ?
function prepare_sum_ranges() // 调用 prepare_for_each_table() 函数生成sum_ranges语句 function execute_sum_ranges() // 执行 prepare_point_selects() 生成的语句
-- 主键范围查询 + 排序 SELECT c FROM sbtest%u WHERE id BETWEEN ? AND ? ORDER BY c function prepare_order_ranges()
function execute_order_ranges()
-- 主键范围查询 + 排序 + 去重 SELECT DISTINCT c FROM sbtest%u WHERE id BETWEEN ? AND ? ORDER BY c
function prepare_distinct_ranges()
function execute_distinct_ranges()
-- 二级索引上更新(k是二级索引) UPDATE sbtest%u SET k=k+1 WHERE id=? function prepare_index_updates() // 调用 prepare_for_each_table() 函数生成index_updates语句 function execute_index_updates() // 执行 prepare_point_selects() 生成的语句
-- 无索引的更新 UPDATE sbtest%u SET c=? WHERE id=? function prepare_non_index_updates()
function execute_non_index_updates()
-- 删除 DELETE FROM sbtest%u WHERE id=?
function prepare_delete_inserts() function execute_delete_inserts() // 执行 prepare_for_each_table 生成的语句
读写测试的语句格式如下(定义在stmt_defs
变量中)
lua--point_selects:主键等值查询
SELECT c FROM sbtest%u WHERE id=?
-- simple_ranges:简单范围查询(主键range)
SELECT c FROM sbtest%u WHERE id BETWEEN ? AND ?
-- sum_ranges:范围求和(简单范围查询的基础上做sum)
SELECT SUM(k) FROM sbtest%u WHERE id BETWEEN ? AND ?
-- order_ranges:简单范围查询(主键range的基础上做排序)
SELECT c FROM sbtest%u WHERE id BETWEEN ? AND ? ORDER BY c
-- distinct_ranges:去重范围查询(主键range + 排序 + 去重)
SELECT DISTINCT c FROM sbtest%u WHERE id BETWEEN ? AND ? ORDER BY c
-- index_updates:索引更新操作
UPDATE sbtest%u SET k=k+1 WHERE id=?
-- non_index_updates:非索引更新
UPDATE sbtest%u SET c=? WHERE id=?
-- deletes:删除操作
DELETE FROM sbtest%u WHERE id=?
-- inserts:插入操作
INSERT INTO sbtest%u (id, k, c, pad) VALUES (?, ?, ?, ?)
bulk_insert.lua 插入的lua脚本
lua-- 初始化线程
function thread_init()
-- 准备表结构
prepare()
-- CREATE TABLE IF NOT EXISTS sbtest%d (
-- id INTEGER NOT NULL,
-- k INTEGER DEFAULT '0' NOT NULL,
-- PRIMARY KEY (id))]], i))
-- 批量插入
event()
-- 关闭线程
thread_done(thread_9d)
-- 清理数据,DROP掉相关的表
cleanup()
oltp_delete.lua DELETE语句的lua脚本
lua-- 加载oltp_common脚本
require("oltp_common")
-- 构造DELETE语句
prepare_statements()
-- prepare_for_each_table("deletes") 构造DELETE语句
-- 执行DELETE语句
event()
oltp_insert.lua INSERT语句的Lua脚本
lua-- 加载oltp_common脚本
require("oltp_common")
-- 构造DELETE语句
prepare_statements()
-- 执行DELETE语句
event()
oltp_point_select.lua
点查语句的Lua脚本,主键查询SELECT c FROM sbtest? WHERE id = ?
lua-- 加载oltp_common脚本
require("oltp_common")
-- 构造点查语句
prepare_statements()
-- 执行语句
event()
oltp_read_only.lua 只读语句的Lua脚本
lua-- 加载oltp_common脚本
require("oltp_common")
-- 构造语句
prepare_statements()
-- prepare_point_selects() // 构造点查语句
-- prepare_begin() // 构造 BEGIN 语句
-- prepare_commit() // 构造 COMMIT 语句
-- prepare_simple_ranges() // 构造简单范围查询语句
-- prepare_sum_ranges() // 构造求和语句
-- prepare_order_ranges() // 构造排序语句
-- prepare_distinct_ranges() // 构造排序去重语句
-- 执行语句
event()
-- execute_point_selects() // 执行点查语句
-- execute_simple_ranges() // 执行简单范围查询语句
-- execute_sum_ranges() // 执行求和语句
-- execute_order_ranges() // 执行排序语句
-- execute_distinct_ranges() //排序去重语句
oltp_read_write.lua 读写的Lua脚本
lua-- 加载oltp_common脚本
require("oltp_common")
-- 构造语句
prepare_statements()
-- 读操作
-- prepare_point_selects() // 构造点查语句
-- prepare_begin() // 构造 BEGIN 语句
-- prepare_commit() // 构造 COMMIT 语句
-- prepare_simple_ranges() // 构造简单范围查询语句
-- prepare_sum_ranges() // 构造求和语句
-- prepare_order_ranges() // 构造排序语句
-- prepare_distinct_ranges() // 构造排序去重语句
-- 写操作
-- prepare_index_updates() // 构造更新索引操作语句
-- prepare_non_index_updates() // 构造更新无索引字段操作语句
-- prepare_delete_inserts() // 构造删除+更新操作语句
-- 执行语句
event()
-- 读操作
-- execute_point_selects() // 执行点查语句
-- execute_simple_ranges() // 执行简单范围查询语句
-- execute_sum_ranges() // 执行求和语句
-- execute_order_ranges() // 执行排序语句
-- execute_distinct_ranges() //排序去重语句
-- 写操作
-- execute_index_updates() // 执行更新索引操作语句
-- execute_non_index_updates() // 执行更新无索引字段操作语句
-- execute_delete_inserts() // 执行删除+更新操作语句
oltp_update_index.lua 更新索引字段操作的Lua语句
lua-- 加载oltp_common脚本
require("oltp_common")
-- 构造语句
prepare_statements()
-- prepare_index_updates()
-- 执行语句
event()
-- execute_index_updates(con)
oltp_update_non_index.lua 更新无索引字段操作语句
lua-- 加载oltp_common脚本
require("oltp_common")
-- 构造语句
prepare_statements()
-- prepare_non_index_updates()
-- 执行语句
event()
-- execute_non_index_updates()
oltp_write_only.lua 纯写语句的Lua脚本
lua-- 加载oltp_common脚本
require("oltp_common")
-- 构造语句
prepare_statements()
-- prepare_index_updates()
-- prepare_non_index_updates()
-- prepare_delete_inserts()
-- 执行语句
event()
-- execute_index_updates()
-- execute_non_index_updates()
-- execute_delete_inserts()
select_random_points.lua 随机点查脚本
lua-- 加载oltp_common脚本
require("oltp_common")
-- 准备阶段
prepare()
-- 线程初始化
thread_init()
-- 关闭线程
thread_done()
-- 执行相关SQL
event()
select_random_ranges.lua
lua-- 加载oltp_common脚本
require("oltp_common")
-- 准备阶段
prepare()
-- 线程初始化
thread_init()
-- 关闭线程
thread_done()
-- 执行相关SQL
event()
oints.lua 随机点查脚本
lua-- 加载oltp_common脚本
require("oltp_common")
-- 准备阶段
prepare()
-- 线程初始化
thread_init()
-- 关闭线程
thread_done()
-- 执行相关SQL
event()
select_random_ranges.lua
lua-- 加载oltp_common脚本
require("oltp_common")
-- 准备阶段
prepare()
-- 线程初始化
thread_init()
-- 关闭线程
thread_done()
-- 执行相关SQL
event()
测试例子:
shell#准备测试脚本
mysqladmin -hxxx -uxxx -pxxx -P3306 create sbtest;
sysbench --mysql-host=xxx \
--mysql-port=3306 \
--mysql-user=xxx \
--mysql-password=xxx \
--mysql-db=sbtest \
/usr/share/sysbench/oltp_common.lua \
--tables=10 \
--table_size=100000 \
prepare
#压测脚本
sysbench --db-driver=mysql --time=30 --threads=5 --report-interval=1 \
--mysql-host=xxx \
--mysql-port=3306 \
--mysql-user=xxx \
--mysql-password=xxx \
--mysql-db=sbtest --tables=10 --table_size=1000000 \
/usr/share/sysbench/oltp_point_select.lua \
--db-ps-mode=disable run
#清理测试数据
sysbench --db-driver=mysql --time=30 --threads=5 --report-interval=1 \
--mysql-host=xxx \
--mysql-port=3306 \
--mysql-user=xxx \
--mysql-password=xxx \
--mysql-db=sbtest --tables=10 --table_size=1000000 \
oltp_read_only --db-ps-mode=disable cleanup
本文作者:wangcw
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!