1、数据库基准测试OLTP,在sysbench 0.5版本中,可使用了lua脚本进行测试,也可以根据业务需求自定义lua脚本进行测试。
2、在测试之前先查看oltp.lua脚本
-
dofile(pathtest .. "common.lua")
从oltp.lua脚本看,首先就要去执行common.lua脚本。
3、common.lua脚本
-
function create_insert(table_id)
local index_name
local i
local j
local query
if (oltp_secondary) then
index_name = "KEY xid"
else
index_name = "PRIMARY KEY"
end
i = table_id
print("Creating table 'sbtest" .. i .. "'...")
if ((db_driver == "mysql") or (db_driver == "attachsql")) then
query = [[
-
if ((db_driver == "mysql") or (db_driver == "attachsql")) then
-
query = [[
-
CREATE TABLE sbtest]] .. i .. [[ (
-
id INTEGER UNSIGNED NOT NULL ]] ..
-
((oltp_auto_inc and "AUTO_INCREMENT") or "") .. [[,
-
k INTEGER UNSIGNED DEFAULT '0' NOT NULL,
-
c CHAR(120) DEFAULT '' NOT NULL,
-
pad CHAR(60) DEFAULT '' NOT NULL,
-
]] .. index_name .. [[ (id)
-
) /*! ENGINE = ]] .. mysql_table_engine ..
-
" MAX_ROWS = " .. myisam_max_rows .. " */ " ..
-
(mysql_table_options or "")
我的测试数据库是MySQL,这里就查看MySQL的脚本。
1、创建表sbtest;
2、列id是自增长列,且id列定义为主键;
3、可根据mysql_table_engine自定义存储引擎;
4、可设定myisam_max_rows的最大行数;
-
db_query("CREATE INDEX k_" .. i .. " on sbtest" .. i .. "(k)")
-
-
print("Inserting " .. oltp_table_size .. " records into 'sbtest" .. i .. "'")
-
-
if (oltp_auto_inc) then
-
db_bulk_insert_init("INSERT INTO sbtest" .. i .. "(k, c, pad) VALUES")
-
else
-
db_bulk_insert_init("INSERT INTO sbtest" .. i .. "(id, k, c, pad) VALUES")
-
end
-
-
local c_val
-
local pad_val
-
-
-
for j = 1,oltp_table_size do
-
-
c_val = sb_rand_str([[
-
###########-###########-###########-###########-###########-###########-###########-###########-###########-###########]])
-
pad_val = sb_rand_str([[
-
###########-###########-###########-###########-###########]])
-
-
if (oltp_auto_inc) then
-
db_bulk_insert_next("(" .. sb_rand(1, oltp_table_size) .. ", '".. c_val .."', '" .. pad_val .. "')")
-
else
-
db_bulk_insert_next("("..j.."," .. sb_rand(1, oltp_table_size) .. ",'".. c_val .."', '" .. pad_val .. "' )")
-
end
-
end
-
-
db_bulk_insert_done()
-
-
-
end
5、在k列上创建索引,且值为oltp_table_size定义的随机值
6、列c和pad为字符型随机值
-
function prepare()
-
local query
-
local i
-
local j
-
-
set_vars()
-
-
db_connect()
-
-
-
for i = 1,oltp_tables_count do
-
create_insert(i)
-
end
-
-
return 0
-
end
-
-
function cleanup()
-
local i
-
-
set_vars()
-
-
for i = 1,oltp_tables_count do
-
print("Dropping table 'sbtest" .. i .. "'...")
-
db_query("DROP TABLE sbtest".. i )
-
end
-
end
7、定义prepare过程中,需设定表oltp_tables_count的数值,默认为1
8、定义cleanup的过程
-
function set_vars()
-
oltp_table_size = oltp_table_size or 10000
-
oltp_range_size = oltp_range_size or 100
-
oltp_tables_count = oltp_tables_count or 1
-
oltp_point_selects = oltp_point_selects or 10
-
oltp_simple_ranges = oltp_simple_ranges or 1
-
oltp_sum_ranges = oltp_sum_ranges or 1
-
oltp_order_ranges = oltp_order_ranges or 1
-
oltp_distinct_ranges = oltp_distinct_ranges or 1
-
oltp_index_updates = oltp_index_updates or 1
-
oltp_non_index_updates = oltp_non_index_updates or 1
-
-
if (oltp_auto_inc == 'off') then
-
oltp_auto_inc = false
-
else
-
oltp_auto_inc = true
-
end
-
-
if (oltp_read_only == 'on') then
-
oltp_read_only = true
-
else
-
oltp_read_only = false
-
end
-
-
if (oltp_skip_trx == 'on') then
-
oltp_skip_trx = true
-
else
-
oltp_skip_trx = false
-
end
-
-
end
10、这里是在创建表时设置的参数值,可根据需求自定义。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12219480/viewspace-1758335/
,如需转载,请注明出处,否则将追究法律责任。