lundi 23 janvier 2017

How to intentionally consume portion of CPU

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.

Aucun commentaire:

Enregistrer un commentaire