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