blob: 5cb51554abfc4c31175ec8063e0292737ee2ab71 [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;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070020import android.content.Intent;
21
22import android.util.Log;
23
24import android.os.Bundle;
Guang Zhu23a4e882009-04-16 10:16:36 -070025import android.os.Debug;
26import android.os.Debug.MemoryInfo;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070027import android.test.ActivityInstrumentationTestCase2;
28
29import com.android.dumprendertree.TestShellActivity;
30import com.android.dumprendertree.TestShellCallback;
31
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070032import java.io.FileOutputStream;
33import java.io.IOException;
Guang Zhu309f4642009-04-22 09:58:25 -070034import java.io.InputStream;
35import java.io.OutputStream;
Guang Zhu23a4e882009-04-16 10:16:36 -070036import java.io.PrintStream;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070037
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070038public class LoadTestsAutoTest extends ActivityInstrumentationTestCase2<TestShellActivity> {
39
40 private final static String LOGTAG = "LoadTest";
41 private final static String LOAD_TEST_RESULT = "/sdcard/load_test_result.txt";
Guang Zhu20b49352009-04-20 13:57:46 -070042 private boolean mFinished;
Guang Zhu309f4642009-04-22 09:58:25 -070043 static final String LOAD_TEST_RUNNER_FILES[] = {
44 "run_page_cycler.py"
45 };
Guang Zhu47222512009-04-16 10:25:54 -070046
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070047 public LoadTestsAutoTest() {
48 super("com.android.dumprendertree", TestShellActivity.class);
49 }
50
51 // This function writes the result of the layout test to
52 // Am status so that it can be picked up from a script.
53 public void passOrFailCallback(String file, boolean result) {
54 Instrumentation inst = getInstrumentation();
55 Bundle bundle = new Bundle();
56 bundle.putBoolean(file, result);
57 inst.sendStatus(0, bundle);
58 }
Guang Zhu47222512009-04-16 10:25:54 -070059
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070060 // Invokes running of layout tests
61 // and waits till it has finished running.
Guang Zhu309f4642009-04-22 09:58:25 -070062 public void runPageCyclerTest() {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070063 LayoutTestsAutoRunner runner = (LayoutTestsAutoRunner) getInstrumentation();
64
65 if (runner.mTestPath == null) {
66 Log.e(LOGTAG, "No test specified");
67 return;
68 }
Guang Zhu47222512009-04-16 10:25:54 -070069
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070070 TestShellActivity activity = (TestShellActivity) getActivity();
71
72 // Run tests
73 runTestAndWaitUntilDone(activity, runner.mTestPath, runner.mTimeoutInMillis);
74
75 // TODO(fqian): let am instrumentation pass in the command line, currently
76 // am instrument does not allow spaces in the command.
Guang Zhu23a4e882009-04-16 10:16:36 -070077 dumpMemoryInfo();
Guang Zhu47222512009-04-16 10:25:54 -070078
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070079 // Kill activity
80 activity.finish();
81 }
82
Guang Zhu23a4e882009-04-16 10:16:36 -070083 private void dumpMemoryInfo() {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070084 try {
Guang Zhu23a4e882009-04-16 10:16:36 -070085 Log.v(LOGTAG, "Dumping memory information.");
Guang Zhu47222512009-04-16 10:25:54 -070086
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070087 FileOutputStream out = new FileOutputStream(LOAD_TEST_RESULT, true);
Guang Zhu23a4e882009-04-16 10:16:36 -070088 PrintStream ps = new PrintStream(out);
Guang Zhu47222512009-04-16 10:25:54 -070089
Guang Zhu23a4e882009-04-16 10:16:36 -070090 MemoryInfo mi = new MemoryInfo();
91 Debug.getMemoryInfo(mi);
Guang Zhu47222512009-04-16 10:25:54 -070092
Guang Zhu23a4e882009-04-16 10:16:36 -070093 //try to fake the dumpsys format
94 //this will eventually be changed to XML
95 String format = "%15s:%9d%9d%9d%9d";
96 String pss =
97 String.format(format, "(Pss)",
98 mi.nativePss, mi.dalvikPss, mi.otherPss,
99 mi.nativePss + mi.dalvikPss + mi.otherPss);
100 String sd =
101 String.format(format, "(shared dirty)",
102 mi.nativeSharedDirty, mi.dalvikSharedDirty, mi.otherSharedDirty,
103 mi.nativeSharedDirty + mi.dalvikSharedDirty + mi.otherSharedDirty);
104 String pd =
105 String.format(format, "(priv dirty)",
106 mi.nativePrivateDirty, mi.dalvikPrivateDirty, mi.otherPrivateDirty,
107 mi.nativePrivateDirty + mi.dalvikPrivateDirty + mi.otherPrivateDirty);
Guang Zhu47222512009-04-16 10:25:54 -0700108
Guang Zhu23a4e882009-04-16 10:16:36 -0700109 ps.print("\n\n\n");
110 ps.println("** MEMINFO in pid 0 [com.android.dumprendertree] **");
111 ps.println(" native dalvik other total");
112 ps.println(" size: 12060 5255 N/A 17315");
113 ps.println(" allocated: 12060 5255 N/A 17315");
114 ps.println(" free: 12060 5255 N/A 17315");
115 ps.println(pss);
116 ps.println(sd);
117 ps.println(pd);
118 ps.print("\n\n\n");
119 ps.flush();
120 ps.close();
121 out.flush();
122 out.close();
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700123 } catch (IOException e) {
124 Log.e(LOGTAG, e.getMessage());
Guang Zhu47222512009-04-16 10:25:54 -0700125 }
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700126 }
Guang Zhu47222512009-04-16 10:25:54 -0700127
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700128 // A convenient method to be called by another activity.
129 private void runTestAndWaitUntilDone(TestShellActivity activity, String url, int timeout) {
130 activity.setCallback(new TestShellCallback() {
131 public void finished() {
132 synchronized (LoadTestsAutoTest.this) {
Guang Zhu20b49352009-04-20 13:57:46 -0700133 mFinished = true;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700134 LoadTestsAutoTest.this.notifyAll();
135 }
Guang Zhu47222512009-04-16 10:25:54 -0700136 }
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700137 });
Guang Zhu47222512009-04-16 10:25:54 -0700138
Guang Zhu20b49352009-04-20 13:57:46 -0700139 mFinished = false;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700140 Intent intent = new Intent(Intent.ACTION_VIEW);
141 intent.setClass(activity, TestShellActivity.class);
142 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
143 intent.putExtra(TestShellActivity.TEST_URL, url);
144 intent.putExtra(TestShellActivity.TIMEOUT_IN_MILLIS, timeout);
145 intent.putExtra(TestShellActivity.RESULT_FILE, LOAD_TEST_RESULT);
146 activity.startActivity(intent);
Guang Zhu47222512009-04-16 10:25:54 -0700147
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700148 // Wait until done.
149 synchronized (this) {
Guang Zhu20b49352009-04-20 13:57:46 -0700150 while(!mFinished) {
151 try {
152 this.wait();
153 } catch (InterruptedException e) { }
154 }
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700155 }
Guang Zhu47222512009-04-16 10:25:54 -0700156 }
Guang Zhu309f4642009-04-22 09:58:25 -0700157
158 public void copyRunnerAssetsToCache() {
159 try {
160 String out_dir = getActivity().getApplicationContext()
161 .getCacheDir().getPath() + "/";
162
163 for( int i=0; i< LOAD_TEST_RUNNER_FILES.length; i++) {
164 InputStream in = getActivity().getAssets().open(
165 LOAD_TEST_RUNNER_FILES[i]);
166 OutputStream out = new FileOutputStream(
167 out_dir + LOAD_TEST_RUNNER_FILES[i]);
168
169 byte[] buf = new byte[2048];
170 int len;
171
172 while ((len = in.read(buf)) >= 0 ) {
173 out.write(buf, 0, len);
174 }
175 out.close();
176 in.close();
177 }
178 }catch (IOException e) {
179 e.printStackTrace();
180 }
181
182 }
183
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700184}