Home | History | Annotate | Download | only in dumprendertree
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.dumprendertree;
     18 
     19 import dalvik.system.VMRuntime;
     20 
     21 import android.app.Instrumentation;
     22 import android.content.Intent;
     23 import android.os.Bundle;
     24 import android.os.Debug;
     25 import android.os.Process;
     26 import android.test.ActivityInstrumentationTestCase2;
     27 import android.util.Log;
     28 
     29 import java.io.FileOutputStream;
     30 import java.io.IOException;
     31 import java.io.InputStream;
     32 import java.io.OutputStream;
     33 import java.io.PrintStream;
     34 
     35 public class LoadTestsAutoTest extends ActivityInstrumentationTestCase2<TestShellActivity> {
     36 
     37     private final static String LOGTAG = "LoadTest";
     38     private final static String LOAD_TEST_RESULT = "/sdcard/load_test_result.txt";
     39     private boolean mFinished;
     40     static final String LOAD_TEST_RUNNER_FILES[] = {
     41         "run_page_cycler.py"
     42     };
     43 
     44     public LoadTestsAutoTest() {
     45         super("com.android.dumprendertree", TestShellActivity.class);
     46     }
     47 
     48     // This function writes the result of the layout test to
     49     // Am status so that it can be picked up from a script.
     50     public void passOrFailCallback(String file, boolean result) {
     51         Instrumentation inst = getInstrumentation();
     52         Bundle bundle = new Bundle();
     53         bundle.putBoolean(file, result);
     54         inst.sendStatus(0, bundle);
     55     }
     56 
     57     // Invokes running of layout tests
     58     // and waits till it has finished running.
     59     public void runPageCyclerTest() {
     60         LayoutTestsAutoRunner runner = (LayoutTestsAutoRunner) getInstrumentation();
     61 
     62         if (runner.mTestPath == null) {
     63             Log.e(LOGTAG, "No test specified");
     64             return;
     65         }
     66 
     67         TestShellActivity activity = (TestShellActivity) getActivity();
     68 
     69         Log.v(LOGTAG, "About to run tests, calling gc first...");
     70         freeMem();
     71 
     72         // Run tests
     73         runTestAndWaitUntilDone(activity, runner.mTestPath, runner.mTimeoutInMillis,
     74                 runner.mGetDrawTime, runner.mSaveImagePath);
     75 
     76         activity.clearCache();
     77         try {
     78             Thread.sleep(5000);
     79         } catch (InterruptedException e) {
     80         }
     81         dumpMemoryInfo();
     82 
     83         // Kill activity
     84         activity.finish();
     85     }
     86 
     87     private void freeMem() {
     88         Log.v(LOGTAG, "freeMem: calling gc/finalization...");
     89         final VMRuntime runtime = VMRuntime.getRuntime();
     90 
     91         runtime.gcSoftReferences();
     92         runtime.runFinalizationSync();
     93         runtime.gcSoftReferences();
     94         runtime.runFinalizationSync();
     95         runtime.gcSoftReferences();
     96         runtime.runFinalizationSync();
     97         Runtime.getRuntime().runFinalization();
     98         Runtime.getRuntime().gc();
     99         Runtime.getRuntime().gc();
    100 
    101     }
    102 
    103     private void printRow(PrintStream ps, String format, Object...objs) {
    104         ps.println(String.format(format, objs));
    105     }
    106 
    107     private void dumpMemoryInfo() {
    108         try {
    109             freeMem();
    110             Log.v(LOGTAG, "Dumping memory information.");
    111 
    112             FileOutputStream out = new FileOutputStream(LOAD_TEST_RESULT, true);
    113             PrintStream ps = new PrintStream(out);
    114 
    115             ps.print("\n\n\n");
    116             ps.println("** MEMINFO in pid " + Process.myPid()
    117                     + " [com.android.dumprendertree] **");
    118             String formatString = "%17s %8s %8s %8s %8s";
    119 
    120             long nativeMax = Debug.getNativeHeapSize() / 1024;
    121             long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
    122             long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
    123             Runtime runtime = Runtime.getRuntime();
    124             long dalvikMax = runtime.totalMemory() / 1024;
    125             long dalvikFree = runtime.freeMemory() / 1024;
    126             long dalvikAllocated = dalvikMax - dalvikFree;
    127 
    128 
    129             Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
    130             Debug.getMemoryInfo(memInfo);
    131 
    132             final int nativeShared = memInfo.nativeSharedDirty;
    133             final int dalvikShared = memInfo.dalvikSharedDirty;
    134             final int otherShared = memInfo.otherSharedDirty;
    135 
    136             final int nativePrivate = memInfo.nativePrivateDirty;
    137             final int dalvikPrivate = memInfo.dalvikPrivateDirty;
    138             final int otherPrivate = memInfo.otherPrivateDirty;
    139 
    140             printRow(ps, formatString, "", "native", "dalvik", "other", "total");
    141             printRow(ps, formatString, "size:", nativeMax, dalvikMax, "N/A", nativeMax + dalvikMax);
    142             printRow(ps, formatString, "allocated:", nativeAllocated, dalvikAllocated, "N/A",
    143                     nativeAllocated + dalvikAllocated);
    144             printRow(ps, formatString, "free:", nativeFree, dalvikFree, "N/A",
    145                     nativeFree + dalvikFree);
    146 
    147             printRow(ps, formatString, "(Pss):", memInfo.nativePss, memInfo.dalvikPss,
    148                     memInfo.otherPss, memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss);
    149 
    150             printRow(ps, formatString, "(shared dirty):", nativeShared, dalvikShared, otherShared,
    151                     nativeShared + dalvikShared + otherShared);
    152             printRow(ps, formatString, "(priv dirty):", nativePrivate, dalvikPrivate, otherPrivate,
    153                     nativePrivate + dalvikPrivate + otherPrivate);
    154             ps.print("\n\n\n");
    155             ps.flush();
    156             ps.close();
    157             out.flush();
    158             out.close();
    159         } catch (IOException e) {
    160             Log.e(LOGTAG, e.getMessage());
    161         }
    162     }
    163 
    164     // A convenient method to be called by another activity.
    165     private void runTestAndWaitUntilDone(TestShellActivity activity, String url, int timeout,
    166             boolean getDrawTime, String saveImagePath) {
    167         activity.setCallback(new TestShellCallback() {
    168             public void finished() {
    169                 synchronized (LoadTestsAutoTest.this) {
    170                     mFinished = true;
    171                     LoadTestsAutoTest.this.notifyAll();
    172                 }
    173             }
    174 
    175             public void timedOut(String url) {
    176             }
    177         });
    178 
    179         mFinished = false;
    180         Intent intent = new Intent(Intent.ACTION_VIEW);
    181         intent.setClass(activity, TestShellActivity.class);
    182         intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    183         intent.putExtra(TestShellActivity.TEST_URL, url);
    184         intent.putExtra(TestShellActivity.TIMEOUT_IN_MILLIS, timeout);
    185         intent.putExtra(TestShellActivity.RESULT_FILE, LOAD_TEST_RESULT);
    186         intent.putExtra(TestShellActivity.GET_DRAW_TIME, getDrawTime);
    187         if (saveImagePath != null)
    188             intent.putExtra(TestShellActivity.SAVE_IMAGE, saveImagePath);
    189         activity.startActivity(intent);
    190 
    191         // Wait until done.
    192         synchronized (this) {
    193             while(!mFinished) {
    194                 try {
    195                     this.wait();
    196                 } catch (InterruptedException e) { }
    197             }
    198         }
    199     }
    200 
    201     public void copyRunnerAssetsToCache() {
    202         try {
    203             String out_dir = getActivity().getApplicationContext()
    204                 .getCacheDir().getPath() + "/";
    205 
    206             for( int i=0; i< LOAD_TEST_RUNNER_FILES.length; i++) {
    207                 InputStream in = getActivity().getAssets().open(
    208                         LOAD_TEST_RUNNER_FILES[i]);
    209                 OutputStream out = new FileOutputStream(
    210                         out_dir + LOAD_TEST_RUNNER_FILES[i]);
    211 
    212                 byte[] buf = new byte[2048];
    213                 int len;
    214 
    215                 while ((len = in.read(buf)) >= 0 ) {
    216                     out.write(buf, 0, len);
    217                 }
    218                 out.close();
    219                 in.close();
    220             }
    221         }catch (IOException e) {
    222           e.printStackTrace();
    223         }
    224 
    225     }
    226 
    227 }
    228