blob: 622fb0e8387cd26f27bb0f532f37c8a24c4ff6bb [file] [log] [blame]
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07001/*
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
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070019import android.app.Instrumentation;
Guang Zhu00069522010-12-06 11:17:38 -080020import android.content.Context;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070021import android.content.Intent;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070022import android.os.Bundle;
Guang Zhub933f6692009-05-05 01:02:14 -070023import android.os.Debug;
Christian Mehlmauer8b85dce2010-07-19 20:11:27 +020024import android.os.Environment;
Guang Zhu40656be2009-06-30 11:56:13 -070025import android.os.Process;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070026import android.test.ActivityInstrumentationTestCase2;
Guang Zhu40656be2009-06-30 11:56:13 -070027import android.util.Log;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070028
Guang Zhu00069522010-12-06 11:17:38 -080029import java.io.File;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070030import java.io.FileOutputStream;
31import java.io.IOException;
Guang Zhub933f6692009-05-05 01:02:14 -070032import java.io.InputStream;
33import java.io.OutputStream;
34import java.io.PrintStream;
Guang Zhu5794f232011-01-25 15:43:57 -080035import java.util.concurrent.CountDownLatch;
36import java.util.concurrent.TimeUnit;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070037
38public class LoadTestsAutoTest extends ActivityInstrumentationTestCase2<TestShellActivity> {
39
40 private final static String LOGTAG = "LoadTest";
Christian Mehlmauer8b85dce2010-07-19 20:11:27 +020041 private final static String LOAD_TEST_RESULT =
42 Environment.getExternalStorageDirectory() + "/load_test_result.txt";
Guang Zhu5794f232011-01-25 15:43:57 -080043 private final static int MAX_GC_WAIT_SEC = 10;
Guang Zhub933f6692009-05-05 01:02:14 -070044 private boolean mFinished;
45 static final String LOAD_TEST_RUNNER_FILES[] = {
46 "run_page_cycler.py"
Guang Zhu4010ac32009-04-29 14:49:03 -070047 };
Guang Zhub933f6692009-05-05 01:02:14 -070048
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070049 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 Zhub933f6692009-05-05 01:02:14 -070061
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070062 // Invokes running of layout tests
63 // and waits till it has finished running.
Guang Zhub933f6692009-05-05 01:02:14 -070064 public void runPageCyclerTest() {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070065 LayoutTestsAutoRunner runner = (LayoutTestsAutoRunner) getInstrumentation();
66
67 if (runner.mTestPath == null) {
68 Log.e(LOGTAG, "No test specified");
69 return;
70 }
Guang Zhub933f6692009-05-05 01:02:14 -070071
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070072 TestShellActivity activity = (TestShellActivity) getActivity();
73
Guang Zhu2a197b12009-05-18 14:37:23 -070074 Log.v(LOGTAG, "About to run tests, calling gc first...");
Guang Zhu40656be2009-06-30 11:56:13 -070075 freeMem();
Guang Zhu2a197b12009-05-18 14:37:23 -070076
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070077 // Run tests
Guang Zhu5dc4f212009-10-29 18:24:54 -070078 runTestAndWaitUntilDone(activity, runner.mTestPath, runner.mTimeoutInMillis,
79 runner.mGetDrawTime, runner.mSaveImagePath);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070080
Guang Zhu889181d2009-07-09 12:55:15 -070081 activity.clearCache();
Guang Zhu1f6c72b2009-07-17 14:22:24 -070082 try {
83 Thread.sleep(5000);
84 } catch (InterruptedException e) {
85 }
Guang Zhub933f6692009-05-05 01:02:14 -070086 dumpMemoryInfo();
87
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070088 // Kill activity
89 activity.finish();
90 }
91
Guang Zhu40656be2009-06-30 11:56:13 -070092 private void freeMem() {
Guang Zhu3974be62011-01-18 18:04:34 -080093 Log.v(LOGTAG, "freeMem: calling gc...");
Guang Zhu5794f232011-01-25 15:43:57 -080094 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 Zhu40656be2009-06-30 11:56:13 -0700111 }
112
113 private void printRow(PrintStream ps, String format, Object...objs) {
114 ps.println(String.format(format, objs));
115 }
116
Guang Zhub933f6692009-05-05 01:02:14 -0700117 private void dumpMemoryInfo() {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700118 try {
Guang Zhu40656be2009-06-30 11:56:13 -0700119 freeMem();
Guang Zhub933f6692009-05-05 01:02:14 -0700120 Log.v(LOGTAG, "Dumping memory information.");
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700121
Guang Zhub933f6692009-05-05 01:02:14 -0700122 FileOutputStream out = new FileOutputStream(LOAD_TEST_RESULT, true);
123 PrintStream ps = new PrintStream(out);
124
Guang Zhub933f6692009-05-05 01:02:14 -0700125 ps.print("\n\n\n");
Guang Zhu40656be2009-06-30 11:56:13 -0700126 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 Zhub933f6692009-05-05 01:02:14 -0700164 ps.print("\n\n\n");
165 ps.flush();
166 ps.close();
167 out.flush();
168 out.close();
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700169 } catch (IOException e) {
170 Log.e(LOGTAG, e.getMessage());
Guang Zhub933f6692009-05-05 01:02:14 -0700171 }
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700172 }
Guang Zhub933f6692009-05-05 01:02:14 -0700173
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700174 // A convenient method to be called by another activity.
Guang Zhu5dc4f212009-10-29 18:24:54 -0700175 private void runTestAndWaitUntilDone(TestShellActivity activity, String url, int timeout,
176 boolean getDrawTime, String saveImagePath) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700177 activity.setCallback(new TestShellCallback() {
178 public void finished() {
179 synchronized (LoadTestsAutoTest.this) {
Guang Zhub933f6692009-05-05 01:02:14 -0700180 mFinished = true;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700181 LoadTestsAutoTest.this.notifyAll();
182 }
Guang Zhub933f6692009-05-05 01:02:14 -0700183 }
Guang Zhu40656be2009-06-30 11:56:13 -0700184
Guang Zhu4010ac32009-04-29 14:49:03 -0700185 public void timedOut(String url) {
186 }
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700187 });
Guang Zhub933f6692009-05-05 01:02:14 -0700188
189 mFinished = false;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700190 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 Zhu5dc4f212009-10-29 18:24:54 -0700196 intent.putExtra(TestShellActivity.GET_DRAW_TIME, getDrawTime);
197 if (saveImagePath != null)
198 intent.putExtra(TestShellActivity.SAVE_IMAGE, saveImagePath);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700199 activity.startActivity(intent);
Guang Zhub933f6692009-05-05 01:02:14 -0700200
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700201 // Wait until done.
202 synchronized (this) {
Guang Zhub933f6692009-05-05 01:02:14 -0700203 while(!mFinished) {
204 try {
205 this.wait();
206 } catch (InterruptedException e) { }
207 }
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700208 }
Guang Zhub933f6692009-05-05 01:02:14 -0700209 }
210
211 public void copyRunnerAssetsToCache() {
212 try {
Guang Zhu00069522010-12-06 11:17:38 -0800213 Context targetContext = getInstrumentation().getTargetContext();
214 File cacheDir = targetContext.getCacheDir();
Guang Zhub933f6692009-05-05 01:02:14 -0700215
216 for( int i=0; i< LOAD_TEST_RUNNER_FILES.length; i++) {
Guang Zhu00069522010-12-06 11:17:38 -0800217 InputStream in = targetContext.getAssets().open(
Guang Zhub933f6692009-05-05 01:02:14 -0700218 LOAD_TEST_RUNNER_FILES[i]);
219 OutputStream out = new FileOutputStream(
Guang Zhu00069522010-12-06 11:17:38 -0800220 new File(cacheDir, LOAD_TEST_RUNNER_FILES[i]));
Guang Zhub933f6692009-05-05 01:02:14 -0700221
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 Projectba87e3e2009-03-13 13:04:22 -0700237}