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