how can I simulate cpu and memory stress in python

21,254

Solution 1

The below link answers the cpu stress. https://github.com/GaetanoCarlucci/CPULoadGenerator

It appeared in that session: https://superuser.com/questions/396501/how-can-i-produce-high-cpu-load-on-windows/734782#734782?newreg=bec18b2f032a444187a8be7540ec6083

Solution 2

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.
Share:
21,254
adib1
Author by

adib1

Updated on June 16, 2020

Comments

  • adib1
    adib1 almost 4 years

    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?

    • Peter pete
      Peter pete over 8 years
      If you're trying to create load on your computer to test other stuff, then you'll probably have to use Python's multiprocess module to spawn new pythons to do some tight-looping. Since a single instance of python only uses 1 cpu. For memory ... allocate a whole bunch? But then your OS may just page it out to disk. So you'd have to write a bunch of memory and keep accessing it.
    • adib1
      adib1 over 8 years
      thanks..about cpu- I wil use pool..however, how can I determine in code that the cpu will have 90% of usage? and..there's not a way to determine arbitrarily to have the memory 90% usage?
    • Peter pete
      Peter pete over 8 years
      If you want exactly 90% CPU and exactly 90% memory. Then I don't think you can do anything to result in that. You could make a program that allocates 90% of its Address Space in memory. But whether that translates to 90% physical memory being used by the OS is a different matter. The OS could choose to page-to-disk parts of the application's address space that are not in use, thus freeing up physical memory. The question really is, what are you trying to achieve?
    • adib1
      adib1 over 8 years
      I want to create a sress on the operating system(cpu and memory on 90-95%) automatically and then stop it and test the performace of my system..however, I saw some porgrams doing that in c#..I would like to know how to do that in python
    • Peter pete
      Peter pete over 8 years
      I see. Well, if you simply want to trash your system. Then you can in Python. Open up many Multiprocessor instances each with a tight loop that does something and something that makes a lot of small allocations in each of them. If you want to exactly target 90% cpu and 90% memory then it'll be tricky no matter what you build it in. There'll be stuff in the internet to find out how to use the multiprocessor module.
    • adib1
      adib1 over 8 years
      fine..thanks a lot for trying to help me!!.. however' I would like to know how can I theoretically create a n% stress on the cpu abd on memory?
    • Peter pete
      Peter pete over 8 years
      Well, maybe if you assume your program will be the only one executing on the processor and if you run one instance of your program per processor and if your program does intense CPU-bound work on n% of milliseconds per second and sleeps for the remainder - then maybe - theoretically - this may achieve what you want. I think in practice it may be bit off since the OS has other things to do, etc, too.