一,安裝stress:
說明:el8的源里面還沒有,先用el7的rpm包
[root@centos8 source]# wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/s/stress-1.0.4-16.el7.x86_64.rpm [root@centos8 source]# rpm -ivh stress-1.0.4-16.el7.x86_64.rpm
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,stress的用途:
stress 命令主要用來模擬系統負載較高時的場景,
可以對cpu、memory、IO以及磁盤進行壓力測試
三,查看stress的版本和幫助
1,查看版本
[root@centos8 source]# stress --version stress 1.0.4
2,查看幫助:
[root@centos8 source]# stress --help `stress' imposes certain types of compute stress on your system Usage: stress [OPTION [ARG]] ... -?, --help show this help statement --version show version statement -v, --verbose be verbose -q, --quiet be quiet -n, --dry-run show what would have been done -t, --timeout N timeout after N seconds --backoff N wait factor of N microseconds before work starts -c, --cpu N spawn N workers spinning on sqrt() -i, --io N spawn N workers spinning on sync() -m, --vm N spawn N workers spinning on malloc()/free() --vm-bytes B malloc B bytes per vm worker (default is 256MB) --vm-stride B touch a byte every B bytes (default is 4096) --vm-hang N sleep N secs before free (default none, 0 is inf) --vm-keep redirty memory instead of freeing and reallocating -d, --hdd N spawn N workers spinning on write()/unlink() --hdd-bytes B write B bytes per hdd worker (default is 1GB) Example: stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 10s Note: Numbers may be suffixed with s,m,h,d,y (time) or B,K,M,G (size)
四,stress命令的參數說明:
-c --cpu 產生n個進程 每個進程都反復不停的計算隨機數的平方根
-i --io 產生n個進程 每個進程反復調用sync(),sync()用於將內存上的內容寫到硬盤上
-m --vm n 產生n個進程,每個進程不斷調用內存分配malloc和內存釋放free函數
--vm-bytes B 指定malloc時內存的字節數 (默認256MB)
--vm-hang N 指示每個消耗內存的進程在分配到內存后轉入休眠狀態,與正常的無限分配和釋放內存的處理相反,這有利於模擬只有少量內存的機器
-d --hadd n 產生n個執行write和unlink函數的進程
--hadd-bytes B 指定寫的字節數,默認是1GB
--hadd-noclean 不要將寫入隨機ASCII數據的文件Unlink
-t, --timeout N 持續指定的N秒后退出
五,stress的使用例子:cpu
1,在容器內執行stress,創建四個消耗 CPU 資源的進程
[root@centos8 ~]# stress -c 4 --timeout 600 stress: info: [3868] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd
2,用pidstat查看cpu的使用數據
# 顯示所有CPU的指標,並在間隔5秒輸出一組數據
[root@centos8 ~]# pidstat -p ALL 5 平均時間: UID PID %usr %system %guest %wait %CPU CPU Command 平均時間: 0 3869 48.76 0.00 0.00 50.65 48.76 - stress 平均時間: 0 3870 46.87 0.10 0.00 52.54 46.97 - stress 平均時間: 0 3871 47.36 0.00 0.00 51.84 47.36 - stress 平均時間: 0 3872 48.16 0.00 0.00 51.44 48.16 - stress
說明:可以明顯看到,stress 進程的 CPU 使用率很高
六,stress的使用例子:內存
1,啟動2個消耗內存的進程,每個進程占用200M內存
[root@centos8 ~]# stress -m 2 --vm-bytes 200M stress: info: [4364] dispatching hogs: 0 cpu, 0 io, 2 vm, 0 hdd
2,用pidstat 查看內存的占用情況
[root@centos8 ~]# pidstat -r | grep stress 13時32分48秒 UID PID minflt/s majflt/s VSZ RSS %MEM Command 13時33分38秒 0 4364 0.04 0.00 7948 1044 0.03 stress 13時33分38秒 0 4365 8748.16 0.00 212752 56072 1.46 stress 13時33分38秒 0 4366 9156.42 0.00 212752 91712 2.38 stress
七,stress的使用例子:硬盤
1, stress -d N 會產生N個進程:
每個進程往當前目錄中寫入固定大小的臨時文件,然后執行unlink操作刪除該臨時文件。
臨時文件的大小默認為1G,但可以通過 --hdd-bytes 設置臨時文件的大小。
[root@centos8 ~]# stress -d 2 stress: info: [4523] dispatching hogs: 0 cpu, 0 io, 0 vm, 2 hdd
2,用pidstat監控磁盤,可以看到兩個stress進程在做大量的寫入
[root@centos8 ~]# pidstat -d Linux 4.18.0-147.5.1.el8_1.x86_64 (centos8) 2020年04月03日 _x86_64_ (2 CPU)
13時43分47秒 UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command 13時43分47秒 0 4524 0.00 166.98 0.00 0 stress 13時43分47秒 0 4525 0.00 165.31 0.00 0 stress
3,用iostat查看,也可以看到 kB_wrtn/s 的數值增長
[root@centos8 ~]# iostat Linux 4.18.0-147.5.1.el8_1.x86_64 (centos8) 2020年04月03日 _x86_64_ (2 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 6.40 0.02 3.14 1.25 0.00 89.19 Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn sda 11.34 273.05 5296.74 863387 16748451 scd0 0.01 0.34 0.00 1060 0 dm-0 11.14 252.57 5296.85 798627 16748789 dm-1 0.17 0.75 0.51 2376 1616 dm-2 0.24 7.82 1.71 24714 5422
八,stress的使用例子:io
1,使用4個進程生成大量io, 即執行:sync()操作
[root@centos8 source]# stress -i 4
2,使用top可以看到stress的4個進程占用了大量的cpu資源
[root@centos8 conf]# top
... PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 9143 root 20 0 7948 96 0 R 54.5 0.0 0:20.52 stress 9142 root 20 0 7948 96 0 R 53.8 0.0 0:20.88 stress 9141 root 20 0 7948 96 0 R 52.5 0.0 0:20.31 stress 9144 root 20 0 7948 96 0 R 52.2 0.0 0:20.65 stress
3,用pidstat查看進程,能看到stress的4個進程產生的大量的wait類的cpu使用
pidstat參數:間隔5秒后輸出一組數據,-u表示CPU指標
[root@centos8 conf]# pidstat -u 5 1 Linux 4.18.0-147.5.1.el8_1.x86_64 (centos8) 2020年04月01日 _x86_64_ (2 CPU) 15時36分37秒 UID PID %usr %system %guest %wait %CPU CPU Command 15時36分42秒 0 1064 0.20 0.00 0.00 6.72 0.20 1 vmtoolsd 15時36分42秒 1000 2704 1.19 0.00 0.00 0.99 1.19 0 gnome-shell 15時36分42秒 0 2889 0.20 0.00 0.00 0.20 0.20 0 sssd_kcm 15時36分42秒 1000 3279 0.20 0.00 0.00 1.98 0.20 0 gnome-terminal- 15時36分42秒 0 8992 0.00 0.20 0.00 2.57 0.20 0 kworker/0:0-events 15時36分42秒 0 9033 0.59 48.81 0.00 50.59 49.41 0 stress 15時36分42秒 0 9034 0.59 48.42 0.00 50.00 49.01 1 stress 15時36分42秒 0 9035 0.59 48.02 0.00 50.59 48.62 0 stress 15時36分42秒 0 9036 0.59 48.62 0.00 50.20 49.21 1 stress 15時36分42秒 0 9074 0.20 0.40 0.00 0.99 0.59 0 pidstat 平均時間: UID PID %usr %system %guest %wait %CPU CPU Command 平均時間: 0 1064 0.20 0.00 0.00 6.72 0.20 - vmtoolsd 平均時間: 1000 2704 1.19 0.00 0.00 0.99 1.19 - gnome-shell 平均時間: 0 2889 0.20 0.00 0.00 0.20 0.20 - sssd_kcm 平均時間: 1000 3279 0.20 0.00 0.00 1.98 0.20 - gnome-terminal- 平均時間: 0 8992 0.00 0.20 0.00 2.57 0.20 - kworker/0:0-events 平均時間: 0 9033 0.59 48.81 0.00 50.59 49.41 - stress 平均時間: 0 9034 0.59 48.42 0.00 50.00 49.01 - stress 平均時間: 0 9035 0.59 48.02 0.00 50.59 48.62 - stress 平均時間: 0 9036 0.59 48.62 0.00 50.20 49.21 - stress 平均時間: 0 9074 0.20 0.40 0.00 0.99 0.59 - pidstat
可以看到stress占用了cpu大量的wait類時間
九,查看centos的版本
[root@centos8 ~]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core)