brew install sysbench
測試sysbench 是否安裝成功html
//執行這條指令 sysbench cpu --cpu-max-prime=10000 run sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2) Running the test with following options: Number of threads: 1 Initializing random number generator from current time Prime numbers limit: 10000 Initializing worker threads... Threads started! CPU speed: events per second: 859.16 General statistics: total time: 10.0008s total number of events: 8594 Latency (ms): min: 1.10 avg: 1.16 max: 24.47 95th percentile: 1.32 sum: 9990.99 Threads fairness: events (avg/stddev): 8594.0000/0.00 execution time (avg/stddev): 9.9910/0.00
sysbench支持如下幾種測試模式:java
一、CPU運算性能 二、磁盤IO性能 三、調度程序性能 四、內存分配及傳輸速度 五、POSIX線程性能 六、數據庫性能(OLTP基準測試) 目前sysbench主要支持 mysql,drizzle,pgsql,oracle 等幾種數據庫。
sysbench cpu --cpu-max-prime=10000 run sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2) Running the test with following options: Number of threads: 1 Initializing random number generator from current time Prime numbers limit: 10000 Initializing worker threads... Threads started! CPU speed: events per second: 859.16 General statistics: total time: 10.0008s total number of events: 8594 Latency (ms): min: 1.10 avg: 1.16 max: 24.47 95th percentile: 1.32 sum: 9990.99 Threads fairness: events (avg/stddev): 8594.0000/0.00 execution time (avg/stddev): 9.9910/0.00
第一步準備階段,生成測試用到的數據文件,生成的數據文件至少要比內存大mysql
//建立測試文件,後續run操做會讀寫這個文件 這裏文件大小爲10G sysbench --test=fileio --file-total-size=10G prepare ARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options. sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2) 128 files, 81920Kb each, 10240Mb total Creating files for the test... Extra file open flags: (none) Creating file test_file.0 Creating file test_file.1 Creating file test_file.2 Creating file test_file.3 Creating file test_file.4 Creating file test_file.5 Creating file test_file.6 Creating file test_file.7 Creating file test_file.8 Creating file test_file.9 Creating file test_file.10 Creating file test_file.11 Creating file test_file.12 Creating file test_file.13 Creating file test_file.14 Creating file test_file.15 Creating file test_file.16 Creating file test_file.17 Creating file test_file.18 Creating file test_file.19 Creating file test_file.20 Creating file test_file.21 Creating file test_file.22 Creating file test_file.23 Creating file test_file.24 Creating file test_file.25 Creating file test_file.26 Creating file test_file.27 Creating file test_file.28 Creating file test_file.29 Creating file test_file.30 Creating file test_file.31 Creating file test_file.32 Creating file test_file.33 Creating file test_file.34 Creating file test_file.35 Creating file test_file.36 Creating file test_file.37 Creating file test_file.38 Creating file test_file.39 Creating file test_file.40 Creating file test_file.41 Creating file test_file.42 Creating file test_file.43 Creating file test_file.44 Creating file test_file.45 Creating file test_file.46 Creating file test_file.47 Creating file test_file.48 Creating file test_file.49 Creating file test_file.50 Creating file test_file.51 Creating file test_file.52 Creating file test_file.53 Creating file test_file.54 Creating file test_file.55 Creating file test_file.56 Creating file test_file.57 Creating file test_file.58 Creating file test_file.59 Creating file test_file.60 Creating file test_file.61 Creating file test_file.62 Creating file test_file.63 Creating file test_file.64 Creating file test_file.65 Creating file test_file.66 Creating file test_file.67 Creating file test_file.68 Creating file test_file.69 Creating file test_file.70 Creating file test_file.71 Creating file test_file.72 Creating file test_file.73 Creating file test_file.74 Creating file test_file.75 Creating file test_file.76 Creating file test_file.77 Creating file test_file.78 Creating file test_file.79 Creating file test_file.80 Creating file test_file.81 Creating file test_file.82 Creating file test_file.83 Creating file test_file.84 Creating file test_file.85 Creating file test_file.86 Creating file test_file.87 Creating file test_file.88 Creating file test_file.89 Creating file test_file.90 Creating file test_file.91 Creating file test_file.92 Creating file test_file.93 Creating file test_file.94 Creating file test_file.95 Creating file test_file.96 Creating file test_file.97 Creating file test_file.98 Creating file test_file.99 Creating file test_file.100 Creating file test_file.101 Creating file test_file.102 Creating file test_file.103 Creating file test_file.104 Creating file test_file.105 Creating file test_file.106 Creating file test_file.107 Creating file test_file.108 Creating file test_file.109 Creating file test_file.110 Creating file test_file.111 Creating file test_file.112 Creating file test_file.113 Creating file test_file.114 Creating file test_file.115 Creating file test_file.116 Creating file test_file.117 Creating file test_file.118 Creating file test_file.119 Creating file test_file.120 Creating file test_file.121 Creating file test_file.122 Creating file test_file.123 Creating file test_file.124 Creating file test_file.125 Creating file test_file.126 Creating file test_file.127 10737418240 bytes written in 31.64 seconds (323.62 MiB/sec).
文件的讀寫模式以下linux
第二步 測試寫入數據git
//執行混合隨機測試//隨機寫入時間爲300秒 sysbench --test=fileio --file-total-size=10G --file-test-mode=rndrw --time=300 --max-requests=0 run WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options. sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2) Running the test with following options: Number of threads: 1 Initializing random number generator from current time Extra file open flags: (none) 128 files, 80MiB each 10GiB total file size Block size 16KiB Number of IO requests: 0 Read/Write ratio for combined random IO test: 1.50 Periodic FSYNC enabled, calling fsync() each 100 requests. Calling fsync() at the end of test, Enabled. Using synchronous I/O mode Doing random r/w test Initializing worker threads... Threads started! File operations: reads/s: 3688.61 writes/s: 2459.07 fsyncs/s: 7869.33 Throughput: read, MiB/s: 57.63 written, MiB/s: 38.42 General statistics: total time: 300.0014s total number of events: 4205035 Latency (ms): min: 0.00 avg: 0.07 max: 153.05 95th percentile: 0.20 sum: 295336.94 Threads fairness: events (avg/stddev): 4205035.0000/0.00 execution time (avg/stddev): 295.3369/0.00
第三步,清除測試文件github
sysbench --test=fileio --file-total-size=10G cleanup
查看一下:sql
/usr/local/Cellar/sysbench/1.0.17/share/sysbench目錄下的lua文件shell
-rwxr-xr-x 1 xxydliuyss staff 1.4K 3 15 20:16 bulk_insert.lua -rw-r--r-- 1 xxydliuyss staff 14K 3 15 20:16 oltp_common.lua -rwxr-xr-x 1 xxydliuyss staff 1.3K 3 15 20:16 oltp_delete.lua -rwxr-xr-x 1 xxydliuyss staff 2.4K 3 15 20:16 oltp_insert.lua -rwxr-xr-x 1 xxydliuyss staff 1.2K 3 15 20:16 oltp_point_select.lua -rwxr-xr-x 1 xxydliuyss staff 1.6K 3 15 20:16 oltp_read_only.lua -rwxr-xr-x 1 xxydliuyss staff 1.8K 3 15 20:16 oltp_read_write.lua -rwxr-xr-x 1 xxydliuyss staff 1.1K 3 15 20:16 oltp_update_index.lua -rwxr-xr-x 1 xxydliuyss staff 1.1K 3 15 20:16 oltp_update_non_index.lua -rwxr-xr-x 1 xxydliuyss staff 1.4K 3 15 20:16 oltp_write_only.lua -rwxr-xr-x 1 xxydliuyss staff 1.9K 3 15 20:16 select_random_points.lua -rwxr-xr-x 1 xxydliuyss staff 2.1K 3 15 20:16 select_random_ranges.lua drwxr-xr-x 5 xxydliuyss staff 160B 8 28 18:53 tests
0.5版本以後就取消了—test=oltp的命令,改用直接調用腳本的方式數據庫
如何查看腳本的參數和語法數據結構
sysbench --test=/usr/local/Cellar/sysbench/1.0.17/share/sysbench/oltp_common.lua help oltp_common.lua options: --auto_inc[=on|off] Use AUTO_INCREMENT column as Primary Key (for MySQL), or its alternatives in other DBMS. When disabled, use client-generated IDs [on] --create_secondary[=on|off] Create a secondary index in addition to the PRIMARY KEY [on] --delete_inserts=N Number of DELETE/INSERT combinations per transaction [1] --distinct_ranges=N Number of SELECT DISTINCT queries per transaction [1] --index_updates=N Number of UPDATE index queries per transaction [1] --mysql_storage_engine=STRING Storage engine, if MySQL is used [innodb] --non_index_updates=N Number of UPDATE non-index queries per transaction [1] --order_ranges=N Number of SELECT ORDER BY queries per transaction [1] --pgsql_variant=STRING Use this PostgreSQL variant when running with the PostgreSQL driver. The only currently supported variant is 'redshift'. When enabled, create_secondary is automatically disabled, and delete_inserts is set to 0 --point_selects=N Number of point SELECT queries per transaction [10] --range_selects[=on|off] Enable/disable all range SELECT queries [on] --range_size=N Range size for range SELECT queries [100] --secondary[=on|off] Use a secondary index in place of the PRIMARY KEY [off] --simple_ranges=N Number of simple range SELECT queries per transaction [1] --skip_trx[=on|off] Don't start explicit transactions and execute all queries in the AUTOCOMMIT mode [off] --sum_ranges=N Number of SELECT SUM() queries per transaction [1] --table_size=N Number of rows per table [10000] --tables=N Number of tables [1]
第一步準備數據庫
mysqladmin create test
第二步 準備測試數據
//準備十萬條測試數據的量 sysbench --test=/usr/local/Cellar/sysbench/1.0.17/share/sysbench/oltp_common.lua --table-size=100000 --mysql-host=k8s-n1 --mysql-port=3307 --mysql-user=root --mysql-password=123456 --mysql-db=test prepare
第三步測試
進行插入性能測試
//插入10萬條數據,8個線程執行,每10秒中報告一次 sysbench --test=/usr/local/Cellar/sysbench/1.0.17/share/sysbench/oltp_insert.lua --table-size=100000 --mysql-host=k8s-n1 --mysql-port=3307 --mysql-user=root --mysql-password=123456 --mysql-db=test --threads=8 --max-requests=0 --report-interval=10 --time=300 run
結果
sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2) Running the test with following options: Number of threads: 8 Report intermediate results every 10 second(s) Initializing random number generator from current time Initializing worker threads... Threads started! [ 10s ] thds: 8 tps: 1239.65 qps: 1239.65 (r/w/o: 0.00/1239.65/0.00) lat (ms,95%): 9.39 err/s: 0.00 reconn/s: 0.00 [ 20s ] thds: 8 tps: 1073.90 qps: 1073.90 (r/w/o: 0.00/1073.90/0.00) lat (ms,95%): 13.70 err/s: 0.00 reconn/s: 0.00 [ 30s ] thds: 8 tps: 1008.58 qps: 1008.58 (r/w/o: 0.00/1008.58/0.00) lat (ms,95%): 13.46 err/s: 0.00 reconn/s: 0.00 [ 40s ] thds: 8 tps: 1039.27 qps: 1039.27 (r/w/o: 0.00/1039.27/0.00) lat (ms,95%): 12.75 err/s: 0.00 reconn/s: 0.00 [ 50s ] thds: 8 tps: 958.69 qps: 958.69 (r/w/o: 0.00/958.69/0.00) lat (ms,95%): 15.55 err/s: 0.00 reconn/s: 0.00 [ 60s ] thds: 8 tps: 954.88 qps: 954.88 (r/w/o: 0.00/954.88/0.00) lat (ms,95%): 14.46 err/s: 0.00 reconn/s: 0.00 [ 70s ] thds: 8 tps: 954.40 qps: 954.40 (r/w/o: 0.00/954.40/0.00) lat (ms,95%): 14.46 err/s: 0.00 reconn/s: 0.00 [ 80s ] thds: 8 tps: 897.28 qps: 897.28 (r/w/o: 0.00/897.28/0.00) lat (ms,95%): 16.41 err/s: 0.00 reconn/s: 0.00 [ 90s ] thds: 8 tps: 840.91 qps: 840.91 (r/w/o: 0.00/840.91/0.00) lat (ms,95%): 16.71 err/s: 0.00 reconn/s: 0.00 [ 100s ] thds: 8 tps: 728.62 qps: 728.62 (r/w/o: 0.00/728.62/0.00) lat (ms,95%): 22.28 err/s: 0.00 reconn/s: 0.00 [ 110s ] thds: 8 tps: 733.59 qps: 733.59 (r/w/o: 0.00/733.59/0.00) lat (ms,95%): 20.00 err/s: 0.00 reconn/s: 0.00
結果解讀
Running the test with following options: Number of threads: 8 Report intermediate results every 10 second(s) Random number generator seed is 0 and will be ignored Threads started! -- 每10秒鐘報告一次測試結果,tps、每秒讀、每秒寫、99%以上的響應時長統計 [ 10s] threads: 8, tps: 1111.51, reads/s: 15568.42, writes/s: 4446.13, response time: 9.95ms (99%) [ 20s] threads: 8, tps: 1121.90, reads/s: 15709.62, writes/s: 4487.80, response time: 9.78ms (99%) [ 30s] threads: 8, tps: 1120.00, reads/s: 15679.10, writes/s: 4480.20, response time: 9.84ms (99%) [ 40s] threads: 8, tps: 1114.20, reads/s: 15599.39, writes/s: 4456.30, response time: 9.90ms (99%) [ 50s] threads: 8, tps: 1114.00, reads/s: 15593.60, writes/s: 4456.70, response time: 9.84ms (99%) [ 60s] threads: 8, tps: 1119.30, reads/s: 15671.60, writes/s: 4476.50, response time: 9.99ms (99%) OLTP test statistics: queries performed: read: 938224 -- 讀總數 write: 268064 -- 寫總數 other: 134032 -- 其餘操做總數(SELECT、INSERT、UPDATE、DELETE以外的操做,例如COMMIT等) total: 1340320 -- 所有總數 transactions: 67016 (1116.83 per sec.) -- 總事務數(每秒事務數) deadlocks: 0 (0.00 per sec.) -- 發生死鎖總數 read/write requests: 1206288 (20103.01 per sec.) -- 讀寫總數(每秒讀寫次數) other operations: 134032 (2233.67 per sec.) -- 其餘操做總數(每秒其餘操做次數) General statistics: -- 一些統計結果 total time: 60.0053s -- 總耗時 total number of events: 67016 -- 共發生多少事務數 total time taken by event execution: 479.8171s -- 全部事務耗時相加(不考慮並行因素) response time: -- 響應時長統計 min: 4.27ms -- 最小耗時 avg: 7.16ms -- 平均耗時 max: 13.80ms -- 最長耗時 approx. 99 percentile: 9.88ms -- 超過99%平均耗時 Threads fairness: events (avg/stddev): 8377.0000/44.33 execution time (avg/stddev): 59.9771/0.00 其餘信息能夠參考這個鏈接: http://www.linuxidc.com/Linux/2017-04/142856.htm
其餘使用參數能夠用help查看。
若是須要更加詳細關於sysbench 能夠查看http://blog.itpub.net/29773961/viewspace-1849676
詳細介紹
更新時間:2018-09-29 16:44:06
DRDS 性能測試使用 SysBench 做爲壓測工具,本文介紹 SysBench 的使用方法。
測試使用的是 SysBench 0.5 版本,安裝方法以下:
git clone https://github.com/akopytov/sysbench.gitgit checkout 0.5yum -y install make automake libtool pkgconfig libaio-develyum -y install mariadb-devel./autogen.sh./configuremake -jmake install
注意:以上是在壓測 ECS 上的安裝方法,若是須要安裝到其餘操做系統,參考 SysBench 官方文檔。
安裝完畢後,全部自帶壓測腳本都在 /usr/local/share/sysbench
目錄下,DRDS 性能測試將使用該目錄下的腳本。除此以外,也能夠在源碼目錄 sysbench/sysbench/tests/db
下找到對應的壓測腳本。
SysBench 經過腳本定義了若干經常使用的壓測模型,如下簡單介紹幾個經常使用模型:
壓測模型 | 描述 |
---|---|
bulk_inert.lua | 批量插入數據 |
insert.lua | 單值插入數據 |
delete.lua | 刪除數據 |
oltp.lua | 混合讀寫測試,讀寫比例14:4 |
select.lua | 簡單的主鍵查詢 |
對 DRDS 進行測試時,對 SysBench 的測試表稍做改造,將其修改成分庫分表的形式,測試表的建表語句以下:
CREATE TABLE `sbtest1` ( `id` int(10) unsigned NOT NULL, `k` int(10) unsigned NOT NULL DEFAULT '0', `c` char(120) NOT NULL DEFAULT '', `pad` char(60) NOT NULL DEFAULT '', KEY `xid` (`id`), KEY `k_1` (`k`)) dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 4
以上建表語句中,分庫分表數可自行調整,該表會在準備數據階段自動生成,無需手動建立。
使用 SysBench 進行壓測,一般分爲三個步驟:
一般僅需準備一次數據,在此數據基礎上測試各類模型便可。
SysBench 中經常使用的參數以下:
--oltp-tables-count=1
:表數量--oltp-table-size=10000000
:每一個表產生的記錄行數--oltp-read-only=off
:是否生成只讀SQL,默認 off,若是設置爲 on,則 oltp 模型不會生成 update, delete, insert 的 SQL 語句--oltp-skip-trx=[on|off]
:省略 BEGIN / COMMIT 語句。默認是 off--rand-init=on
:是否隨機初始化數據,若是不隨機化那麼初始好的數據每行內容除了主鍵不一樣外其餘徹底相同--num-threads=12
: 併發線程數,能夠理解爲模擬的客戶端併發鏈接數--report-interval=10
:表示每 10s 輸出一次性能數據--max-requests=0
:壓力測試產生請求的總數,若是如下面的 max-time 來記,這個值設爲 0 便可--max-time=120
:測試的持續時間--oltp_auto_inc=off
:id 是否爲自增列--oltp_secondary=on
:將 id 設置爲非主鍵防止主鍵衝突--oltp_range_size=5
: 連續取值 5 個,一定走到 5 個分片--mysql_table_options='dbpartition by hash(id) tbpartition by hash(id) tbpartitions 2'
:DRDS 支持拆分表,在建表的時候須要指定拆分方式如下是示例命令,供參考:
準備數據:
sysbench --test='/usr/local/share/sysbench/oltp.lua' --oltp_tables_count=1 --report-interval=10 --oltp-table-size=10000000 --mysql-user=*** --mysql-password=*** --mysql-table-engine=innodb --rand-init=on --mysql-host=**** --mysql-port=*** --mysql-db=*** --max-time=300 --max-requests=0 --oltp_skip_trx=on --oltp_auto_inc=off --oltp_secondary=on --oltp_range_size=5 --mysql_table_options='dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 2' --num-threads=200 prepare
執行測試
sysbench --test='/usr/local/share/sysbench/oltp.lua' --oltp_tables_count=1 --report-interval=10 --oltp-table-size=10000000 --mysql-user=*** --mysql-password=*** --mysql-table-engine=innodb --rand-init=on --mysql-host=**** --mysql-port=*** --mysql-db=*** --max-time=300 --max-requests=0 --oltp_skip_trx=on --oltp_auto_inc=off --oltp_secondary --oltp_range_size=5 --mysql_table_options='dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 2' --num-threads=200 run
清理環境
sysbench --test='/usr/local/share/sysbench/oltp.lua' --oltp_tables_count=1 --report-interval=10 --oltp-table-size=10000000 --mysql-user=*** --mysql-password=*** --mysql-table-engine=innodb --rand-init=on --mysql-host=**** --mysql-port=*** --mysql-db=*** --max-time=300 --max-requests=0 --oltp_skip_trx=on --oltp_auto_inc=off --oltp_secondary --oltp_range_size=5 --mysql_table_options='dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 2' --num-threads=200 cleanup
sysbench自定義lua腳本實現實際的業務邏輯壓力測試
使用sysbench進行mysql的oltp測試,通常的測試在sysbench中在tests/db中提供了一個oltp.lua腳本能夠進行oltp的壓力測試。 但不能徹底模擬本身實際業務的壓力測試,不一樣的業務,數據結構,數據量都是不同的,爲了更接近實際業務的讀寫壓力測試,就得本身寫lua腳本,而後經過sysbench進行壓力測試。 寫這個lua腳本很簡單,只須要會寫lua腳本就能夠了。 寫這個lua腳本很簡單,只須要會寫lua腳本就能夠了。
一、首先收集實際業務的訪問數據庫的sql; 二、準備一臺恢復好的備份庫(從線上導一個) 三、將收集的sql寫在lua腳本里(具體如何寫,後面舉個例子)四、經過sysbench的--test參數和--mysql-db參數進行測試(這裏就不須要prepare了,直接run就行) 四、經過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 ) 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); 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) endend 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
sysbench具體的使用、結果解讀能夠參數下
http://imysql.com/2014/10/17/sysbench-full-user-manual.shtml
別人的demo
pathtest = string.match(test, "(.*/)") or "" dofile(pathtest .. "common.lua") function thread_init(thread_id) set_vars() if (db_driver == "mysql" and mysql_table_engine == "myisam") then begin_query = "LOCK TABLES sbtest WRITE" commit_query = "UNLOCK TABLES" else begin_query = "BEGIN" commit_query = "COMMIT" end end function event(thread_id) local vid1 local vid2 local vid3 local vid4 local vid5 local vk local vxv vid1 = sb_rand_uniform(1,10000) vid2 = sb_rand_uniform(1,10000) vid3 = sb_rand_uniform(1,10000) vid4 = sb_rand_uniform(1,10000) vid5 = sb_rand_uniform(1,10000) vk = sb_rand_uniform(10,10000) vxv = sb_rand_str([[###########-###########-###########-###########-###########]]) rs = db_query("SELECT pad FROM test1 WHERE id=" .. vid1) rs = db_query("SELECT pad FROM test2 WHERE id=" .. vid2) rs = db_query("SELECT pad FROM test3 WHERE id=" .. vid3) db_query(begin_query) rs = db_query("update test4 set k=k-1 where id=" .. vid4) rs = db_query("update test5 set k=k+1 where id=" .. vid5) db_query(commit_query) rs = db_query("insert into test6 (k,xv) values " .. string.format("(%d , '%s')",vk,vxv)); end
1.準備兩個表,
CREATE TABLE `employ` ( `id` bigint(20) NOT NULL COMMENT '員工編號', `name` varchar(48) NOT NULL COMMENT '名字', `departmentId` bigint(10) NOT NULL COMMENT '部門編號', `datasourceId` varchar(64) NOT NULL COMMENT '數據存儲位置', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `employExt` ( `userid` bigint(20) NOT NULL COMMENT '員工編號', `username` varchar(48) NOT NULL COMMENT '名字', `address` varchar(255) NOT NULL COMMENT '地址', `phone` char(11) NOT NULL COMMENT '號碼', `cardId` char(18) NOT NULL COMMENT '身份證號碼', PRIMARY KEY (`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
第二步編寫lua腳本
#!/usr/bin/env sysbench require("oltp_common") function thread_init() if (db_driver == "mysql" and mysql_table_engine == "myisam") then begin_query = "LOCK TABLES sbtest WRITE" commit_query = "UNLOCK TABLES" else begin_query = "BEGIN" commit_query = "COMMIT" end drv = sysbench.sql.driver() con = drv:connect() stmt = {} param = {} for t = 1, sysbench.opt.tables do stmt[t] = {} param[t] = {} end -- This function is a 'callback' defined by individual benchmark scripts prepare_statements() end function prepare_statements() -- We do not use prepared statements here, but oltp_common.sh expects this -- function to be defined end function event(thread_id) local table_name = "employ" local table1_name = "employExt" local employ_name = get_c_value() local departmentId = sb_rand_uniform(10,10000) local datasourceId = "db1" if (sysbench.opt.auto_inc) then i = 0 else -- Convert a uint32_t value to SQL INT i = sysbench.rand.unique() - 2147483648 end con:query(string.format("INSERT INTO %s (id, name, departmentId, datasourceId) VALUES " .. "(%d, %s, '%d', '%s')", table_name, i, employ_name, departmentId, datasourceId)) end