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