Leighner
Leighner

Reputation: 193

How to to force a process to have high CPU

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.

Upvotes: 2

Views: 2775

Answers (1)

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19149

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
    }
}

Upvotes: 6

Related Questions