Home  >  Article  >  Database  >  sysbench自定义lua脚本实现实际的业务逻辑压力测试

sysbench自定义lua脚本实现实际的业务逻辑压力测试

WBOY
WBOYOriginal
2016-06-07 16:41:562982browse

使用sysbench进行mysql的oltp测试,一般的测试在sysbench中在tests/db中提供了一个oltp.lua脚本可以进行oltp的压力测试。 但不能完全模拟自己实际业务的压力测试,不同的业务,数据结构,数据量都是不一样的,为了更接近实际业务的读写压力测试,就得自己写l

使用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 into s insert t1 set i="i+1;" end while delimiter call add_data>
<p>准备lua脚本:[root@sever3 db]# cat test.lua</p>
<pre class="brush:php;toolbar:false">function thread_init(thread_id)
   -- set_vars()
   db_connect()
end
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

sysbench具体的使用、结果解读可以参数下

http://imysql.com/2014/10/17/sysbench-full-user-manual.shtml

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn