Linux命令:stress-ng 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 PERCENTCPU 负载百分比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 测试方法方法名说明crc32CRC32 校验计算md5MD5 哈希计算sha256SHA256 哈希计算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;doechoTesting with$iCPU cores...stress-ng--cpu$i--timeout30--metrics-briefdone监控系统状态# 后台运行 stress-ng前台监控stress-ng--cpu4--timeout120top相关命令stress原始版本的压力测试工具cpuburnCPU 压力测试工具memtester内存测试工具fio灵活的 I/O 测试工具perf性能分析工具vmstat系统资源监控iostat磁盘 I/O 监控top实时系统监控注意事项资源消耗stress-ng会消耗大量系统资源可能影响其他服务测试时长建议设置合理的测试时间避免长时间运行内存测试确保系统有足够的内存避免触发 OOM killer磁盘测试确保磁盘有足够的可用空间权限要求通常不需要 root 权限但某些系统可能有限制安装方法Debian/Ubuntuapt-get install stress-ngCentOS/RHELyum install stress-ng总结stress-ng是stress的增强版本提供了更丰富的测试选项和更强大的功能。它支持数百种测试方法可以对 CPU、内存、磁盘等资源进行全面的压力测试。通过合理使用stress-ng系统管理员和开发者可以评估系统在高负载情况下的表现为系统优化和资源规划提供依据。