2013-11-15 102 views
6

我正在進行當前的硬件溫度測試,我在想如何做最大性能任務以保持設備的所有4個內核都處於繁忙狀態,以便測量峯值溫度?如何強制最大CPU使用率

我當然可以啓動n個無限循環的線程,但我認爲可能有更好的方法來解決這個問題。

while (true) { 
     try { 
      new Thread() { 
       public void run() { 
        while (true) { 
         try { 
          Runtime.getRuntime().exec("ps"); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 
       } 
      }.start(); 
     } catch (Error e) { 
      // typically will be OutOfMemoryerror during Thread alloc 
     } 
    } 

而且在你的清單:

<application 
    android:largeHeap="true" 
... 

adb shell top

User 99%, System 0%, IOW 0%, IRQ 0% 
User 1216 + Nice 0 + Sys 4 + Idle 0 + IOW 0 + IRQ 0 + SIRQ 0 = 1220 

PID PR CPU% S #THR  VSS  RSS PCY UID  Name 
3534 0 99% S 1292 1990880K 32784K fg u0_a56 com.myapp.cpupressure 

但它還是不如將AnTuTu的穩定性,有效:

temp curve

+0

您需要將線程數限制爲有限數量,否則您的應用將耗盡所有內存然後崩潰。 –

+0

是的,只是看到了,並會編輯我的問題 – bluewhile

回答

1

你可以嘗試這個多核測試,它會耗盡所有內核。並且可以根據需要超載。

public class MultiCore { 
    private static final int SPIN_COUNT = 2000; 

    public static void main(String[] args) { 
     int numThreads = 4; 
     if (args.length == 1) { 
      numThreads = Integer.valueOf(args[0]); 
     } 

     System.out.println("Starting " + numThreads + " threads"); 
     long startWhen = System.nanoTime(); 

     SpinThread threads[] = new SpinThread[numThreads]; 
     for (int i = 0; i < numThreads; i++) { 
      threads[i] = new SpinThread(i); 
      threads[i].start(); 
     } 

     for (int i = 0; i < numThreads; i++) { 
      try { 
       threads[i].join(); 
      } catch (InterruptedException ie) { 
       System.err.println("join " + i + " failed: " + ie); 
      } 
     } 

     long endWhen = System.nanoTime(); 
     System.out.println("All threads finished in " + 
      ((endWhen - startWhen)/1000000) + "ms"); 
    } 

    static class SpinThread extends Thread { 
     private int mTid; 

     SpinThread(int tid) { 
      mTid = tid; 
     } 

     public void run() { 
      long startWhen = System.nanoTime(); 
      System.out.println("Thread " + mTid + " started"); 
      int tid = mTid; 
      int reps = SPIN_COUNT + tid; 
      int ret = 0; 

      for (int i = 0; i < reps; i++) { 
       for (int j = 0; j < 100000; j++) { 
        ret += i * j; 
       } 
      } 

      long endWhen = System.nanoTime(); 
      System.out.println("Thread " + mTid + " finished in " + 
       ((endWhen - startWhen)/1000000) + "ms (" + ret + ")"); 
     } 
    } 
}