Search in sources :

Example 6 with InstrumentationTestRunner

use of android.test.InstrumentationTestRunner in project android_frameworks_base by crdroidandroid.

the class AppLaunch method testMeasureStartUpTime.

public void testMeasureStartUpTime() throws RemoteException, NameNotFoundException, IOException, InterruptedException {
    InstrumentationTestRunner instrumentation = (InstrumentationTestRunner) getInstrumentation();
    Bundle args = instrumentation.getArguments();
    mAm = ActivityManagerNative.getDefault();
    String launchDirectory = args.getString(KEY_LAUNCH_DIRECTORY);
    mTraceDirectoryStr = args.getString(KEY_TRACE_DIRECTORY);
    mDropCache = Boolean.parseBoolean(args.getString(KEY_DROP_CACHE));
    mSimplePerfCmd = args.getString(KEY_SIMPLEPPERF_CMD);
    mLaunchOrder = args.getString(KEY_LAUNCH_ORDER, LAUNCH_ORDER_CYCLIC);
    createMappings();
    parseArgs(args);
    checkAccountSignIn();
    // Root directory for applaunch file to log the app launch output
    // Will be useful in case of simpleperf command is used
    File launchRootDir = null;
    if (null != launchDirectory && !launchDirectory.isEmpty()) {
        launchRootDir = new File(launchDirectory);
        if (!launchRootDir.exists() && !launchRootDir.mkdirs()) {
            throw new IOException("Unable to create the destination directory");
        }
    }
    try {
        File launchSubDir = new File(launchRootDir, LAUNCH_SUB_DIRECTORY);
        if (!launchSubDir.exists() && !launchSubDir.mkdirs()) {
            throw new IOException("Unable to create the lauch file sub directory");
        }
        mFile = new File(launchSubDir, LAUNCH_FILE);
        mOutputStream = new FileOutputStream(mFile);
        mBufferedWriter = new BufferedWriter(new OutputStreamWriter(mOutputStream));
        // Root directory for trace file during the launches
        File rootTrace = null;
        File rootTraceSubDir = null;
        int traceBufferSize = 0;
        int traceDumpInterval = 0;
        Set<String> traceCategoriesSet = null;
        if (null != mTraceDirectoryStr && !mTraceDirectoryStr.isEmpty()) {
            rootTrace = new File(mTraceDirectoryStr);
            if (!rootTrace.exists() && !rootTrace.mkdirs()) {
                throw new IOException("Unable to create the trace directory");
            }
            rootTraceSubDir = new File(rootTrace, TRACE_SUB_DIRECTORY);
            if (!rootTraceSubDir.exists() && !rootTraceSubDir.mkdirs()) {
                throw new IOException("Unable to create the trace sub directory");
            }
            assertNotNull("Trace iteration parameter is mandatory", args.getString(KEY_TRACE_ITERATIONS));
            mTraceLaunchCount = Integer.parseInt(args.getString(KEY_TRACE_ITERATIONS));
            String traceCategoriesStr = args.getString(KEY_TRACE_CATEGORY, DEFAULT_TRACE_CATEGORIES);
            traceBufferSize = Integer.parseInt(args.getString(KEY_TRACE_BUFFERSIZE, DEFAULT_TRACE_BUFFER_SIZE));
            traceDumpInterval = Integer.parseInt(args.getString(KEY_TRACE_DUMPINTERVAL, DEFAULT_TRACE_DUMP_INTERVAL));
            traceCategoriesSet = new HashSet<String>();
            if (!traceCategoriesStr.isEmpty()) {
                String[] traceCategoriesSplit = traceCategoriesStr.split(DELIMITER);
                for (int i = 0; i < traceCategoriesSplit.length; i++) {
                    traceCategoriesSet.add(traceCategoriesSplit[i]);
                }
            }
        }
        // Get the app launch order based on launch order, trial launch,
        // launch iterations and trace iterations
        setLaunchOrder();
        for (LaunchOrder launch : mLaunchOrderList) {
            // launch time calculations.
            if (launch.getLaunchReason().equals(TRIAL_LAUNCH)) {
                // In the "applaunch.txt" file, trail launches is referenced using
                // "TRIAL_LAUNCH"
                long launchTime = startApp(launch.getApp(), true, launch.getLaunchReason());
                if (launchTime < 0) {
                    List<Long> appLaunchList = new ArrayList<Long>();
                    appLaunchList.add(-1L);
                    mNameToLaunchTime.put(launch.getApp(), appLaunchList);
                    // error should have already been logged by startApp
                    continue;
                }
                sleep(INITIAL_LAUNCH_IDLE_TIMEOUT);
                closeApp(launch.getApp(), true);
                dropCache();
                sleep(BETWEEN_LAUNCH_SLEEP_TIMEOUT);
            }
            // App launch times used for final calculation
            if (launch.getLaunchReason().contains(LAUNCH_ITERATION_PREFIX)) {
                long launchTime = -1;
                if (null != mNameToLaunchTime.get(launch.getApp())) {
                    long firstLaunchTime = mNameToLaunchTime.get(launch.getApp()).get(0);
                    if (firstLaunchTime < 0) {
                        // skip if the app has failures while launched first
                        continue;
                    }
                }
                // In the "applaunch.txt" file app launches are referenced using
                // "LAUNCH_ITERATION - ITERATION NUM"
                launchTime = startApp(launch.getApp(), true, launch.getLaunchReason());
                if (launchTime < 0) {
                    // if it fails once, skip the rest of the launches
                    List<Long> appLaunchList = new ArrayList<Long>();
                    appLaunchList.add(-1L);
                    mNameToLaunchTime.put(launch.getApp(), appLaunchList);
                    continue;
                } else {
                    if (null != mNameToLaunchTime.get(launch.getApp())) {
                        mNameToLaunchTime.get(launch.getApp()).add(launchTime);
                    } else {
                        List<Long> appLaunchList = new ArrayList<Long>();
                        appLaunchList.add(launchTime);
                        mNameToLaunchTime.put(launch.getApp(), appLaunchList);
                    }
                }
                sleep(POST_LAUNCH_IDLE_TIMEOUT);
                closeApp(launch.getApp(), true);
                dropCache();
                sleep(BETWEEN_LAUNCH_SLEEP_TIMEOUT);
            }
            // launch time calculations.
            if (launch.getLaunchReason().contains(TRACE_ITERATION_PREFIX)) {
                AtraceLogger atraceLogger = AtraceLogger.getAtraceLoggerInstance(getInstrumentation());
                // Start the trace
                try {
                    atraceLogger.atraceStart(traceCategoriesSet, traceBufferSize, traceDumpInterval, rootTraceSubDir, String.format("%s-%s", launch.getApp(), launch.getLaunchReason()));
                    startApp(launch.getApp(), true, launch.getLaunchReason());
                    sleep(POST_LAUNCH_IDLE_TIMEOUT);
                } finally {
                    // Stop the trace
                    atraceLogger.atraceStop();
                    closeApp(launch.getApp(), true);
                    dropCache();
                    sleep(BETWEEN_LAUNCH_SLEEP_TIMEOUT);
                }
            }
        }
    } finally {
        if (null != mBufferedWriter) {
            mBufferedWriter.close();
        }
    }
    for (String app : mNameToResultKey.keySet()) {
        StringBuilder launchTimes = new StringBuilder();
        for (Long launch : mNameToLaunchTime.get(app)) {
            launchTimes.append(launch);
            launchTimes.append(",");
        }
        mResult.putString(mNameToResultKey.get(app), launchTimes.toString());
    }
    instrumentation.sendStatus(0, mResult);
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) AtraceLogger(android.support.test.rule.logging.AtraceLogger) InstrumentationTestRunner(android.test.InstrumentationTestRunner) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Aggregations

Bundle (android.os.Bundle)6 InstrumentationTestRunner (android.test.InstrumentationTestRunner)6 File (java.io.File)5 AtraceLogger (android.support.test.rule.logging.AtraceLogger)4 BufferedWriter (java.io.BufferedWriter)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4 OutputStreamWriter (java.io.OutputStreamWriter)4 ArrayList (java.util.ArrayList)4 LegacyVpnInfo (com.android.internal.net.LegacyVpnInfo)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1