一、工具說明
sysbench是一個開源的、模塊化的、跨平台的多線程性能測試工具,可以用來進行CPU、內存、磁盤I/O、線程、數據庫的性能測試。目前支持的數據庫有MySQL、Oracle和PostgreSQL。以下操作都將以支持MySQL數據庫為例進行。
sysbench的測試可運用:
CPU性能
磁盤IO性能
調動程序性能
內存分配及傳輸速度
POSIX線程性能
數據庫性能
安裝說明
https://link.zhihu.com/?target=https%3A//www.cnblogs.com/pdlife/p/6698957.html
使用說明
測試fileio命令幫助
sysbench --test=fileio help
參數詳解:
--file-num=N 代表生成測試文件的數量,默認為128。
--file-block-size=N 測試時所使用文件塊的大小,如果想磁盤針對innodb存儲引擎進行測試,可以將其設置為16384,即innodb存儲引擎頁的大小。默認為16384。
--file-total-size=SIZE 創建測試文件的總大小,默認為2G大小。
--file-test-mode=STRING 文件測試模式,包含:seqwr(順序寫), seqrewr(順序讀寫), seqrd(順序讀), rndrd(隨機讀), rndwr(隨機寫), rndrw(隨機讀寫)。
--file-io-mode=STRING 文件操作的模式,sync(同步),async(異步),fastmmap(快速mmap),slowmmap(慢速mmap),默認為sync同步模式。
--file-async-backlog=N 對應每個線程隊列的異步操作數,默認為128。
--file-extra-flags=STRING 打開文件時的選項,這是與API相關的參數。
--file-fsync-freq=N 執行fsync()函數的頻率。fsync主要是同步磁盤文件,因為可能有系統和磁盤緩沖的關系。 0代表不使用fsync函數。默認值為100。
--file-fsync-all=[on|off] 每執行完一次寫操作,就執行一次fsync。默認為off。
--file-fsync-end=[on|off] 在測試結束時執行fsync函數。默認為on。
--file-fsync-mode=STRING文件同步函數的選擇,同樣是和API相關的參數,由於多個操作系統對於fdatasync支持不同,因此不建議使用fdatasync。默認為fsync。
--file-merged-requests=N 大多情況下,合並可能的IO的請求數,默認為0。
--file-rw-ratio=N 測試時的讀寫比例,默認時為1.5,即可3:2
測試CPU命令幫助
sysbench --test=cpu help
參數詳解:
--cpu-max-prime=N 用來選項指定最大的素數,具體參數可以根據CPU的性能來設置,默認為10000
測試memory命令幫助
sysbench --test=memory help
參數詳解:
--memory-block-size=SIZE 測試內存塊的大小,默認為1K
--memory-total-size=SIZE 數據傳輸的總大小,默認為100G
--memory-scope=STRING 內存訪問的范圍,包括全局和本地范圍,默認為global
--memory-hugetlb=[on|off] 是否從HugeTLB池分配內存的開關,默認為off
--memory-oper=STRING 內存操作的類型,包括read, write, none,默認為write
--memory-access-mode=STRING 內存訪問模式,包括seq,rnd兩種模式,默認為seq
測試threads命令幫助
sysbench --test-threads help
參數詳解:
--thread-yields=N 指定每個請求的壓力,默認為1000
--thread-locks=N 指定每個線程的鎖數量,默認為8
測試mutex命令幫助
sysbench --test=mutex help
參數詳解:
--mutex-num=N 數組互斥的總大小。默認是4096
--mutex-locks=N 每個線程互斥鎖的數量。默認是50000
--mutex-loops=N 內部互斥鎖的空循環數量。默認是10000
測試otlp命令幫助
sysbench --test=otlp help
參數詳解:
--oltp-test-mode=STRING 執行模式{simple,complex(advanced transactional),nontrx(non-transactional),sp}。默認是complex
--oltp-reconnect-mode=STRING 重新連接模式{session(不使用重新連接。每個線程斷開只在測試結束),transaction(在每次事務結束后重新連接),query(在每個SQL語句執行完重新連接),random(對於每個事務隨機選擇以上重新連接模式)}。默認是session
--oltp-sp-name=STRING 存儲過程的名稱。默認為空
--oltp-read-only=[on|off] 只讀模式。Update,delete,insert語句不可執行。默認是off
--oltp-skip-trx=[on|off] 省略begin/commit語句。默認是off
--oltp-range-size=N 查詢范圍。默認是100
--oltp-point-selects=N number of point selects [10]
--oltp-simple-ranges=N number of simple ranges [1]
--oltp-sum-ranges=N number of sum ranges [1]
--oltp-order-ranges=N number of ordered ranges [1]
--oltp-distinct-ranges=N number of distinct ranges [1]
--oltp-index-updates=N number of index update [1]
--oltp-non-index-updates=N number of non-index updates [1]
--oltp-nontrx-mode=STRING 查詢類型對於非事務執行模式{select, update_key, update_nokey, insert, delete} [select]
--oltp-auto-inc=[on|off] AUTO_INCREMENT是否開啟。默認是on
--oltp-connect-delay=N 在多少微秒后連接數據庫。默認是10000
--oltp-user-delay-min=N 每個請求最短等待時間。單位是ms。默認是0
--oltp-user-delay-max=N 每個請求最長等待時間。單位是ms。默認是0
--oltp-table-name=STRING 測試時使用到的表名。默認是sbtest
--oltp-table-size=N 測試表的記錄數。默認是10000
--oltp-dist-type=STRING 分布的隨機數{uniform(均勻分布),Gaussian(高斯分布),special(空間分布)}。默認是special
--oltp-dist-iter=N 產生數的迭代次數。默認是12
--oltp-dist-pct=N 值的百分比被視為'special' (for special distribution)。默認是1
--oltp-dist-res=N ‘special’的百分比值。默認是75
對mysql事務型OLTP的測試:
對於mysql的OLTP測試,和file一樣,同樣需要經歷prepare,run,cleanup三個階段。
1. prepare准備階段
prepare階段會在數據庫中產生一張指定行數的表,默認表在sbtest架構下,表名為
sbtest(sysbench默認生成表的存儲引擎為innodb),如創建一張8000萬條記錄的表:
[root@db-master sysbench]# sysbench --test=oltp --oltp-table-size=80000000 --db-driver=mysql --mysql-socket=/data/mysqlsoft3307/mysql.sock --mysql-user=dba_manager --mysql-password='111111' --mysql-db=test prepare
sysbench 0.4.12: multi-threaded system evaluation benchmark
Creating table 'sbtest'...
Creating 10 records in table 'sbtest'...
run運行測試
接下來對上面產生的表進行oltp的測試:
接下來對上面產生的表進行oltp的測試:
[root@db-master sysbench]# sysbench --test=oltp --oltp-table-size=80000000 --oltp-read-only=off --init-rng=on --num-threads=16
--max-requests=0 --oltp-dist-type=uniform --max-time=3600 --mysql-user=dba_manager --mysql-password='111111' --db-driver=mysql
--mysql-socket=/data/mysqlsoft3307/mysql.sock run > result.log
參數說明:
--max-time=3600 指定測試時長為1小時
--mysql-db=test 指定測試的數據庫名
[root@db-master sysbench]# cat result.log
sysbench 0.4.12: multi-threaded system evaluation benchmark
Running the test with following options:
Number of threads: 16
Initializing random number generator from timer.
Doing OLTP test.
Running mixed OLTP test
Using Uniform distribution
Using "BEGIN" for starting transactions
Using auto_inc on the id column
Threads started!
Time limit exceeded, exiting...
(last message repeated 15 times)
Done.
OLTP test statistics:
queries performed:
read: 137346874
write: 49052449
other: 19620980
total: 206020303
transactions: 9810489 (2725.13 per sec.)
deadlocks: 2 (0.00 per sec.)
read/write requests: 186399323 (51777.50 per sec.)
other operations: 19620980 (5450.26 per sec.)
Test execution summary:
total time: 3600.0060s
total number of events: 9810489
total time taken by event execution: 57542.1464
per-request statistics:
min: 3.00ms
avg: 5.87ms
max: 212.09ms
approx. 95 percentile: 8.44ms
Threads fairness:
events (avg/stddev): 613155.5625/1486.47
execution time (avg/stddev): 3596.3842/0.01
以上測試結果顯示了很多操作的詳細信息,transactions代表測試結果的評判標准即TPS,上述測試結果是 2725.13 per sec.
可以對數據庫進行調優后,再使用sysbench對OLTP進行測試,看看TPS是不是會有所提高。
注意:sysbench的測試只是基准測試,並不能代表實際企業環境下的性能指標