Categories: Linux

Linux 使用 Stress-ng 測試 CPU、記憶體、磁碟 I/O 滿載情況教學與範例

介紹如何在 Linux 系統上使用 stress-ng 負載測試工具,產生 CPU、記憶體等資源滿載的狀況。

stress-ng

stress-ng 與舊的 stress 都可以用來產生系統負載,但新的 stress-ng 功能較豐富,所以這裡我們只介紹 stress-ng 的使用方式。

stress-ng 支援多種產生系統負載的方式,包含 CPU 的浮點運算、整數運算、位元運算與控制流程等,可以用來測試系統在高負載的狀況下的穩定性。

stress-ng 必須小心使用,某些測試可能會造成設計不良的硬體過熱,另外也可能讓系統過載而難以停止負載測試。

安裝 stress-ng

若在 Ubuntu Linux 中可以使用 apt 來安裝 stress-ng 套件:

# 安裝 stress-ng 套件
sudo apt install stress-ng

Stressors

stress-ng 會使用各種不同的 stressors 來產生不同性質的系統負載,stressor 的種類非常多,包含:cpucpu-cachedeviceiointerruptfilesystemmemorynetworkospipeschedulervm

我們可以使用以下指令查詢 stress-ng 所有支援的 stressors:

# 查詢支援的 stressors
stress-ng --stressors
af-alg affinity aio aiol apparmor atomic bigheap bind-mount branch brk bsearch cache cap chdir chmod chown chroot clock clone context copy-file cpu cpu-online crypt cyclic daemon dccp dentry dev dir dirdeep dnotify dup dynlib enosys epoll eventfd exec fallocate fanotify fault fcntl fiemap fifo file-ioctl filename flock fork fp-error fstat full funccall futex get getdent getrandom handle hdd heapsort hrtimers hsearch icache icmp-flood inode-flags inotify io iomix ioport ioprio itimer kcmp key kill klog lease link locka lockbus lockf lockofd longjmp lsearch madvise malloc matrix mcontend membarrier memcpy memfd memrate memthrash mergesort mincore mknod mlock mmap mmapaddr mmapfixed mmapfork mmapmany mq mremap msg msync netdev netlink-proc nice nop null numa oom-pipe opcode open personality physpage pipe poll procfs pthread ptrace pty qsort quota radixsort rawdev rdrand readahead remap rename resources revio rlimit rmap rtc schedpolicy sctp seal seccomp seek sem sem-sysv sendfile shm shm-sysv sigfd sigfpe sigio sigpending sigpipe sigq sigsegv sigsuspend sleep sock sockdiag sockfd sockpair softlockup spawn splice stack stackmmap str stream swap switch symlink sync-file sysinfo sysfs tee timer timerfd tlb-shootdown tmpfs tree tsc tsearch udp udp-flood unshare urandom userfaultfd utime vecmath vfork vforkmany vm vm-addr vm-rw vm-splice wait wcs xattr yield zero zlib zombie

若要查詢指定類別之下有哪些 stressors 可用,可以使用 --class 參數指定類別名稱,再加上一個問號(?),例如若要查詢 vm 類別下的 stressors,就可以執行:

# 查詢 vm 類別的 stressors
stress-ng --class vm?
class 'vm' stressors: bigheap brk madvise malloc mlock mmap mmapaddr mmapfixed mmapfork mmapmany mremap msync physpage shm shm-sysv stack stackmmap swap tmpfs userfaultfd vm vm-addr vm-rw vm-splice

關於這些 stressors 的詳細用法與說明,可以參考 stress-ng 的線上手冊:

# 查詢 stress-ng 線上手冊
man stress-ng

使用者可以依據需求,組合各種 stressors 創造出適合的負載環境,以下是一些常見的組合範例。

測試 CPU 滿載狀況

若要產生 2 個 CPU 核心滿載的狀況,可以使用 2 個 CPU stressors:

