使用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
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