【Mysql】sysbench基準測試工具

小亮520cl發表於2016-01-25
對於innodb引擎的資料庫應用來說,使用者可能更關心磁碟和oltp的效能,因此主要測試fileio和oltp這兩個專案

測試fileio
[root@iZ257l556beZ ~]# sysbench --test=fileio help
sysbench 0.4.12:  multi-threaded system evaluation benchmark


fileio options:
  --file-num=N                  number of files to create [128]
  --file-block-size=N           block size to use in all IO operations [16384]
  --file-total-size=SIZE        total size of files to create [2G]
  --file-test-mode=STRING       test mode {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw}
  --file-io-mode=STRING         file operations mode {sync,async,fastmmap,slowmmap} [sync]
  --file-async-backlog=N        number of asynchronous operatons to queue per thread [128]
  --file-extra-flags=STRING     additional flags to use on opening files {sync,dsync,direct} []
  --file-fsync-freq=N           do fsync() after this number of requests (0 - don't use fsync()) [100]
  --file-fsync-all=[on|off]     do fsync() after each write operation [off]
  --file-fsync-end=[on|off]     do fsync() at the end of test [on]
  --file-fsync-mode=STRING      which method to use for synchronization {fsync, fdatasync} [fsync]
  --file-merged-requests=N      merge at most this number of IO requests if possible (0 - don't merge) [0]
  --file-rw-ratio=N             reads/writes ratio for combined test [1.5]


其中測試模式裡分為 連續/順序寫(seqwr)、連續改寫(seqrewr)、連續讀(seqrd)、隨機讀(rndrd)、隨機寫(rndwr)、隨機讀寫(rndrw) 六種 。
以man手冊裡的測試示例為例,一次測試需要三步,如:
  1. $ sysbench --num-threads=16 --test=fileio --file-total-size=3G --file-test-mode=rndrw prepare --準備
  2. $ sysbench --num-threads=16 --test=fileio --file-total-size=3G --file-test-mode=rndrw run --測試
  3. $ sysbench --num-threads=16 --test=fileio --file-total-size=3G --file-test-mode=rndrw cleanup --清除test檔案

執行第一步生成test檔案
sysbench --num-threads=16 --test=fileio --file-total-size=3G --file-test-mode=rndrw prepare


執行第二步操作測試的結果為:
  1. $ sysbench --num-threads=16 --test=fileio --file-total-size=3G --file-test-mode=rndrw run
  2. sysbench 0.4.12: multi-threaded system evaluation benchmark
  3. Running the test with following options:
  4. Number of threads: 16
  5. Extra file open flags: 0
  6. 128 files, 24Mb each
  7. 3Gb total file size
  8. Block size 16Kb
  9. Number of random requests for random IO: 10000    --可透過max-requests引數設定,最大請求數
  10. Read/Write ratio for combined random IO test: 1.50
  11. Periodic FSYNC enabled, calling fsync() each 100 requests.
  12. Calling fsync() at the end of test, Enabled.
  13. Using synchronous I/O mode
  14. Doing random r/w test
  15. Threads
  16. Done.
  17. Operations performed: 6000 Read, 4002 Write, 12800 Other = 22802 Total
  18. Read 93.75Mb Written 62.531Mb Total transferred 156.28Mb (2.8256Mb/sec)  ---隨機寫效能
  19.   180.84 Requests/sec executed   --隨機寫的iops
  20. Test execution summary:
  21.     total time: 55.3085s
  22.     total number of events: 10002
  23.     total time taken by event execution: 306.1095
  24.     per-request statistics:
  25.          min: 0.00ms
  26.          avg: 30.60ms
  27.          max: 508.92ms
  28.          approx. 95 percentile: 162.07ms
  29. Threads fairness:
  30.     events (avg/stddev): 625.1250/66.54
  31.     execution time (avg/stddev): 19.1318/1.07

注:實際測試中最好幾種模式的都逐一進行測試比對 。


測試基準報告(與上面無關)

執行緒數 測試模式 最大請求數 檔案大小 傳輸速度 總執行時間
最小 最大 平均
16 rndrw 20000 10G 2.8256Mb/sec 200.3292s 0.00ms 684.51ms 55.36ms
16 rndwr 20000 10G 2.2149Mb/sec 352.7680s 0.00ms 684.52ms 55.37ms
16 seqwr 20000 10G 2.3958Mb/sec 200.3293s 0.00ms 684.53ms 55.38ms
16 seqrd 20000 10G 2.1065Mb/sec 125.8323s 0.00ms 684.54ms 55.39ms

備註:IO測試中,最好讓執行緒、最大請求數、檔案大小三個值的固定為合適的值,透過改變不同的測試模式進行效能比對 。




測試資料庫的OLTP效能

  1. [root@iZ257l556beZ soft]# sysbench --test=oltp help
  2. sysbench 0.4.12: multi-threaded system evaluation benchmark

  3. oltp options:
  4.   --oltp-test-mode=STRING test type to use {simple,complex,nontrx,sp} [complex]
  5.   --oltp-reconnect-mode=STRING reconnect mode {session,transaction,query,random} [session]
  6.   --oltp-sp-name=STRING name of store procedure to call in SP test mode []
  7.   --oltp-read-only=[on|off] generate only 'read' queries (do not modify database) [off]
  8.   --oltp-skip-trx=[on|off] skip BEGIN/COMMIT statements [off]
  9.   --oltp-range-size=N range size for range queries [100]
  10.   --oltp-point-selects=N number of point selects [10]
  11.   --oltp-simple-ranges=N number of simple ranges [1]
  12.   --oltp-sum-ranges=N number of sum ranges [1]
  13.   --oltp-order-ranges=N number of ordered ranges [1]
  14.   --oltp-distinct-ranges=N number of distinct ranges [1]
  15.   --oltp-index-updates=N number of index update [1]
  16.   --oltp-non-index-updates=N number of non-index updates [1]
  17.   --oltp-nontrx-mode=STRING mode for non-transactional test {select, update_key, update_nokey, insert, delete} [select]
  18.   --oltp-auto-inc=[on|off] whether AUTO_INCREMENT (or equivalent) should be used on id column [on]
  19.   --oltp-connect-delay=N time in microseconds to sleep after connection to database [10000]
  20.   --oltp-user-delay-min=N minimum time in microseconds to sleep after each request [0]
  21.   --oltp-user-delay-max=N maximum time in microseconds to sleep after each request [0]
  22.   --oltp-table-name=STRING name of test table [sbtest]
  23.   --oltp-table-size=N number of records in test table [10000]
  24.   --oltp-dist-type=STRING random numbers distribution {uniform,gaussian,special} [special]
  25.   --oltp-dist-iter=N number of iterations used for numbers generation [12]
  26.   --oltp-dist-pct=N percentage of values to be treated as 'special' (for special distribution) [1]
  27.   --oltp-dist-res=N percentage of 'special' values to use (for special distribution) [75]

  28. General database options:

  29.   --db-driver=STRING specifies database driver to use ('help' to get list of available drivers)
  30.   --db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]


  31. Compiled-in database drivers:
  32.   mysql - MySQL driver
  33.   pgsql - PostgreSQL driver

  34. mysql options:
  35.   --mysql-host=[LIST,...] MySQL server host [localhost]
  36.   --mysql-port=N MySQL server port [3306]
  37.   --mysql-socket=STRING MySQL socket
  38.   --mysql-user=STRING MySQL user [sbtest]
  39.   --mysql-password=STRING MySQL password []
  40.   --mysql-db=STRING MySQL database name [sbtest]
  41.   --mysql-table-engine=STRING storage engine to use for the test table {myisam,innodb,bdb,heap,ndbcluster,federated} [innodb]
  42.   --mysql-engine-trx=STRING whether storage engine used is transactional or not {yes,no,auto} [auto]
  43.   --mysql-ssl=[on|off] use SSL connections, if available in the client library [off]
  44.   --myisam-max-rows=N max-rows parameter for MyISAM tables [1000000]
  45.   --mysql-create-options=STRING additional options passed to CREATE TABLE []

  46. pgsql options:
  47.   --pgsql-host=STRING PostgreSQL server host [localhost]
  48.   --pgsql-port=N PostgreSQL server port [5432]
  49.   --pgsql-user=STRING PostgreSQL user [sbtest]
  50.   --pgsql-password=STRING PostgreSQL password []
  51.   --pgsql-db=STRING PostgreSQL database name [sbtest]

建立模擬資料
  1. [root@iZ257l556beZ ~]# sysbench --test=oltp --oltp-table-size=1000000 --db-driver=mysql --mysql-socket=/home/data/mydata/3307/mysql.sock --mysql-user=root --mysql-password=ESBecs00 --mysql-port=3307 --mysql-host=127.0.0.1 --mysql-db=pt   --oltp-read-only=off --oltp-dist-type=uniform  --num-threads=10  prepare   --控制併發與只讀

  2. sysbench 0.4.12: multi-threaded system evaluation benchmark


  3. Creating table 'sbtest'...
  4. Creating 500000 records in table 'sbtest'...

run測試
  1. [root@iZ257l556beZ ~]# sysbench --test=oltp --oltp-table-size=500000 --db-driver=mysql --mysql-socket=/home/data/mydata/3307/mysql.sock --mysql-user=root --mysql-password=ESBecs00 --mysql-port=3307 --mysql-host=127.0.0.1 --mysql-db=pt   --oltp-read-only=off --oltp-dist-type=uniform  --num-threads=10  run>res

檢視測試結果
  1. [root@iZ257l556beZ ~]# more res
  2. sysbench 0.4.12: multi-threaded system evaluation benchmark

  3. Running the test with following options:
  4. Number of threads: 1

  5. Doing OLTP test.
  6. Running mixed OLTP test
  7. Using Special distribution (12 iterations, 1 pct of values are returned in 75 pct cases)
  8. Using "BEGIN" for starting transactions
  9. Using auto_inc on the id column
  10. Maximum number of requests for OLTP test is limited to 10000
  11. Threads
  12. Done.

  13. OLTP test statistics:
  14.     queries performed:
  15.         read: 140000
  16.         write: 50000
  17.         other: 20000
  18.         total: 210000
  19.     transactions: 10000 (120.50 per sec.)     ----tps      10000/82.9887=120.5
  20.     deadlocks: 0 (0.00 per sec.)
  21.     read/write requests: 190000 (2289.47 per sec.)   ----qps       190000/82.9887
  22.     other operations: 20000 (241.00 per sec.)

  23. Test execution summary:
  24.     total time: 82.9887s
  25.     total number of events: 10000
  26.     total time taken by event execution: 82.9137
  27.     per-request statistics:
  28.          min: 5.98ms
  29.          avg: 8.29ms
  30.          max: 86.63ms
  31.          approx. 95 percentile: 10.78ms

  32. Threads fairness:
  33.     events (avg/stddev): 10000.0000/0.00
  34.     execution time (avg/stddev): 82.9137/0.00



sysbench5.0改進版本
  1. sysbench 0.5相比0.4版本的主要變化是,oltp測試結合了lua指令碼,不需要修改原始碼,透過自定義lua指令碼就可以實現不同業務型別的測試。同時0.5相比0.4需要消耗更多的cpu資源。
    1、檢視幫助
    sysbench --help
    sysbench --test=fileio help 

    可以檢視 fileio、 cpu、 memory、 threads、 mutex的詳細引數,0.4版本還可以檢視oltp的引數
    這裡和0.4版本不同,這裡--test不再有
    oltp - OLTP test這個專案,而是換成了對應的lua指令碼路徑(0.5版本rpm包安裝的情況下,預設lua指令碼放在/usr/share/doc/sysbench/tests/db下)

    可以檢視:
    [root@VM-MYSQL-TEST-LiDan db]# ll /usr/share/doc/sysbench/tests/db
    total 44
    -rw-r--r-- 1 root root 3585 Sep  7  2012 common.lua
    -rw-r--r-- 1 root root  340 Sep  7  2012 delete.lua
    -rw-r--r-- 1 root root  830 Sep  7  2012 insert.lua
    -rw-r--r-- 1 root root 2925 Sep  7  2012 oltp.lua
    -rw-r--r-- 1 root root  342 Sep  7  2012 oltp_simple.lua
    -rw-r--r-- 1 root root  425 Sep  7  2012 parallel_prepare.lua
    -rw-r--r-- 1 root root  343 Sep  7  2012 select.lua
    -rw-r--r-- 1 root root 3964 Sep  7  2012 select_random_points.lua
    -rw-r--r-- 1 root root 4066 Sep  7  2012 select_random_ranges.lua
    -rw-r--r-- 1 root root  343 Sep  7  2012 update_index.lua
    -rw-r--r-- 1 root root  552 Sep  7  2012 update_non_index.lua
    如果對lua熟悉也可以自行修改lua指令碼來滿足自己的測試需求。
  2. 引數說明:
    --oltp-table-size:指定表的大小,即表的行數
    --oltp-tables-count:表的個數
    --mysql-table-engine:指定儲存引擎,如myisam,innodb,heap,ndbcluster,bdb,maria,falcon,pbxt
    --mysql-db:指定在哪個資料庫建立測試表,預設為sbtest庫,需要提前建立好
    --test:指定Lua指令碼,引數選項大部分同老版本的--test=oltp help
    --db-driver:指定驅動,預設為Mysql
    --myisam-max-rows:指定Myisam表的MAX_ROWS選項
    --oltp-secondary:測試表將使用二級索引KEY xid (ID) 替代 PRIMARY KEY (ID),innodb引擎內部為每個表 建立唯一6位元組的主鍵索引 
    --oltp-auto-inc:設定id列為auto-incremental,值為on或off,預設為on
    --report-interval=10:每隔多久列印一次統計資訊,單位秒,0.5版本新增  
    --rand-init=on:是否隨機初始化資料,如果不隨機化那麼初始好的資料每行內容除了主鍵不同外其他完全相同。  
     --rand-type=special:資料分佈模式,special表示存在熱點資料,uniform表示非熱點資料模式
  3.  --mysql-table-engine=xxx:表的儲存引擎型別,innodb、myisam、tokudb這些都可以


  4. 1準備資料:
  5. [root@localhost /root ]# sysbench --test=/usr/share/doc/sysbench/tests/db/oltp.lua --oltp_tables_count=5 --report-interval=5  --oltp-table-size=1000000 --num-threads=50 --mysql-user=root --mysql-password=ESBecs00 --mysql-table-engine=INNODB --rand-init=on --mysql-host=127.0.0.1 --mysql-port=3308 --mysql-db=test prepare  
    sysbench 0.5:  multi-threaded system evaluation benchmark


    Creating table 'sbtest1'...
    Inserting 1000000 records into 'sbtest1'
    Creating table 'sbtest2'...
    Inserting 1000000 records into 'sbtest2'
    Creating table 'sbtest3'...
    Inserting 1000000 records into 'sbtest3'
    Creating table 'sbtest4'...
    Inserting 1000000 records into 'sbtest4'

    2 壓測
  6. [root@localhost /root ]# sysbench --test=/usr/share/doc/sysbench/tests/db/insert.lua --oltp_tables_count=4 --report-interval=5  --oltp-table-size=1000000 --num-threads=50 --mysql-user=root --mysql-password=ESBecs00 --mysql-table-engine=INNODB --rand-init=on --mysql-host=127.0.0.1 --mysql-port=3308 --mysql-db=test --oltp-read-only=off run    --和上面一樣,控制讀寫與併發執行緒數
  3 清理環境
 sysbench --test=/usr/share/doc/sysbench/tests/db/oltp.lua --oltp_tables_count=5 --report-interval=5  --oltp-table-size=1000000 --num-threads=50 --mysql-user=root --mysql-password=ESBecs00 --mysql-table-engine=INNODB --rand-init=on --mysql-host=127.0.0.1 --mysql-port=3308 --mysql-db=test cleanup


參考文件
http://my.oschina.net/anthonyyau/blog/290030   ---sysbench
  ----好的一篇測試mysql的效能的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-1981855/,如需轉載,請註明出處,否則將追究法律責任。

相關文章