我想知道是否有人用 python 编写了模拟 cpu 和内存压力的代码。 我看到加载 CPU 的代码,但我如何才能强制它们在 90% 的使用率下工作?
最佳答案
一个节点主要有4种经常使用的资源-
- 可用内存
- CPU 周期
- 存储空间
- 网络负载(上传和下载)
这就是 Windows 上的任务管理器或 macOS 上的事件监视器(或 *nix 系统上的顶级免费命令)的原因。
如果需要特定的 python 解决方案,那么我会推荐 stress 和 stressypy 模块。这是链接- https://pypi.org/project/stress/ https://pypi.org/project/stressypy/
简单的 pip 安装就可以完成这项工作。
但就个人而言,我是 stress-ng 应用程序的粉丝。您可以轻松安装上述所有资源并将所需的负载放在上面 在这里,找到它- https://www.mankier.com/1/stress-ng
引用这些例子- https://www.mankier.com/1/stress-ng#Examples
stress-ng --cpu 4 --io 2 --vm 1 --vm-bytes 1G --timeout 60s
runs for 60 seconds with 4 cpu stressors, 2 io stressors and 1 vm stressor using 1GB of virtual memory.
stress-ng --iomix 2 --iomix-bytes 10% -t 10m
runs 2 instances of the mixed I/O stressors using a total of 10% of the available file system space for 10 minutes. Each stressor will use 5% of the available file system space.
stress-ng --cpu 8 --cpu-ops 800000
runs 8 cpu stressors and stops after 800000 bogo operations.
stress-ng --sequential 2 --timeout 2m --metrics
run 2 simultaneous instances of all the stressors sequentially one by one, each for 2 minutes and summarise with performance metrics at the end.
关于python - 如何在 python 中模拟 cpu 和内存压力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35312756/