1.素數介紹
sysbench的cpu測試是在指定時間內,循環進行素數計算
素數(也叫質數)就是從1開始的自然數中,無法被整除的數,比如2、3、5、7、11、13、17等。編程公式:對正整數n,如果用2到根號n之間的所有整數去除,均無法整除,則n為素數。
2.sysbench安裝、使用
# CentOS7下可使用yum安裝 yum install sysbench
#使用方法
sysbench [general-options]... --test=<test-name> [test-options]... command
General options: #通用選項
--num-threads=N number of threads to use [1] #創建測試線程的數目。默認為1.
--max-requests=N limit for total number of requests [10000] #請求的最大數目。默認為10000,0代表不限制。
--max-time=N limit for total execution time in seconds [0] #最大執行時間,單位是s。默認是0,不限制。
--forced-shutdown=STRING amount of time to wait after --max-time before forcing shutdown [off] #超過max-time強制中斷。默認是off。
--thread-stack-size=SIZE size of stack per thread [32K] #每個線程的堆棧大小。默認是32K。
--init-rng=[on|off] initialize random number generator [off] #在測試開始時是否初始化隨機數發生器。默認是off。
--test=STRING test to run #指定測試項目名稱。
--debug=[on|off] print more debugging info [off] #是否顯示更多的調試信息。默認是off。
--validate=[on|off] perform validation checks where possible [off] #在可能情況下執行驗證檢查。默認是off。
--help=[on|off] print help and exit #幫助信息。
--version=[on|off] print version and exit #版本信息。
Compiled-in tests: #測試項目
fileio - File I/O test #IO
cpu - CPU performance test #CPU
memory - Memory functions speed test #內存
threads - Threads subsystem performance test #線程
mutex - Mutex performance test #互斥性能測試
oltp - OLTP test # 數據庫,事務處理
Commands:
prepare:測試前准備工作;
run:正式測試
cleanup:測試后刪掉測試數據 help version
See 'sysbench --test=<name> help' for a list of options for each test. #查看每個測試項目的更多選項列表
3.CPU壓測命令
# 默認參數,素數上限10000,時間10秒,單線程 sysbench --test=cpu --cpu-max-prime=**** run --num-threads=*****
參數介紹
--cpu-max-prime: 素數生成數量的上限
- 若設置為3,則表示2、3、5(這樣要計算1-5共5次) - 若設置為10,則表示2、3、5、7、11、13、17、19、23、29(這樣要計算1-29共29次) - 默認值為10000
--threads: 線程數
- 若設置為1,則sysbench僅啟動1個線程進行素數的計算 - 若設置為2,則sysbench會啟動2個線程,同時分別進行素數的計算 - 默認值為1
--time: 運行時長,單位秒
- 若設置為5,則sysbench會在5秒內循環往復進行素數計算, 從輸出結果可以看到在5秒內完成了幾次, 比如配合--cpu-max-prime=3,則表示第一輪算得3個素數, 如果時間還有剩就再進行一輪素數計算,直到時間耗盡。 每完成一輪就叫一個event - 默認值為10 - 相同時間,比較的是誰完成的event多
--events: event上限次數
- 若設置為100,則表示當完成100次event后,即使時間還有剩,也停止運行 - 默認值為0,則表示不限event次數 - 相同event次數,比較的是誰用時更少
結果分析
執行命令
# 素數上限2萬,默認10秒,2個線程 sysbench cpu --cpu-max-prime=20000 --threads=2 run
結果分析
sysbench 1.0.9 (using system LuaJIT 2.0.4) Running the test with following options: Number of threads: 2 // 指定線程數為2 Initializing random number generator from current time Prime numbers limit: 20000 // 每個線程產生的素數上限均為2萬個 Initializing worker threads... Threads started! CPU speed: events per second: 650.74 // 所有線程每秒完成了650.74次event General statistics: total time: 10.0017s // 共耗時10秒 total number of events: 6510 // 10秒內所有線程一共完成了6510次event Latency (ms): min: 3.03 // 完成1次event的最少耗時3.03秒 avg: 3.07 // 所有event的平均耗時3.07毫秒 max: 3.27 // 完成1次event的最多耗時3.27毫秒 95th percentile: 3.13 // 95%次event在3.13秒毫秒內完成 sum: 19999.91 // 每個線程耗時10秒,2個線程疊加耗時就是20秒 Threads fairness: events (avg/stddev): 3255.0000/44.00 // 平均每個線程完成3255次event,標准差為44 execution time (avg/stddev): 10.0000/0.00 // 每個線程平均耗時10秒,標准差為0
event: 完成了幾輪的素數計算
stddev(標准差): 在相同時間內,多個線程分別完成的素數計算次數是否穩定,如果數值越低,則表示多個線程的結果越接近(即越穩定)。該參數對於單線程無意義。
性能對比
如果有2台服務器進行CPU性能對比,當素數上限和線程數一致時:
- 相同時間,比較event
- 相同event,比較時間
- 時間和event都相同,比較stddev(標准差)