# 產生 CPU 滿載狀況(2 CPU stressors、持續 30 秒)
stress-ng --cpu 2 --timeout 30s
stress-ng: info:  [31788] dispatching hogs: 2 cpu
stress-ng: info:  [31788] successful run completed in 30.02s

事實上 CPU 的 stressors 種類非常多,單純指定 --cpu 參數的話,會輪流使用各種 CPU 類型的 stressors,若要明確測試指定的 CPU 負載類型,可以指定要使用的 stressor。

測試 zlib 壓縮、解壓縮

使用 2 個 zlib stressors 壓縮與解壓縮隨機資料,進行 CPU 負載測試:

# 以 2 個 zlib stressors 壓縮、解壓縮資料,產生 CPU 滿載狀況
stress-ng --zlib 2 --timeout 30s

使用 zlib 壓縮與解壓縮資料時,除了 CPU 之外,同時也會產生快取(cache)與記憶體(memory)的負載。

測試矩陣運算

使用 2 個 matrix stressors 產生各種矩陣運算,進行 CPU 負載測試:

# 以兩個 matrix stressors 進行矩陣運算,產生 CPU 滿載狀況
stress-ng --matrix 2 --timeout 30s

記憶體測試

以 8 個 vm stressors 使用 80% 的可用記憶體(1 個 stressor 使用 10% 可用記憶體)執行記憶體測試,持續 1 小時:

# 使用 8 個 vm stressors 執行記憶體測試
# 總共使用 80% 的可用記憶體,持續 1 小時
stress-ng --vm 8 --vm-bytes 80% -t 1h

I/O 測試

以 2 個 iomix stressors 使用 10% 磁碟空間(1 個 stressor 使用 5% 磁碟空間)執行 I/O 測試,持續 10 分鐘:

# 使用 2 個 iomix stressors 執行混合 I/O,
# 使用 10% 磁碟空間,持續 10 分鐘
stress-ng --iomix 2 --iomix-bytes 10% -t 10m

輪流執行 8 個 io 類型的 stressors,每個類型持續 5 分鐘,並產生執行時間報表:

# 輪流執行 8 個 io 類型的 stressors,每個類型持續 5 分鐘
# 並產生執行時間報表
stress-ng --sequential 8 --class io -t 5m --times

混合多種 Stressors

我們也可以混合不同的 stressors 來使用,例如一個 zlib 與一個 matrix

# 以 1 個 zlib 與 1 個 matrix stressors
stress-ng --zlib 1 --matrix 1 --timeout 30s

FFT 計算測試

以 2 個 FFT 的 stressors,執行 5000 個 bogo 運算,產出報表:

# 以 2 個 FFT 的 stressors,執行 5000 個 bogo 運算,產出報表
stress-ng --cpu 2 --cpu-method fft --cpu-ops 5000 --metrics-brief

混合測試

使用 4 個各種類的 stressors 持續 5 分鐘:

# 使用 4 個各種類的 stressors 持續 5 分鐘
stress-ng --all 4 --timeout 5m

每一種 stressors 輪流執行,每個持續 10 分鐘:

# 每一種 stressors 輪流執行,每個持續 10 分鐘
stress-ng --sequential 0 -t 10m

在所有的 CPU 上執行各種 CPU stressors,持續 1 小時:

# 在所有的 CPU 上執行各種 stressors,持續 1 小時
stress-ng --cpu 0 --cpu-method all -t 1h

參考資料

Share
Published by
Office Guide

Recent Posts

Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤

本篇介紹如何在 Python ...

9 個月 ago

Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例

本篇介紹如何在 Ubuntu ...

9 個月 ago

Linux 網路設定 ip 指令用法教學與範例

本篇介紹如何在 Linux 系...

9 個月 ago

Windows 使用 TPM 虛擬智慧卡保護 SSH 金鑰教學與範例

本篇介紹如何在 Windows...

10 個月 ago

Linux 以 Shamir’s Secret Sharing 分割保存金鑰教學與範例

介紹如何在 Linux 中使用...

11 個月 ago

Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例

介紹如何在 Linux 系統中...

11 個月 ago