在oltp测试中,它基本上分为三个阶段:
一是prepare阶段,在此阶段,sysbench会在被测试数据库中建立一张表,名为sbtest。这张表默认是在sbtest库中。你可以随意指定其它自己手动建立的库,即使你指定为sbtest库,库仍然需要你手动建立, sysbench并不会为你建立库。
sbtest表的结构如下:
CREATE
TABLE
`sbtest` (
`id`
int
(
10
) unsigned
NOT
NULL
AUTO_INCREMENT,
`k`
int
(
10
) unsigned
NOT
NULL
DEFAULT
'
0
'
,
`c`
char
(
120
)
NOT
NULL
DEFAULT
''
,
`pad`
char
(
60
)
NOT
NULL
DEFAULT
''
,
PRIMARY
KEY
(`id`),
KEY
`k` (`k`));
在表结构建立完成后,它会被填充入你指定的行数(--oltp-table-size)
二是run阶段,这个阶段中有三个mode可以使用:
1.simple mode,如果指定为此mode,那么在运行过程中, sysbench的每个thread将只会运行如下的语句:
SELECT
c
FROM
sbtest
WHERE
id
=
N
N值是个随机值,它的范围将会从1...<table size>
2.advanced transactional mode
如果启用此模式,那么sysbench的每个thread将会在测试表上(sbtest)上执行事务。不过要注意的是,对于表(sbtest使用的引擎)是否支持事务,其执行的过程是不一样的。如果是innodb engine,那么它将会使用BEGIN/COMMIT 语句来start/stop一个事务;如果是MyISAM engine,sysbench将会使用LOCK TABLES/UNLOCK TABLES语句。在一个事务中若是有一些记录被删除了,不用担心,在同一个事务中,会有相同的记录被补回。所以同一张sbtest测试表可以多次用来进行测试。
这个mode中包含执行的语句如下:
Point queries:
SELECT
c
FROM
sbtest
WHERE
id
=
N
Range queries:
SELECT
c
FROM
sbtest
WHERE
id
BETWEEN
N
AND
M
Range
SUM
() queries:
SELECT
SUM
(K)
FROM
sbtest
WHERE
id
BETWEEN
N
and
M
Range
ORDER
BY
queries:
SELECT
c
FROM
sbtest
WHERE
id
between
N
and
M
ORDER
BY
c
Range
DISTINCT
queries:
SELECT
DISTINCT
c
FROM
sbtest
WHERE
id
BETWEEN
N
and
M
ORDER
BY
c
UPDATEs
on
index
column
:
UPDATE
sbtest
SET
k
=
k
+
1
WHERE
id
=
N
UPDATEs
on
non
-
index
column
:
UPDATE
sbtest
SET
c
=
N
WHERE
id
=
M
DELETE
queries:
DELETE
FROM
sbtest
WHERE
id
=
N
INSERT
queries:
INSERT
INTO
sbtest
VALUES
(...)
3.Non-transcational mode
这个mode与simple mode相似,不同的地方是可以自主选择想要运行的query,这里要注意的是,它与Advanced transcational mode的区别,它在查询请求间不会保护测试表中的数据,所以如果有多次测试的话,需要进行cleanup/prepare操作。
它包含执行的语句如下:
Point queries:
SELECT
pad
FROM
sbtest
WHERE
id
=
N
UPDATEs
on
index
column
:
UPDATE
sbtest
SET
k
=
k
+
1
WHERE
id
=
N
UPDATEs
on
non
-
index
column
:
UPDATE
sbtest
SET
c
=
N
WHERE
id
=
M
DELETE
queries:
DELETE
FROM
sbtest
WHERE
id
=
N
The generated row IDs are
unique
over
each test run, so no row
is
deleted twice.
INSERT
queries:
INSERT
INTO
sbtest (k, c, pad)
VALUES
(N, M, S)
最后一个阶段就是cleanup,这个没什么好讲的。就是清除测试数据。
上面的内容来自手册,但似乎手册的内容已经很长时间不更新了,所以在实际应用中应该查看sysbench的help:
以下是sysbench的oltp相关测试选项:
--oltp-test-mode
=
STRING test type to use {simple
,
complex
,
nontrx
,
sp}
[
complex
]
--oltp-reconnect-mode
=
STRING reconnect mode {session
,
transaction
,
query
,
random}
[
session
]
--oltp-sp-name
=
STRING name of store procedure to call in SP test mode
[]
--oltp-read-only
=
[
on|off
]
generate only 'read' queries (do not modify database)
[
off
]
--oltp-skip-trx
=
[
on|off
]
skip BEGIN/COMMIT statements
[
off
]
--oltp-range-size
=
N range size for range queries
[
100
]
--oltp-point-selects
=
N number of point selects
[
10
]
--oltp-simple-ranges
=
N number of simple ranges
[
1
]
--oltp-sum-ranges
=
N number of sum ranges
[
1
]
--oltp-order-ranges
=
N number of ordered ranges
[
1
]
--oltp-distinct-ranges
=
N number of distinct ranges
[
1
]
--oltp-index-updates
=
N number of index update
[
1
]
--oltp-non-index-updates
=
N number of non-index updates
[
1
]
--oltp-nontrx-mode
=
STRING mode for non-transactional test {select
,
update_key
,
update_nokey
,
insert
,
delete}
[
select
]
--oltp-auto-inc
=
[
on|off
]
whether AUTO_INCREMENT (or equivalent) should be used on id column
[
on
]
--oltp-connect-delay
=
N time in microseconds to sleep after connection to database
[
10000
]
--oltp-user-delay-min
=
N minimum time in microseconds to sleep after each request
[
0
]
--oltp-user-delay-max
=
N maximum time in microseconds to sleep after each request
[
0
]
--oltp-table-name
=
STRING name of test table
[
sbtest
]
--oltp-table-size
=
N number of records in test table
[
10000
]
--oltp-dist-type
=
STRING random numbers distribution {uniform
,
gaussian
,
special}
[
special
]
--oltp-dist-iter
=
N number of iterations used for numbers generation
[
12
]
--oltp-dist-pct
=
N percentage of values to be treated as 'special' (for special distribution)
[
1
]
--oltp-dist-res
=
N percentage of 'special' values to use (for special distribution)
[
75
]
安装sysbench(补充):
sysbench安装有时会很麻烦,如果用rpm包或在ubuntu上用apt-get在线安装会容易许多。不过我倾向与使用tar包安装,特别是使用的数据库大多不是系统提供的版本时。
1.去官站下载tar包(略)
2.安装:
# tar -zxvf sysbench-
0.4.12
.tar.gz
# cd sysbench-
0.4.12
# ./autogen.sh
# ./configure --with-mysql-includes
=
/usr/local/mysql/include --with-mysql-libs
=
/usr/local/mysql/lib
# make && make install
在安装过程中可能会遇到三个问题:
一是在编译时会报libtools错误,解决的方法是在configure之前运行autogen.sh脚本;
二是在运行sysbench时报如下错误:
sysbench: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
遇到此问题需要将libmysqlclient.so.xx文件所在的目录append至/etc/ld.so.conf文件
如果还是不行,可执行export LD_LIBRARY_PATH=/usr/local/mysql/lib
三是报如下错误:
# sysbench --test=oltp --db-driver=mysql --mysql-host=localhost --mysql-socket=/tmp/mysql.sock --mysql-user=root --mysql-table-engine=innodb --oltp-table-size=100000000 prepare
sysbench 0.4.12: multi-threaded system evaluation benchmark
FATAL: unable to connect to MySQL server, aborting...
FATAL: error 1049: Unknown database 'sbtest'
FATAL: failed to connect to database server!
解决方法是加--mysql-db option,告诉sysbench往哪个数据库中装填数据
参考:
http://sysbench.sourceforge.net/docs/
http://www.penglixun.com/tech/system/install_sysbench.html