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