From 7fd5ff7d01ed33f3eddddb1db5b6d028762de3d2 Mon Sep 17 00:00:00 2001 From: Hakan Kuecuekyilmaz Date: Thu, 28 Oct 2010 18:53:45 +0200 Subject: [PATCH 1/2] Added two new tests for MariaDB: * select_random_points * select_random_ranges Both test are intended to benchmark MariaDB's key_cache_segmented feature. --- sysbench/tests/db/select_random_points.lua | 139 ++++++++++++++++++++ sysbench/tests/db/select_random_ranges.lua | 143 +++++++++++++++++++++ 2 files changed, 282 insertions(+) create mode 100644 sysbench/tests/db/select_random_points.lua create mode 100644 sysbench/tests/db/select_random_ranges.lua diff --git a/sysbench/tests/db/select_random_points.lua b/sysbench/tests/db/select_random_points.lua new file mode 100644 index 0000000..2af7c5b --- /dev/null +++ b/sysbench/tests/db/select_random_points.lua @@ -0,0 +1,139 @@ +function prepare() + local query + local i + + set_vars() + + db_connect() + + print("Creating table 'sbtest'...") + + if (db_driver == "mysql") then + query = [[ + CREATE TABLE sbtest ( + 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, + PRIMARY KEY (id) + ) /*! ENGINE = ]] .. mysql_table_engine .. " MAX_ROWS = " .. myisam_max_rows .. " */" + + elseif (db_driver == "oracle") then + query = [[ + CREATE TABLE sbtest ( + id INTEGER NOT NULL, + k INTEGER, + c CHAR(120) DEFAULT '' NOT NULL, + pad CHAR(60 DEFAULT '' NOT NULL, + PRIMARY KEY (id) + ) ]] + + + elseif (db_driver == "pgsql") then + query = [[ + CREATE TABLE sbtest ( + id ]] .. (sb.oltp_auto_inc and "SERIAL") or "" .. [[, + k INTEGER DEFAULT '0' NOT NULL, + c CHAR(120) DEFAULT '' NOT NULL, + pad CHAR(60) DEFAULT '' NOT NULL, + PRIMARY KEY (id) + ) ]] + + elseif (db_driver == "drizzle") then + query = [[ + CREATE TABLE sbtest ( + id INTEGER NOT NULL ]] .. ((oltp_auto_inc and "AUTO_INCREMENT") or "") .. [[, + k INTEGER DEFAULT '0' NOT NULL, + c CHAR(120) DEFAULT '' NOT NULL, + pad CHAR(60) DEFAULT '' NOT NULL, + PRIMARY KEY (id) + ) ]] + + else + print("Unknown database driver: " .. db_driver) + return 1 + end + + db_query(query) + + if (db_driver == "oracle") then + db_query("CREATE SEQUENCE sbtest_seq") + db_query([[CREATE TRIGGER sbtest_trig BEFORE INSERT ON sbtest + FOR EACH ROW BEGIN SELECT sbtest_seq.nextval INTO :new.id FROM DUAL; END;]]) + end + + db_query("CREATE INDEX k on sbtest(k)") + + print("Inserting " .. oltp_table_size .. " records into 'sbtest'") + + if (oltp_auto_inc) then + db_bulk_insert_init("INSERT INTO sbtest(k, c, pad) VALUES") + else + db_bulk_insert_init("INSERT INTO sbtest(id, k, c, pad) VALUES") + end + + for i = 1,oltp_table_size do + if (oltp_auto_inc) then + db_bulk_insert_next("("..i..", ' ', 'qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt')") + else + db_bulk_insert_next("("..i..", "..i..", ' ', 'qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt')") + end + end + + db_bulk_insert_done() + + return 0 +end + +function cleanup() + print("Dropping table 'sbtest'...") + db_query("DROP TABLE sbtest") +end + +function thread_init(thread_id) + set_vars() + + points = "" + for i = 1,random_points do + points = points .. "?, " + end + + -- Get rid of last comma and space. + points = string.sub(points, 1, string.len(points) - 2) + + stmt = db_prepare([[ + SELECT id, k, c, pad + FROM sbtest + WHERE k IN (]] .. points .. [[) + ]]) + + params = {} + for j = 1,random_points do + params[j] = 1 + end + + db_bind_param(stmt, params) +end + +function event(thread_id) + local rs + + for i = 1,random_points do + params[i] = sb_rand(thread_id, oltp_table_size) + end + + rs = db_execute(stmt) + db_store_results(rs) + db_free_results(rs) +end + +function set_vars() + oltp_table_size = oltp_table_size or 10000 + random_points = random_points or 10 + + if (oltp_auto_inc == 'off') then + oltp_auto_inc = false + else + oltp_auto_inc = true + end +end diff --git a/sysbench/tests/db/select_random_ranges.lua b/sysbench/tests/db/select_random_ranges.lua new file mode 100644 index 0000000..8c3da43 --- /dev/null +++ b/sysbench/tests/db/select_random_ranges.lua @@ -0,0 +1,143 @@ +function prepare() + local query + local i + + set_vars() + + db_connect() + + print("Creating table 'sbtest'...") + + if (db_driver == "mysql") then + query = [[ + CREATE TABLE sbtest ( + 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, + PRIMARY KEY (id) + ) /*! ENGINE = ]] .. mysql_table_engine .. " MAX_ROWS = " .. myisam_max_rows .. " */" + + elseif (db_driver == "oracle") then + query = [[ + CREATE TABLE sbtest ( + id INTEGER NOT NULL, + k INTEGER, + c CHAR(120) DEFAULT '' NOT NULL, + pad CHAR(60 DEFAULT '' NOT NULL, + PRIMARY KEY (id) + ) ]] + + + elseif (db_driver == "pgsql") then + query = [[ + CREATE TABLE sbtest ( + id ]] .. (sb.oltp_auto_inc and "SERIAL") or "" .. [[, + k INTEGER DEFAULT '0' NOT NULL, + c CHAR(120) DEFAULT '' NOT NULL, + pad CHAR(60) DEFAULT '' NOT NULL, + PRIMARY KEY (id) + ) ]] + + elseif (db_driver == "drizzle") then + query = [[ + CREATE TABLE sbtest ( + id INTEGER NOT NULL ]] .. ((oltp_auto_inc and "AUTO_INCREMENT") or "") .. [[, + k INTEGER DEFAULT '0' NOT NULL, + c CHAR(120) DEFAULT '' NOT NULL, + pad CHAR(60) DEFAULT '' NOT NULL, + PRIMARY KEY (id) + ) ]] + + else + print("Unknown database driver: " .. db_driver) + return 1 + end + + db_query(query) + + if (db_driver == "oracle") then + db_query("CREATE SEQUENCE sbtest_seq") + db_query([[CREATE TRIGGER sbtest_trig BEFORE INSERT ON sbtest + FOR EACH ROW BEGIN SELECT sbtest_seq.nextval INTO :new.id FROM DUAL; END;]]) + end + + db_query("CREATE INDEX k on sbtest(k)") + + print("Inserting " .. oltp_table_size .. " records into 'sbtest'") + + if (oltp_auto_inc) then + db_bulk_insert_init("INSERT INTO sbtest(k, c, pad) VALUES") + else + db_bulk_insert_init("INSERT INTO sbtest(id, k, c, pad) VALUES") + end + + for i = 1,oltp_table_size do + if (oltp_auto_inc) then + db_bulk_insert_next("("..i..", ' ', 'qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt')") + else + db_bulk_insert_next("("..i..", "..i..", ' ', 'qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt')") + end + end + + db_bulk_insert_done() + + return 0 +end + +function cleanup() + print("Dropping table 'sbtest'...") + db_query("DROP TABLE sbtest") +end + +function thread_init(thread_id) + set_vars() + + ranges = "" + for i = 1,number_of_ranges do + ranges = ranges .. "k BETWEEN ? AND ? OR " + end + + -- Get rid of last OR and space. + ranges = string.sub(ranges, 1, string.len(ranges) - 3) + + stmt = db_prepare([[ + SELECT count(k) + FROM sbtest + WHERE ]] .. ranges .. [[ + ]]) + + params = {} + for j = 1,number_of_ranges do + params[j] = 1 + params[j + 1] = 1 + end + + db_bind_param(stmt, params) + +end + +function event(thread_id) + local rs + + for i = 1,random_points do + params[i] = sb_rand(thread_id, oltp_table_size) + params[i + 1] = params[i] + delta + end + + rs = db_execute(stmt) + db_store_results(rs) + db_free_results(rs) +end + +function set_vars() + oltp_table_size = oltp_table_size or 10000 + number_of_ranges = number_of_ranges or 10 + delta = random_ranges_delta or 5 + + if (oltp_auto_inc == 'off') then + oltp_auto_inc = false + else + oltp_auto_inc = true + end +end From cea90cbc87f22ab76565275969bb0f28d736f4aa Mon Sep 17 00:00:00 2001 From: Hakan Kuecuekyilmaz Date: Fri, 29 Oct 2010 04:15:54 +0200 Subject: [PATCH 2/2] Review changes. Prevent overlapping of range queries by partitioning the whole into num_threads segments and then make each thread work with its own segment. --- sysbench/tests/db/select_random_points.lua | 12 ++++++++++-- sysbench/tests/db/select_random_ranges.lua | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/sysbench/tests/db/select_random_points.lua b/sysbench/tests/db/select_random_points.lua index 2af7c5b..1d9fc01 100644 --- a/sysbench/tests/db/select_random_points.lua +++ b/sysbench/tests/db/select_random_points.lua @@ -1,3 +1,9 @@ +-- This test is designed for testing MariaDB's key_cache_segments for MyISAM, +-- and should work with other storage engines as well. +-- +-- For details about key_cache_segments please refer to: +-- http://kb.askmonty.org/v/segmented-key-cache +-- function prepare() local query local i @@ -118,8 +124,10 @@ end function event(thread_id) local rs + -- To prevent overlapping of our range queries we need to partition the whole table + -- into num_threads segments and then make each thread work with its own segment. for i = 1,random_points do - params[i] = sb_rand(thread_id, oltp_table_size) + params[i] = sb_rand(oltp_table_size / num_threads * thread_id, oltp_table_size / num_threads * (thread_id + 1)) end rs = db_execute(stmt) @@ -130,7 +138,7 @@ end function set_vars() oltp_table_size = oltp_table_size or 10000 random_points = random_points or 10 - + if (oltp_auto_inc == 'off') then oltp_auto_inc = false else diff --git a/sysbench/tests/db/select_random_ranges.lua b/sysbench/tests/db/select_random_ranges.lua index 8c3da43..bd23536 100644 --- a/sysbench/tests/db/select_random_ranges.lua +++ b/sysbench/tests/db/select_random_ranges.lua @@ -1,3 +1,9 @@ +-- This test is designed for testing MariaDB's key_cache_segments for MyISAM, +-- and should work with other storage engines as well. +-- +-- For details about key_cache_segments please refer to: +-- http://kb.askmonty.org/v/segmented-key-cache +-- function prepare() local query local i @@ -108,9 +114,8 @@ function thread_init(thread_id) ]]) params = {} - for j = 1,number_of_ranges do + for j = 1,number_of_ranges * 2 do params[j] = 1 - params[j + 1] = 1 end db_bind_param(stmt, params) @@ -120,8 +125,10 @@ end function event(thread_id) local rs - for i = 1,random_points do - params[i] = sb_rand(thread_id, oltp_table_size) + -- To prevent overlapping of our range queries we need to partition the whole table + -- into num_threads segments and then make each thread work with its own segment. + for i = 1,number_of_ranges * 2,2 do + params[i] = sb_rand(oltp_table_size / num_threads * thread_id, oltp_table_size / num_threads * (thread_id + 1)) params[i + 1] = params[i] + delta end @@ -134,7 +141,7 @@ function set_vars() oltp_table_size = oltp_table_size or 10000 number_of_ranges = number_of_ranges or 10 delta = random_ranges_delta or 5 - + if (oltp_auto_inc == 'off') then oltp_auto_inc = false else