0

Right now I have a service that reacts to changes in CPU usage on a server, and I need to write a test to ensure that it will work. The task that I have been handed down from on high is that I need to be able to tell the test program "consume X% of the CPU of the machine you're running on" and have it consume that much CPU indefinitely (or, barring that, for a specific period of time).

It's easy enough to just consume as much CPU as I can, like so:

var n = 0
for i in 0..<5_000_000_000 {
  n += 1
}

But beyond that I'm drawing a blank. Has anyone even tried this before? Are we engaged in a fool's errand?

EDIT: This would be done on Linux (possibly in a container) and written in Swift.

TheSoundDefense
  • 6,753
  • 1
  • 30
  • 42
  • 1
    I'm not an expert but i doubt there is a non-calibrated way without reading out system/os-stats and auto-calibrating as this will be dependent on the compiler / interpreter and cpu-instructions available. – sascha Jan 23 '17 at 21:40
  • What platform / OS? What language? – Suma Jan 23 '17 at 21:40
  • 2
    See https://stackoverflow.com/questions/14231322/simulating-cpu-load-in-c or https://stackoverflow.com/questions/2514544/simulate-steady-cpu-load-and-spikes or https://stackoverflow.com/questions/25101370/simulate-high-cpu-load-for-java-development – Suma Jan 23 '17 at 21:43
  • @Suma thanks! That second link looks like it might be just what I need. – TheSoundDefense Jan 23 '17 at 21:46

0 Answers0