C #: creating CPU usage in user percentage

I am looking for a system health check, etc. on several machines under certain conditions of CPU usage. Unfortunately, I can only create 100% utilization (infinite loop) or not enough processor utilization (I use C #).

Is there any way, in a rough approximation, as other tasks are also performed in the system, for the artificial use of the processor in increments of 20, 30, 40% (etc.)?

I understand that there are differences between systems, obviously, because the processor is different. This is more about algorithms / ideas for custom computations with intensive computation, which create sufficient use on the current processor without increasing it so that I can configure them, and then somehow configure them to create the desired percentage.

+6
performance c #
source share
6 answers

You can add a multi-threaded timer that wakes up at intervals and does some work. Then adjust the interval and amount of work until you get closer to the desired load.

private void button1_Click(object sender, EventArgs e) { m_timer = new Timer(DoWork); m_timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(10)); } private static void DoWork(object state) { long j = 0; for (int i = 0; i < 2000000; i++) { j += 1; } Console.WriteLine(j); } 

alt text

With this and setting the cycle value, I was able to add 20%, 60% and full load to my system. It will scale for multiple cores, using additional threads for more uniform loading.

+4
source share

It means?

  DateTime lastSleep = DateTime.Now; while (true) { TimeSpan span = DateTime.Now - lastSleep; if (span.TotalMilliseconds > 700) { Thread.Sleep(300); lastSleep = DateTime.Now; } } 

You can use smaller numbers to get a more stable load ... as long as the ratio is what you want. This uses only one core, so you may need to do this in multiple threads.

+5
source share

Not done, but you can try to develop priority threads running in several programs.

0
source share

When you tried to use a dream, did you do it that way or just put the dream in the actual cycle? I think it will work.

 while(true) { Thread.Sleep(0); for(int i=0;i<veryLargeNumber; i++) { //maybe add more loops here if looping to veryLargeNumber goes to quickly //also maybe add some dummy operation so the compiler doesn't optimize the loop away } } 
0
source share

Here is a function that uses all available processors / cores up to a custom percentage and can be canceled at any time using the calling code.

 private static CancellationTokenSource StressCPU(int percent) { if (percent < 0 || percent > 100) throw new ArgumentException(nameof(percent)); var cts = new CancellationTokenSource(); for (int i = 0; i < Environment.ProcessorCount; i++) { new Thread(() => { var stopwatch = new Stopwatch(); while (!cts.IsCancellationRequested) { stopwatch.Restart(); while (stopwatch.ElapsedMilliseconds < percent) { } // hard work Thread.Sleep(100 - percent); // chill out } }).Start(); } return cts; } 

Usage example:

 var cts = StressCPU(50); Thread.Sleep(15000); cts.Cancel(); 

Result:

CPU usage

0
source share

All Articles