sysbench本身就是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试,数据库目前支持MySQL/Oracle/PostgreSQL了,下面给各位介绍sysbench自定义lua脚本实现实际的业务逻辑压力测试例子.
使用sysbench进行mysql的oltp测试,一般的测试在sysbench中在tests/db中提供了一个oltp.lua脚本可以进行oltp的压力测试.
但不能完全模拟自己实际业务的压力测试,不同的业务,数据结构,数据量都是不一样的,为了更接近实际业务的读写压力测试,就得自己写lua脚本,然后通过sysbench进行压力测试.
写这个lua脚本很简单,只需要会写lua脚本就可以了.
1、首先收集实际业务的访问数据库的sql;
2、准备一台恢复好的备份库(从线上导一个).
3、将收集的sql写在lua脚本里(具体如何写,后面举个例子).
4、通过sysbench的--test参数和--mysql-db参数进行测试(这里就不需要prepare了,直接run就行).
举个小例子,模拟实际业务环境:
- CREATE TABLE `t1` (
- `id` int(10) NOT NULL AUTO_INCREMENT,
- `name` varchar(50) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=10101 DEFAULT CHARSET=utf8
插入模拟的际业务数据:
- delimiter $$
- create procedure add_data(in maxnum int)
- begin
- declare i int default 0;
- declare s varchar(500);
- while(i<maxnum) do
- select concat("abcdedsfsfsdfsfsf",i) into s;
- insert into t1(name)values(s);
- set i=i+1;
- end while;
- end $$
- delimiter ;
- call add_data(10000);
准备lua脚本:[root@sever3 db]# cat test.lua
- function thread_init(thread_id)
- -- set_vars()
- db_connect()
- end
- //Vevb.com
- function event(thread_id)
- local table_name
- local rs
- table_name = "t1"
- db_query("begin")
- for i=1, 10000 do
- rs = db_query("SELECT name FROM ".. table_name .." WHERE id=" .. i)
- end
- end
set_vars() 如果需要使用更多的参数,需要执行这个,前面需要引用下common.lua
db_connect() 是连接数据库的,这个是sysbench里的函数,不管那么多,直接用就行。
thread_init() 第一个调用的lua函数
event(thread_id) 可以把sql逻辑写到这里,–num-threads多少个,就会同时调用多少个,然后进行压测就ok.
- [root@sever3 sysbench]# ./sysbench --mysql-socket=/data/mysql_3309/mysql.sock --test=tests/db/test.lua --mysql-user=root --num-threads=12 --report-interval=10 --rand-type=uniform --max-time=30 --max-requests=0 --percentile=99 --mysql-db=test run
- sysbench 0.5: multi-threaded system evaluation benchmark
- Running the test with following options:
- Number of threads: 12
- Report intermediate results every 10 second(s)
- Random number generator seed is 0 and will be ignored
- Threads started!
- [ 10s] threads: 12, tps: 0.00, reads/s: 64131.41, writes/s: 0.00, response time: 3291.51ms (99%)
- [ 20s] threads: 12, tps: 0.00, reads/s: 79980.83, writes/s: 0.00, response time: 1947.61ms (99%)
- [ 30s] threads: 12, tps: 0.00, reads/s: 78354.15, writes/s: 0.00, response time: 2418.21ms (99%)
- OLTP test statistics:
- queries performed:
- read: 2280000
- write: 0
- other: 228
- total: 2280228
- transactions: 0 (0.00 per sec.)
- deadlocks: 0 (0.00 per sec.)
- read/write requests: 2280000 (72705.35 per sec.)
- other operations: 228 (7.27 per sec.)
- General statistics:
- total time: 31.3595s
- total number of events: 228
- total time taken by event execution: 368.0393s
- response time:
- min: 985.61ms
- avg: 1614.21ms
- max: 3756.13ms
- approx. 99 percentile: 3289.54ms
- Threads fairness:
- events (avg/stddev): 19.0000/3.83
- execution time (avg/stddev): 30.6699/0.42
新闻热点
疑难解答