stress-ng 命令
基本介绍
stress-ng是stress工具的增强版本,提供了更丰富的压力测试选项和更强大的功能。它支持 CPU、内存、磁盘、网络等多种资源的压力测试,包含数百种测试方法,是系统性能测试和稳定性验证的理想工具。
资料合集:https://pan.quark.cn/s/6fe3007c3e95、https://pan.quark.cn/s/561de99256a5、https://pan.quark.cn/s/985f55b13d94、https://pan.quark.cn/s/d0fb20abd19a
语法
stress-ng [OPTIONS]常用选项
CPU 压力测试选项
--cpu N:生成 N 个 CPU 密集型进程--cpu-method METHOD:指定 CPU 测试方法(如 crc32、md5、sha256、fibonacci 等)--cpu-load PERCENT:CPU 负载百分比(0-100)--cpu-max-threads N:最大 CPU 线程数
内存压力测试选项
--vm N:生成 N 个内存分配进程--vm-bytes SIZE:每个内存进程分配的内存大小--vm-keep:保持分配的内存不释放--vm-hang N:分配内存后暂停 N 秒再释放--vm-method METHOD:内存分配方法(如 malloc、mmap、posix_memalign 等)--vm-stride SIZE:内存访问步长
磁盘 I/O 压力测试选项
--hdd N:生成 N 个磁盘 I/O 进程--hdd-bytes SIZE:每个 I/O 进程写入的文件大小--hdd-method METHOD:磁盘测试方法(如 write、read、sync、randwrite 等)--hdd-noclean:测试完成后不清理临时文件
时间控制选项
--timeout SECS:运行指定秒数后退出--sleep SECS:在启动每个工作进程前等待指定秒数--interval SECS:输出状态间隔时间
其他选项
-q, --quiet:静默模式,减少输出-v, --verbose:详细模式,增加输出-?, --help:显示帮助信息-V, --version:显示版本信息--metrics-brief:输出简要性能指标--metrics:输出详细性能指标
使用示例
1. 单 CPU 压力测试
stress-ng--cpu12. 多 CPU 压力测试
stress-ng--cpu4--timeout60stress-ng--cpu$(nproc)--cpu-method crc32--timeout1203. 指定 CPU 测试方法
stress-ng--cpu2--cpu-method md5--timeout30stress-ng--cpu4--cpu-method fibonacci--timeout60输出示例:
stress-ng: info: [1234] dispatching hogs: 4 cpu stress-ng: info: [1234] stress-ng 0.13.04 (gcc 9.4.0) 64-bit stress-ng: info: [1234] 4 cpus, 48363 MB RAM, 4096 MB swap stress-ng: info: [1234] time limit set to 60s stress-ng: info: [1234] running 4 cpu stressors stress-ng: info: [1234] completed 48000000 operations in 60.00s stress-ng: info: [1234] cpu: 48000000 operations, 800000 ops/sec4. 内存压力测试
stress-ng--vm2--vm-bytes 1G--timeout60stress-ng--vm4--vm-bytes 512M --vm-keep--timeout1205. 磁盘 I/O 压力测试
stress-ng--hdd1--hdd-bytes 2G--timeout60stress-ng--hdd2--hdd-method randwrite --hdd-bytes 1G--timeout1206. 混合压力测试
stress-ng--cpu4--vm2--hdd1--timeout120stress-ng--cpu8--cpu-load80--vm4--vm-bytes 1G--timeout3007. 显示性能指标
stress-ng--cpu4--timeout30--metrics-brief输出示例:
stress-ng: info: [1234] metrics: stress-ng: info: [1234] cpu: ops/sec 800000, time 30.00s stress-ng: info: [1234] vm: alloc 2.0G, ops/sec 1000 stress-ng: info: [1234] hdd: ops/sec 500, bytes 500M8. 使用特定 CPU 核心
stress-ng--cpu2--cpu-affinity0,1--timeout609. 测试指定时间后停止
stress-ng--cpu4--timeout60--sleep5CPU 测试方法
| 方法名 | 说明 |
|---|---|
crc32 | CRC32 校验计算 |
md5 | MD5 哈希计算 |
sha256 | SHA256 哈希计算 |
fibonacci | 斐波那契数列计算 |
pi | 圆周率计算 |
prime | 素数查找 |
sqrt | 平方根计算 |
log | 对数计算 |
sin | 正弦函数计算 |
float | 浮点数运算 |
内存测试方法
| 方法名 | 说明 |
|---|---|
malloc | 使用 malloc 分配内存 |
mmap | 使用 mmap 分配内存 |
posix_memalign | 使用 posix_memalign 分配内存 |
aligned_alloc | 使用 aligned_alloc 分配内存 |
calloc | 使用 calloc 分配内存 |
磁盘测试方法
| 方法名 | 说明 |
|---|---|
write | 顺序写入 |
read | 顺序读取 |
sync | 同步写入 |
randwrite | 随机写入 |
randread | 随机读取 |
mixed | 混合读写 |
实用技巧
测试 CPU 性能
# 使用多种方法测试stress-ng--cpu4--cpu-method crc32,md5,sha256,prime--timeout60# 测试所有 CPU 核心stress-ng--cpu$(nproc)--timeout120--metrics-brief测试内存稳定性
# 分配大量内存并保持stress-ng--vm4--vm-bytes 2G --vm-keep--timeout120# 测试内存分配和释放stress-ng--vm2--vm-bytes 1G --vm-hang1--timeout60测试磁盘 I/O
# 测试磁盘写入性能stress-ng--hdd2--hdd-bytes 5G --hdd-methodsync--timeout120# 测试随机读写stress-ng--hdd1--hdd-method randwrite --hdd-bytes 2G--timeout60综合压力测试
# 模拟高负载场景stress-ng--cpu8--cpu-load90--vm4--vm-bytes 1G--hdd2--timeout300# 逐步增加压力foriin1248;doecho"Testing with$iCPU cores..."stress-ng--cpu$i--timeout30--metrics-briefdone监控系统状态
# 后台运行 stress-ng,前台监控stress-ng--cpu4--timeout120&top相关命令
stress:原始版本的压力测试工具cpuburn:CPU 压力测试工具memtester:内存测试工具fio:灵活的 I/O 测试工具perf:性能分析工具vmstat:系统资源监控iostat:磁盘 I/O 监控top:实时系统监控
注意事项
- 资源消耗:
stress-ng会消耗大量系统资源,可能影响其他服务 - 测试时长:建议设置合理的测试时间,避免长时间运行
- 内存测试:确保系统有足够的内存,避免触发 OOM killer
- 磁盘测试:确保磁盘有足够的可用空间
- 权限要求:通常不需要 root 权限,但某些系统可能有限制
- 安装方法:
- Debian/Ubuntu:
apt-get install stress-ng - CentOS/RHEL:
yum install stress-ng
- Debian/Ubuntu:
总结
stress-ng是stress的增强版本,提供了更丰富的测试选项和更强大的功能。它支持数百种测试方法,可以对 CPU、内存、磁盘等资源进行全面的压力测试。通过合理使用stress-ng,系统管理员和开发者可以评估系统在高负载情况下的表现,为系统优化和资源规划提供依据。