如何强制最大CPU使用率

时间:2013-11-15 08:57:02

标签: android performance

我目前正在进行硬件温度测试,我想知道如何进行最高性能任务以保持设备的所有4个内核忙于测量峰值温度?

我当然可以使用无限循环启动n个Thread,但我认为可能有更好的方法来解决这个问题。

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
        }
    }

同样在你的Manifest:

<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

1 个答案:

答案 0 :(得分:2)

您可以尝试这种耗尽所有内核的多核测试。并且可以根据需要超载。

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