Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate steady CPU load and spikes

Tags:

c#

cpu-usage

How could I generate steady CPU load in C#, lower than 100% for a certain time? I would also like to be able to change the load amount after a certain period of time. How do you recommend to generate usage spikes for a very short time?

like image 846
kjv Avatar asked Mar 25 '10 10:03

kjv


People also ask

How do I spike my CPU?

Now, in order to create a spike in the cpu we have different ways that we can use. The first and the most common way is to make use of the stress command. The stress command is used along with the number of cpu cores and / or the timeout for which you want the cores to max out.

Why does my CPU load keep spiking?

One common culprit for CPU spikes is a virus, spyware or other form of harmful software that is busy using your CPU. This might be the case if you see a process on the Processes tab in the Task Manager that you don't recognize.

How do I simulate high CPU usage Windows?

Open Task Manager and go to the Performance tab to monitor the CPU load. Right click the CPU graph on the right and select "Change graph to -> Logical processors". Double click the loop. vbs script you created to execute it.


2 Answers

First off, you have to understand that CPU usage is always an average over a certain time. At any given time, the CPU is either working or it is not. The CPU is never 40% working.

We can, however, simulate a 40% load over say a second by having the CPU work for 0.4 seconds and sleep 0.6 seconds. That gives an average utilization of 40% over that second.

Cutting it down to smaller than one second, say 100 millisecond chunks should give even more stable utilization.

The following method will take an argument that is desired utilization and then utilize a single CPU/core to that degree:

public static void ConsumeCPU(int percentage) {     if (percentage < 0 || percentage > 100)         throw new ArgumentException("percentage");     Stopwatch watch = new Stopwatch();     watch.Start();                 while (true)     {         // Make the loop go on for "percentage" milliseconds then sleep the          // remaining percentage milliseconds. So 40% utilization means work 40ms and sleep 60ms         if (watch.ElapsedMilliseconds > percentage)         {             Thread.Sleep(100 - percentage);             watch.Reset();             watch.Start();         }     } } 

I'm using a stopwatch here because it is more accurate than the the TickCount property, but you could likewise use that and use subtraction to check if you've run long enough.

Two things to keep in mind:

  • on multi core systems, you will have to spawn one thread for each core. Otherwise, you'll see only one CPU/core being exercised giving roughly "percentage/number-of-cores" utilization.
  • Thread.Sleep is not very accurate. It will never guarantee times exactly to the millisecond so you will see some variations in your results

To answer your second question, about changing the utilization after a certain time, I suggest you run this method on one or more threads (depending on number of cores) and then when you want to change utilization you just stop those threads and spawn new ones with the new percentage values. That way, you don't have to implement thread communication to change percentage of a running thread.

like image 73
Isak Savo Avatar answered Sep 28 '22 03:09

Isak Savo


Just in add of the Isak response, I let here a simple implementation for multicore:

 public static void CPUKill(object cpuUsage)     {         Parallel.For(0, 1, new Action<int>((int i) =>         {             Stopwatch watch = new Stopwatch();             watch.Start();             while (true)             {                 if (watch.ElapsedMilliseconds > (int)cpuUsage)                 {                     Thread.Sleep(100 - (int)cpuUsage);                     watch.Reset();                     watch.Start();                 }             }         }));      }      static void Main(string[] args)     {         int cpuUsage = 50;         int time = 10000;         List<Thread> threads = new List<Thread>();         for (int i = 0; i < Environment.ProcessorCount; i++)         {             Thread t = new Thread(new ParameterizedThreadStart(CPUKill));             t.Start(cpuUsage);             threads.Add(t);         }         Thread.Sleep(time);         foreach (var t in threads)         {             t.Abort();         }    } 
like image 26
Guido Zanon Avatar answered Sep 28 '22 02:09

Guido Zanon