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

how can I simulate cpu and memory stress in python

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

下面link回答了cpu的压力。 https://github.com/GaetanoCarlucci/CPULoadGenerator

它出现在那个会话中: https://superuser.com/questions/396501/how-can-i-produce-high-cpu-load-on-windows/734782#734782?newreg=bec18b2f032a444187a8be7540ec6083

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

  • 可用内存
  • cpu 周期
  • 存储space
  • 网络负载(上传和下载)

这就是 Windows 上的任务管理器或 Activity 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.