Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0daaaaf3 authored by David Sehr's avatar David Sehr Committed by Android (Google) Code Review
Browse files

Merge "Add a threaded CPU-intensive benchmark to test"

parents a486c752 decc2b71
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
android_app {
    name: "startop_test_app",
    srcs: [
        "src/CPUIntensive.java",
        "src/EmptyActivity.java",
        "src/LayoutInflationActivity.java",
        "src/ComplexLayoutInflationActivity.java",
+67 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 *  A threaded CPU intensive class for use in benchmarks.
 */

package com.android.startop.test;

final class CPUIntensive {
    public static final int THREAD_COUNT = 8;
    public static final int ARRAY_SIZE = 30000;
    public static int[][] array = new int[THREAD_COUNT][ARRAY_SIZE];

    static class WorkerThread extends Thread {
        int mThreadNumber;
        WorkerThread(int number) {
            mThreadNumber = number;
        }
        public void run() {
            final int arrayLength = array[mThreadNumber].length;
            for (int i = 0; i < arrayLength; ++i) {
                array[mThreadNumber][i] = i * i;
            }
            for (int i = 0; i < arrayLength; ++i) {
                for (int j = 0; j < arrayLength; ++j) {
                    int swap = array[mThreadNumber][j];
                    array[mThreadNumber][j] = array[mThreadNumber][(j + i) % arrayLength];
                    array[mThreadNumber][(j + i) % arrayLength] = swap;
                }
            }
        }
    };

    public static void doSomeWork(int threadCount) {
        WorkerThread[] threads = new WorkerThread[threadCount];
        // Create the threads.
        for (int i = 0; i < threadCount; ++i) {
            threads[i] = new WorkerThread(i);
        }
        // Start the threads.
        for (int i = 0; i < threadCount; ++i) {
            threads[i].start();
        }
        // Join the threads.
        for (int i = 0; i < threadCount; ++i) {
            try {
                threads[i].join();
            } catch (Exception ex) {
            }
        }
    }
}
+16 −4
Original line number Diff line number Diff line
@@ -26,15 +26,11 @@ import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Trace;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;

import java.util.Arrays;

class Benchmark {
    // Time limit to run benchmarks in seconds
@@ -107,6 +103,22 @@ public class SystemServerBenchmarkActivity extends Activity {
        new Benchmark(benchmarkList, "Empty", () -> {
        });

        new Benchmark(benchmarkList, "CPU Intensive (1 thread)", () -> {
            CPUIntensive.doSomeWork(1);
        });

        new Benchmark(benchmarkList, "CPU Intensive (2 thread)", () -> {
            CPUIntensive.doSomeWork(2);
        });

        new Benchmark(benchmarkList, "CPU Intensive (4 thread)", () -> {
            CPUIntensive.doSomeWork(4);
        });

        new Benchmark(benchmarkList, "CPU Intensive (8 thread)", () -> {
            CPUIntensive.doSomeWork(8);
        });

        PackageManager pm = getPackageManager();
        new Benchmark(benchmarkList, "getInstalledApplications", () -> {
            pm.getInstalledApplications(PackageManager.MATCH_SYSTEM_ONLY);