Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I simulate cpu and memory stress in python

Tags:

python

cpu

I would like to know if anyone wrote a code in python that simulates a cpu and memory stress. I saw a code loading the cpus, but how can I force them to work on 90% usage?

like image 474
adib1 Avatar asked Feb 10 '16 10:02

adib1


People also ask

How do I see CPU and RAM usage in Python?

The psutil. getloadavg() provides the load information of the CPU in the form of a tuple. The psutil. getloadavg() runs in the background and the results get updated every 5 seconds.

How do I eat memory in Python?

For example, you can use an 8-fold higher value for multiplier_per_allocation . INFO:Allocating cumulative instances of 1,073,741,824 bytes (1 GiB) each. Each allocation uses 1,024 unique bytes (1 KiB) with a multiplier of 1,048,576 (1 MiB). INFO:Used a total of 1,073,741,824 bytes (1 GiB) via 1 allocations.

What is CPU in Python?

In Python, single-CPU use is caused by the global interpreter lock (GIL), which allows only one thread to carry the Python interpreter at any given time. The GIL was implemented to handle a memory management issue, but as a result, Python is limited to using a single processor.


1 Answers

A node has mainly 4 resources that are under constant use -

  • available memory
  • cpu cycles
  • storage space
  • network load (uploads & downloads)

That is why the task manager on Windows or Activity Monitor on macOS (or top, free commands on *nix systems).

If a specific python solution is required, then I would recommend stress and stressypy modules. Here are the links- https://pypi.org/project/stress/ https://pypi.org/project/stressypy/

simple pip install would do the work.

But personally, I am a fan of stress-ng application. You can easily install and put the required loads on all the above resources Here, find it- https://www.mankier.com/1/stress-ng

Refer to these examples- 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.
like image 73
Mohammad Shahid Siddiqui Avatar answered Sep 21 '22 14:09

Mohammad Shahid Siddiqui