模拟稳定的CPU负载和尖峰

我怎么能在C#中产生稳定的CPU负载,在一定的时间内低于100%? 我也想在一段时间后能够改变负载量。 您如何build议在很短的时间内产生使用率峰值?

首先,您必须了解CPU使用率在一定时间内总是平均值。 在任何时候,CPU都在工作,或者没有工作。 CPU从来没有40%的工作。

然而,我们可以通过让CPU工作0.4秒,睡眠0.6秒来模拟40%的负载。 那平均利用率为40%。

把它缩小到一秒以内,比如说100毫秒的块应该给予更加稳定的利用。

下面的方法将采取一个理想的利用率,然后利用一个单一的CPU /核心:

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(); } } } 

我在这里使用秒表 ,因为它比TickCount属性更精确,但是您也可以使用它并使用减法来检查您是否已经运行了足够长的时间。

需要牢记两点:

  • 在多核心系统上,你将不得不为每个核心产生一个线程。 否则,你会看到只有一个CPU /内核正在执行,大致“百分比/内核数”利用率。
  • Thread.Sleep不是很准确。 它不会保证精确到毫秒,所以你会看到你的结果中的一些变化

要回答第二个问题,关于在一段时间之后改变利用率,我build议你在一个或多个线程上运行这个方法(取决于核心的数量),然后当你想改变利用率时,你只需停止这些线程并产生新的线程与新的百分比值。 这样,您不必实现线程通信来更改正在运行的线程的percentage

加上Isak的回应,我在这里给出了一个简单的多核实现:

  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(); } } 

每次你必须设置cpuUsageIncreasebyvariables。 例如:

1- Cpu%增加> cpuUsage由%增加一分钟。 2-下降到0%20秒。 3-转到步骤1。

  private void test() { int cpuUsageIncreaseby = 10; while (true) { for (int i = 0; i < 4; i++) { //Console.WriteLine("am running "); //DateTime start = DateTime.Now; int cpuUsage = cpuUsageIncreaseby; int time = 60000; // duration for cpu must increase for process... List<Thread> threads = new List<Thread>(); for (int j = 0; j < Environment.ProcessorCount; j++) { Thread t = new Thread(new ParameterizedThreadStart(CPUKill)); t.Start(cpuUsage); threads.Add(t); } Thread.Sleep(time); foreach (var t in threads) { t.Abort(); } //DateTime end = DateTime.Now; //TimeSpan span = end.Subtract(start); //Console.WriteLine("Time Difference (seconds): " + span.Seconds); //Console.WriteLine("10 sec wait... for another."); cpuUsageIncreaseby = cpuUsageIncreaseby + 10; System.Threading.Thread.Sleep(20000); } } } 

对于统一的压力:Isak Savo的回答略有调整

 int percentage = 80; for (int i = 0; i < Environment.ProcessorCount; i++) { (new Thread(() = > { 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(); } } })).Start(); }