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; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
| 21 | |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import android.os.Bundle; |
Guang Zhu | 23a4e88 | 2009-04-16 10:16:36 -0700 | [diff] [blame] | 25 | import android.os.Debug; |
| 26 | import android.os.Debug.MemoryInfo; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 27 | import android.test.ActivityInstrumentationTestCase2; |
| 28 | |
| 29 | import com.android.dumprendertree.TestShellActivity; |
| 30 | import com.android.dumprendertree.TestShellCallback; |
| 31 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 32 | import java.io.FileOutputStream; |
| 33 | import java.io.IOException; |
Guang Zhu | 309f464 | 2009-04-22 09:58:25 -0700 | [diff] [blame] | 34 | import java.io.InputStream; |
| 35 | import java.io.OutputStream; |
Guang Zhu | 23a4e88 | 2009-04-16 10:16:36 -0700 | [diff] [blame] | 36 | import java.io.PrintStream; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 37 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 38 | public 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 Zhu | 20b4935 | 2009-04-20 13:57:46 -0700 | [diff] [blame] | 42 | private boolean mFinished; |
Guang Zhu | 309f464 | 2009-04-22 09:58:25 -0700 | [diff] [blame] | 43 | static final String LOAD_TEST_RUNNER_FILES[] = { |
| 44 | "run_page_cycler.py" |
| 45 | }; |
Guang Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 46 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 47 | 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 Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 59 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 60 | // Invokes running of layout tests |
| 61 | // and waits till it has finished running. |
Guang Zhu | 309f464 | 2009-04-22 09:58:25 -0700 | [diff] [blame] | 62 | public void runPageCyclerTest() { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 63 | LayoutTestsAutoRunner runner = (LayoutTestsAutoRunner) getInstrumentation(); |
| 64 | |
| 65 | if (runner.mTestPath == null) { |
| 66 | Log.e(LOGTAG, "No test specified"); |
| 67 | return; |
| 68 | } |
Guang Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 69 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 70 | 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 Zhu | 23a4e88 | 2009-04-16 10:16:36 -0700 | [diff] [blame] | 77 | dumpMemoryInfo(); |
Guang Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 78 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 79 | // Kill activity |
| 80 | activity.finish(); |
| 81 | } |
| 82 | |
Guang Zhu | 23a4e88 | 2009-04-16 10:16:36 -0700 | [diff] [blame] | 83 | private void dumpMemoryInfo() { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 84 | try { |
Guang Zhu | 23a4e88 | 2009-04-16 10:16:36 -0700 | [diff] [blame] | 85 | Log.v(LOGTAG, "Dumping memory information."); |
Guang Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 86 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 87 | FileOutputStream out = new FileOutputStream(LOAD_TEST_RESULT, true); |
Guang Zhu | 23a4e88 | 2009-04-16 10:16:36 -0700 | [diff] [blame] | 88 | PrintStream ps = new PrintStream(out); |
Guang Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 89 | |
Guang Zhu | 23a4e88 | 2009-04-16 10:16:36 -0700 | [diff] [blame] | 90 | MemoryInfo mi = new MemoryInfo(); |
| 91 | Debug.getMemoryInfo(mi); |
Guang Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 92 | |
Guang Zhu | 23a4e88 | 2009-04-16 10:16:36 -0700 | [diff] [blame] | 93 | //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 Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 108 | |
Guang Zhu | 23a4e88 | 2009-04-16 10:16:36 -0700 | [diff] [blame] | 109 | 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 Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 123 | } catch (IOException e) { |
| 124 | Log.e(LOGTAG, e.getMessage()); |
Guang Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 125 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 126 | } |
Guang Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 127 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 128 | // 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 Zhu | 20b4935 | 2009-04-20 13:57:46 -0700 | [diff] [blame] | 133 | mFinished = true; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 134 | LoadTestsAutoTest.this.notifyAll(); |
| 135 | } |
Guang Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 136 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 137 | }); |
Guang Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 138 | |
Guang Zhu | 20b4935 | 2009-04-20 13:57:46 -0700 | [diff] [blame] | 139 | mFinished = false; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 140 | 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 Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 147 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 148 | // Wait until done. |
| 149 | synchronized (this) { |
Guang Zhu | 20b4935 | 2009-04-20 13:57:46 -0700 | [diff] [blame] | 150 | while(!mFinished) { |
| 151 | try { |
| 152 | this.wait(); |
| 153 | } catch (InterruptedException e) { } |
| 154 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 155 | } |
Guang Zhu | 4722251 | 2009-04-16 10:25:54 -0700 | [diff] [blame] | 156 | } |
Guang Zhu | 309f464 | 2009-04-22 09:58:25 -0700 | [diff] [blame] | 157 | |
| 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 Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 184 | } |