CPU和内存压力测试
1. Linux环境
Linux环境下,我们可以用 stress
工具进行内存、CPU等的压力测试。
【1】. stress工具说明
shell
[kalami@kysrv1 ~]$ 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).
[kalami@kysrv1 ~]$
【2】. CPU压力测试
命令格式如下:
shell
stress -c 10 -v --timeout 10s
模拟10个任务。

top
命令查看压力测试效果:

【3】. 内存压力测试
命令格式如下:
shell
stress --vm 20 --vm-bytes 4096M --timeout 10s
压力测试前的内存情况如下:

执行内存压力测试:

top
命令查看压力测试效果:

2. Windows环境
CPU压力测试推荐使用 CpuStres
工具,内存压力测试推荐使用 Testlimit
工具。
【1】. 压力测试工具
CpuStres工具
CpuStres 工具可以通过在紧密循环中运行多达 64 个线程来模拟 CPU 活动。
下载地址(中文): CpuStres - Sysinternals | Microsoft Learn
shell
https://learn.microsoft.com/zh-cn/sysinternals/downloads/cpustres

下载地址(英文): CpuStres - Sysinternals | Microsoft Learn
shell
https://learn.microsoft.com/en-us/sysinternals/downloads/cpustres

Testlimit工具
Testlimit 是一种命令行实用工具,可用于通过模拟内存、句柄、进程、线程和其他系统对象的低资源条件来对电脑和/或应用程序进行压力测试。
下载地址(中文): Testlimit - Sysinternals | Microsoft Learn
shell
https://learn.microsoft.com/zh-cn/sysinternals/downloads/testlimit

下载地址(英文): Testlimit - Sysinternals | Microsoft Learn
shell
https://learn.microsoft.com/en-us/sysinternals/downloads/testlimit

解压测试工具包。

【2】. CPU压力测试
CpuStres 工具可以通过在紧密循环中运行多达 64 个线程来模拟 CPU 活动。
双击运行 CPUSTRES.EXE
工具,界面如下图所示:

增加工作线程
方法一:
tex
菜单-->Process-->Create 4 Threads

方法二:
tex
主视图空白处-->右键菜单-->Create 4 Threads

方法三:
tex
工具栏-->点击"Create Thread" 按钮

设置工作线程优先级
方法一:
shell
菜单栏-->Process-->Priority Class

方法二:
tex
主视图空白处-->右键菜单-->Priority Class

设置工作线程运行级别
方法:
tex
菜单栏-->Thread-->Activity Level

压力测试
tex
设置线程数-->选中相关线程-->设置优先级

tex
选中相关线程-->设置运行级别

tex
选中相关线程-->运行

开始CPU压力测试

CPU曲线

【3】. 内存压力测试
Testlimit 是一种命令行实用工具,可用于通过模拟内存、句柄、进程、线程和其他系统对象的低资源条件来对电脑和/或应用程序进行压力测试。
用法: Testlimit [[-h [-u]] | [-p [-n]] | [-t [-n [KB]]] | [-u [-i]] | [-g [对象大小]] | [-a|-d|-l|-m|-r|-s|-v [MB]] | [-w]] [-c [计数]] [-e [秒数]]
参数 | 说明 |
---|---|
-a | 以指定的 MB 单位泄漏地址窗口扩展 (AWE) 内存(默认值为 1) |
-c | 要分配的对象计数(默认值是"尽可能多")。 这必须是指定的最后一个选项 |
-d | 以指定的 MB 单位泄漏和触摸内存(默认值为 1) |
-e | 两次分配之间经过的秒数(默认值为 0) |
-g | 创建指定大小的 GDI 句柄(默认为 1 字节)。 将大小指定为 0 将导致 GDI 对象耗尽 |
-h | 创建句柄。 指定 -u 也会分配文件对象 |
-i | 耗尽 USER 桌面堆 |
-l | 分配指定数量的大页面 |
-m | 以指定的 MB 单位泄漏内存(默认值为 1) |
-p | 创建进程 - 添加 -n 以设置最小工作集。 添加 -n 以将进程的最小工作集设置为最小 |
-r | 以指定的 MB 单位保留内存(默认值为 1) |
-s | 以指定的 MB 单位泄露共享内存(默认值为 1) |
-t | 创建线程 - 添加 -n 以指定最小堆栈保留(以 KB 为单位) |
-u | 创建菜单的 USER 句柄 |
-v | 以指定的 MB 单位对内存执行 VirtualLock(默认值为 1) |
-w | 将工作集最小值重置为最高可能值 |
英文说明
tex
usage: testlimit [[-h [-u]] | [-p [-n]] | [-t [-n [KB]]] | [-u [-i]] | [-g [object size]] | [-a|-d|-l|-m|-r|-s|-v [MB]] | [-w]] [-c [count]] [-e [seconds]]
-a Leak Address Windowing Extensions (AWE) memory in
specified MBs (default is 1).
-c Count of number of objects to allocate (default is as many as
possible). This must be the last option specified.
-d Leak and touch memory in specified MBs (default is 1).
-e Seconds elapsed between allocations (default is 0).
-g Create GDI handles of specified size (default 1 byte).
Specify a size of 0 to cause GDI object exhaustion.
-h Create handles. Specify -u to also allocate file objects.
-i Exhaust USER desktop heap.
-l Allocate the specified amount of large pages (rounded to large
size multiple.
-m Leak memory in specified MBs (default is 1).
-p Create processes - add -n to set min working set. Add -n to
set min working set of processes to smallest.
-r Reserve memory in specified MBs (default is 1).
-s Leak shared memory in specified MBs (default is 1).
-t Create threads - add -n to specify minimum stack reserve (in KB).
-u Create USER handles to menus.
-v VirtualLock memory in specified MBs (default is 1).
-w Reset working set minimum to highest possible value.
进行内存压力测试
shell
Testlimit.exe -v 512 -p 10

