5

I am currently working on a little in-house testing app to create multiple types of load on the computer so that we can see how our software works under load. I am getting hung up on one request, in that I make some sort of way to specify a percent usage of the CPU (say, 40%) before the user runs the program, and that will hopefully keep the cpu in a range +-10% of what they put in. I've been told that they had seen programs doing it before, but every one that I had seen that claimed to do so did not work. I am attempting to read an average usage and sleep the thread running so that the CPU usage lowers, but I have found that is not accurate. It is also going to be used on multiple servers, each with different CPUs, so I can't do very much hard coding as it will need to work on each server. I personally do not think its possible to accurately do, but I'm asking here as a last ditch effort to see if anyone else has come across software similar to this, and knows how it would be written.

Edit: I have attempted to use a lower priority thread, and was also told that would not be useful to them (though I had the option created just for the purpose of testing before this was requested of me).

wgallon
  • 57
  • 4
  • I'm not aware of any way to cap how much CPU power you can consume, that is the task of the OS, determine the scheduling; other than limiting how many cores it can use. What you can do is alter the priority of the process, I was also going to mention sleeping it, but you've already noted that that is an imperfect solution. – 0_______0 Aug 17 '12 at 19:22

2 Answers2

3

We did something analogous a while back tuning an application server. We found that our overall throughput was best when the CPU was around 70%. Our solution was to spin up additional threads, with a delay between each one, until the desired load was reached. If CPU went above 80%, we reduced the number of running threads (signaled a thread to finish its current work and then terminate). If CPU dropped below 60%, we fired up a new thread.

You could apply similar logic to create artificial load that you can tune to a target CPU utilization. Your threads would need to balance CPU and non-CPU (IO, or just sleep) in order to allow for fine tuning. If your threads are very CPU intensive, each will consume 1 CPU core to about 100%.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • If you don't mind me asking, what did your thread look like? Currently I just have mine doing a constant addition and subtraction in an infinite loop, and that eats up an entire core on its own (though the machine I'm working on at the moment isn't very powerful) – wgallon Aug 17 '12 at 19:46
  • @BillyGallon, Infinite CPU bound loop will eat 100% of a core on any machine (unless OS level scheduling will not schedule that thread either due to priority or other limits i.e. as suggested by usr) – Alexei Levenkov Aug 17 '12 at 20:15
  • The threads in our case did real-world work, selecting data from a DB table and then performing calculations on it. I suspect that interspersing Thread.Sleep calls (sleep for random but fairly short intervals) with CPU intensive calculations should allow you to create a thread that averages modest CPU utilization so that you have reasonable ability to control the overall load generated by a number of threads. – Eric J. Aug 20 '12 at 04:03
2

Starting with Windows 8 you can make the OS scheduler cap the CPU usage using Windows Jobs.

usr
  • 168,620
  • 35
  • 240
  • 369