如何在python中模拟cpu和内存压力

adi*_*ib1 6 python cpu

我想知道是否有人用python编写了模拟cpu和内存压力的代码。我看到了一个加载 cpu 的代码,但是如何强制它们以 90% 的使用率工作?

小智 7

一个节点主要有 4 种经常使用的资源 -

  • 有效内存
  • 中央处理器周期
  • 储存空间
  • 网络负载(上传和下载)

这就是 Windows 上的任务管理器或 macOS 上的活动监视器(或 *nix 系统上的顶级免费命令)的原因。

如果需要特定的Python解决方案,那么我会推荐stress和stressypy模块。以下是链接 - https://pypi.org/project/stress/ https://pypi.org/project/stressypy/

简单的 pip install 就可以完成工作。

但就我个人而言,我很喜欢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.
Run Code Online (Sandbox Code Playgroud)