| 1 |
#!/usr/bin/env sysbench |
| 2 |
|
| 3 |
|
| 4 |
if sysbench.cmdline.command == nil then |
| 5 |
error("Command is required. Supported commands: prepare, run, " .. |
| 6 |
"cleanup, help") |
| 7 |
end |
| 8 |
|
| 9 |
-- Command line options |
| 10 |
sysbench.cmdline.options = { |
| 11 |
table_size = |
| 12 |
{"Rows per table", 10000}, |
| 13 |
tables = |
| 14 |
{"Number of tables", 1}, |
| 15 |
primary_keys = |
| 16 |
{ "Number of primary keys in table", 1 }, |
| 17 |
blob_length = |
| 18 |
{"Length of blob column (this also changes column c to blob)", 0}, |
| 19 |
table_nologging = |
| 20 |
{"Use NOLOGGING when creating table", ""}, |
| 21 |
|
| 22 |
-- Options for being compatible with oltp_ scripts |
| 23 |
mysql_storage_engine = |
| 24 |
{"Storage engine used", "ndbcluster"}, |
| 25 |
auto_inc = |
| 26 |
{"Use AUTO_INCREMENT as PRIMARY KEY, otherwise generate id.", true}, |
| 27 |
secondary = |
| 28 |
{"Use a secondary index in place of the PRIMARY KEY", false}, |
| 29 |
create_secondary = |
| 30 |
{"Create a secondary index in addition to the PRIMARY KEY", true}, |
| 31 |
} |
| 32 |
|
| 33 |
local c_len = 4500 -- 4,5kB by default |
| 34 |
|
| 35 |
function create_table(drv, con, table_num) |
| 36 |
local id_def |
| 37 |
if sysbench.opt.auto_inc then |
| 38 |
id_def = "INTEGER NOT NULL AUTO_INCREMENT" |
| 39 |
else |
| 40 |
id_def = "INTEGER NOT NULL" |
| 41 |
end |
| 42 |
|
| 43 |
local c_def = "varchar(6000)" |
| 44 |
if sysbench.opt.blob_length > 0 then |
| 45 |
if sysbench.opt.blob_length > 65535 then |
| 46 |
c_def = "longblob" |
| 47 |
else |
| 48 |
c_def = "blob" |
| 49 |
end |
| 50 |
c_len = sysbench.opt.blob_length |
| 51 |
end |
| 52 |
|
| 53 |
local tab_comment = ""; |
| 54 |
if sysbench.opt.nologging then |
| 55 |
tab_comment = "COMMENT='NDB_TABLE=NOLOGGING=1'" |
| 56 |
end |
| 57 |
|
| 58 |
print(string.format("Creating table 'crunch%d'...", table_num)) |
| 59 |
|
| 60 |
local query = string.format([[ |
| 61 |
CREATE TABLE crunch%d( |
| 62 |
id %s, |
| 63 |
k INTEGER DEFAULT '0' NOT NULL, |
| 64 |
c %s NOT NULL, |
| 65 |
pad CHAR(60) DEFAULT '' NOT NULL, |
| 66 |
PRIMARY KEY (id) |
| 67 |
) engine %s %s]], |
| 68 |
table_num, id_def, c_def, sysbench.opt.mysql_storage_engine, tab_comment) |
| 69 |
|
| 70 |
con:query(query) |
| 71 |
|
| 72 |
if (sysbench.opt.table_size > 0) then |
| 73 |
print(string.format("Inserting %d records into 'crunch%d'", |
| 74 |
sysbench.opt.table_size, table_num)) |
| 75 |
end |
| 76 |
|
| 77 |
if sysbench.opt.auto_inc then |
| 78 |
query = "INSERT INTO crunch" .. table_num .. "(k, c, pad) VALUES" |
| 79 |
else |
| 80 |
query = "INSERT INTO crunch" .. table_num .. "(id, k, c, pad) VALUES" |
| 81 |
end |
| 82 |
|
| 83 |
con:bulk_insert_init(query) |
| 84 |
|
| 85 |
local c_val |
| 86 |
local pad_val |
| 87 |
|
| 88 |
local pad_value_template = "###########-###########-###########-" .. |
| 89 |
"###########-###########" |
| 90 |
|
| 91 |
for i = 1, sysbench.opt.table_size do |
| 92 |
|
| 93 |
c_val = sysbench.rand.string(string.rep("@", math.min(c_len, 1000))); |
| 94 |
pad_val = sysbench.rand.string(pad_value_template) |
| 95 |
|
| 96 |
if (sysbench.opt.auto_inc) then |
| 97 |
query = string.format("(%d, '%s', '%s')", |
| 98 |
sysbench.rand.default(1, sysbench.opt.table_size), |
| 99 |
c_val, pad_val) |
| 100 |
else |
| 101 |
query = string.format("(%d, %d, '%s', '%s')", |
| 102 |
i, |
| 103 |
sysbench.rand.default(1, sysbench.opt.table_size), |
| 104 |
c_val, pad_val) |
| 105 |
end |
| 106 |
|
| 107 |
con:bulk_insert_next(query) |
| 108 |
end |
| 109 |
|
| 110 |
con:bulk_insert_done() |
| 111 |
|
| 112 |
if sysbench.opt.create_secondary then |
| 113 |
print(string.format("Creating a secondary index on 'crunch%d'...", |
| 114 |
table_num)) |
| 115 |
con:query(string.format("CREATE INDEX k_%d ON crunch%d(k)", |
| 116 |
table_num, table_num)) |
| 117 |
end |
| 118 |
end |
| 119 |
|
| 120 |
-- Prepare the dataset. This command supports parallel execution, i.e. will |
| 121 |
-- benefit from executing with --threads > 1 as long as --tables > 1 |
| 122 |
function cmd_prepare() |
| 123 |
local drv = sysbench.sql.driver() |
| 124 |
local con = drv:connect() |
| 125 |
|
| 126 |
for i = sysbench.tid % sysbench.opt.threads + 1, sysbench.opt.tables, |
| 127 |
sysbench.opt.threads do |
| 128 |
create_table(drv, con, i) |
| 129 |
end |
| 130 |
end |
| 131 |
|
| 132 |
function cleanup() |
| 133 |
local drv = sysbench.sql.driver() |
| 134 |
local con = drv:connect() |
| 135 |
|
| 136 |
for i = 1, sysbench.opt.tables do |
| 137 |
print(string.format("Dropping table 'crunch%d'...", i)) |
| 138 |
con:query("DROP TABLE IF EXISTS crunch" .. i ) |
| 139 |
end |
| 140 |
end |
| 141 |
|
| 142 |
sysbench.cmdline.commands = { |
| 143 |
prepare = {cmd_prepare, sysbench.cmdline.PARALLEL_COMMAND}, |
| 144 |
} |
| 145 |
|
| 146 |
function thread_init() |
| 147 |
-- init MySQL driver and connect |
| 148 |
drv = sysbench.sql.driver() |
| 149 |
con = drv:connect() |
| 150 |
end |
| 151 |
|
| 152 |
function thread_done() |
| 153 |
con:disconnect() |
| 154 |
end |
| 155 |
|
| 156 |
function event() |
| 157 |
local table_num = sysbench.rand.uniform(1, sysbench.opt.tables) |
| 158 |
local id = sysbench.rand.default(1, sysbench.opt.table_size) |
| 159 |
|
| 160 |
local c_data = sysbench.rand.string(string.rep("@", c_len)); |
| 161 |
local q = string.format("UPDATE crunch%d SET c='%s' WHERE id=%d", |
| 162 |
table_num, c_data, id ) |
| 163 |
con:query(q) |
| 164 |
end |