2

I have a service that monitors how much CPU a process is using and alarms when the process is above a certain threshold.

I need to test to see that the monitor is detecting that the service is using a large amount of CPU cycles.

Is there any way to force a process to show a high amount of CPU usage without having access to the source code on the process?

I would eventually like to have a testing application that could spike the CPU of any process.

Leighner
  • 193
  • 1
  • 11
  • You can use a algorithm like one that calculates primes or pi multithreaded. – online Thomas Oct 15 '15 at 14:28
  • Is your question to let an arbitrary process show a high CPU usage? That is not trivial. You can use a tool that does stress the CPU though, as explained in http://blogs.msdn.com/b/vijaysk/archive/2012/10/27/tools-to-simulate-cpu-memory-disk-load.aspx – CodeCaster Oct 15 '15 at 14:28
  • What about just creating a fake process and monitoring that? – Lasse V. Karlsen Oct 15 '15 at 14:28
  • I need to be able to make another process appear to be using high CPU. – Leighner Oct 15 '15 at 14:29
  • Are you really sure you need that? Can't you point your monitoring software at a bogus process that was made for this specifically? – CodeCaster Oct 15 '15 at 14:29
  • If you don't need to write the code yourself you could try benchmarking software like https://www.cpubenchmark.net/ – online Thomas Oct 15 '15 at 14:31
  • Okay, I'll try to just simulate it on a different process, it doesnt sound like an easy task – Leighner Oct 15 '15 at 14:31
  • see here: http://stackoverflow.com/questions/2514544/simulate-steady-cpu-load-and-spikes –  Oct 15 '15 at 14:32
  • When I need to check software against high CPU load I usually use [Mersenne Prime Generator](http://www.mersenne.org/download/). This attempts to calculate next Mersenne Prime number and if you set it to high CPU and high Memory usage you can kill your machine for other processes. I'd think that was a fairly good test for your service. – Stephen Ross Oct 15 '15 at 14:35
  • Is it possible you could do this by (using automation to simulate) using the target application? It won't work for an arbitrarily chosen process, but it sounds like this might be what you really want to do. Also consider doing some unit testing: if you simulate the response from however you get the list of process CPU usages, you can write automated tests that are much better and more reliable than hackily setting CPU percentages. – 31eee384 Oct 15 '15 at 15:35
  • Write a dumb native DLL that in `case` of `DLL_PROCESS_ATTACH` perform some operation that takes all the CPU, then inject it to the target process – Matteo Umili Oct 15 '15 at 15:43

1 Answers1

6

If you are using multi core processor Use ThreadPool to spam threads. spam as number of processor should be enough to make cpu almost 100% load.

private static void Main()
{
    for (int i = 0; i < Environment.ProcessorCount; i++)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
    }

    Console.ReadLine();
}

static void ThreadProc(Object stateInfo)
{
    while (true)
    {
       // do something
    }
}
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118