2010-03-25 53 views
35

如何在C#中產生穩定的CPU負載,在特定時間內低於100%?我也希望能夠在一段時間後改變負載量。你如何建議在很短的時間內產生使用率峯值?模擬穩定的CPU負載和峯值

回答

48

首先,您必須瞭解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(); 
     } 
    } 
} 

我使用stopwatch這裏,因爲它比所述TickCount更準確財產,但你也可以使用它並使用減法來檢查你是否跑得夠長。

兩件事情要記住:

  • 多核心繫統,你將有產卵一個線程每個核心。否則,你會看到只有一個CPU /內核正在執行,大致「百分比/內核數」利用率。
  • Thread.Sleep不是很準確。它將永遠無法保證時間準確到毫秒,所以你會看到你的結果

一些變化要回答你的第二個問題,關於在一段時間後更改利用率,我建議你在一個或多個線程運行該方法(取決於內核的數量),然後當你想改變利用率時,你只需停止這些線程並用新的百分比值產生新的線程。這樣,您不必實現線程通信就可以更改正在運行的線程的percentage

13

只是在伊薩克響應的補充,我讓這裏簡單的實現對多核:

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

我用你的函數,它幾乎工作。 8個內核中有7個處於期望的級別,然後我將「for(int i = 0; i Joshi 2016-02-03 14:59:09

0

每次必須設置cpuUsageIncreaseby可變時間。例如: 例如:

1- CPU增加> cpuUsage由%增加一分鐘。 2-下降到20%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); 
       } 
      } 
     } 
1

一個統一的強調:伊薩克薩沃的答案有輕微的調整

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