Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to artificially load CPUs to a certain percentage point?

For the purpose of testing my app (that detects the current CPU usage) I need to set an artificial work load on the CPUs, for example: 10%, 20%, 30%, etc. up to a 100%. Is there any way to do this?

like image 588
c00000fd Avatar asked Oct 01 '22 15:10

c00000fd


1 Answers

Use any cpu-bound code you want, then wax-on, wax-off.

In other words,

to achieve 50% load, run the cpu-bound code for x ms, then do nothing for x ms.

to achieve other loads, well, you do the math.

doing nothing is easy, sleep, pause, delay, etc. if c++ 11, try threads and

   std::this_thread::sleep_for (std::chrono::milliseconds(100));
like image 50
2785528 Avatar answered Oct 18 '22 08:10

2785528