linux 下對CPU壓測的兩種方法

NO IMAGE

常用的對CPU進行壓測是通過計算圓周率讓CPU高負荷運作,以達到考驗CPU計算能力與穩定性的目的。下面介紹兩種對CPU進行壓測的方法:

第一種:bc計算圓周率

[[email protected] ~]# echo "scale=5000; 4*a(1)" | bc -l -q

表示計算圓周率達到小數點後面的5000位。

第二種:Super PI 計算圓周率小數點後n位所需要的時間
Super PI是利用CPU的浮點運算能力來計算出圓周率π的。
1、下包、解壓:

[[email protected] ~]# wget http://linux-wiki.cn/archieve/super_pi.tar.gz
[[email protected] ~]# tar -zxvf super_pi.tar.gz 
pi
super_pi
Readme.txt

解壓完在當前目錄下會生成三個檔案:pi Readme.txt super_pi

然後直接執行./super_pi 20

[[email protected] ~]# ./super_pi 20
./super_pi: ./pi: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory

出現這個報錯,解決辦法:

[[email protected] ~]#  yum install glibc.i686

然後再執行:

[[email protected] ~]# ./super_pi 20
Version 2.0 of the super_pi for Linux OS
Fortran source program was translated into C program with version 19981204 of
f2c, then generated C source program was optimized manually.
pgcc 3.2-3 with compile option of "-fast -tp px -Mbuiltin -Minline=size:1000 -Mnoframe -Mnobounds -Mcache_align -Mdalign -Mnoreentrant" was used for the
compilation.
------ Started super_pi run : Fri Dec 29 18:48:43 CST 2017
Start of PI calculation up to 1048576 decimal digits
End of initialization. Time=       0.234 Sec.
I= 1 L=       0        Time=       0.672 Sec.
I= 2 L=       0        Time=       0.810 Sec.
= 3 L=       1        Time=       0.882 Sec.
I= 4 L=       2        Time=       0.792 Sec.
I= 5 L=       5        Time=       0.780 Sec.
I= 6 L=      10        Time=       0.798 Sec.
I= 7 L=      21        Time=       0.875 Sec.
I= 8 L=      43        Time=       0.787 Sec.
I= 9 L=      87        Time=       0.770 Sec.
I=10 L=     174        Time=       0.838 Sec.
I=11 L=     349        Time=       0.830 Sec.
I=12 L=     698        Time=       0.800 Sec.
I=13 L=    1396        Time=       0.766 Sec.
I=14 L=    2794        Time=       0.911 Sec.
I=15 L=    5588        Time=       0.785 Sec.
I=16 L=   11176        Time=       0.778 Sec.
I=17 L=   22353        Time=       0.762 Sec.
I=18 L=   44707        Time=       0.802 Sec.
I=19 L=   89415        Time=       0.687 Sec.
End of main loop
End of calculation.    Time=      15.967 Sec.
End of data output.    Time=       0.081 Sec.
Total calculation(I/O) time=      16.048(       0.746) Sec.
------ Ended super_pi run : Fri Dec 29 18:49:00 CST 2017

現在來解釋一下這條命令到底幹了什麼:
是計算圓周率小數點後2的20次方位。如果換成執行:./super_pi 30 就是計算圓周率小數點後2的30次方位。

輸出的是檢測的時間,計算的位數越多,檢測時間越長,對CPU的考驗也越大。

通常一般情況下可以選擇104萬位(2的20次方)的運算,如果要求較高則可以選擇419萬位(2的22次方)的運算。如果CPU能夠在最高的3355萬位(2的25方)的檢測中通過,則該CPU將能夠在非常苛刻的環境下穩定執行。

可以結合zabbix監控CPU的剩餘時間來判斷是否通過檢測:
zabbix (3.4.2)新增監控項,觸發器,讓CPU使用超過85%就報警