LoadTestsAutoTest.java revision 1f6c72bff6fa875a08f2d5510b5a61ce766643d2
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        activity.clearCache();
76        try {
77            Thread.sleep(5000);
78        } catch (InterruptedException e) {
79        }
80        dumpMemoryInfo();
81
82        // Kill activity
83        activity.finish();
84    }
85
86    private void freeMem() {
87        Log.v(LOGTAG, "freeMem: calling gc/finalization...");
88        final VMRuntime runtime = VMRuntime.getRuntime();
89
90        runtime.gcSoftReferences();
91        runtime.runFinalizationSync();
92        runtime.gcSoftReferences();
93        runtime.runFinalizationSync();
94        runtime.gcSoftReferences();
95        runtime.runFinalizationSync();
96        Runtime.getRuntime().runFinalization();
97        Runtime.getRuntime().gc();
98        Runtime.getRuntime().gc();
99
100    }
101
102    private void printRow(PrintStream ps, String format, Object...objs) {
103        ps.println(String.format(format, objs));
104    }
105
106    private void dumpMemoryInfo() {
107        try {
108            freeMem();
109            Log.v(LOGTAG, "Dumping memory information.");
110
111            FileOutputStream out = new FileOutputStream(LOAD_TEST_RESULT, true);
112            PrintStream ps = new PrintStream(out);
113
114            ps.print("\n\n\n");
115            ps.println("** MEMINFO in pid " + Process.myPid()
116                    + " [com.android.dumprendertree] **");
117            String formatString = "%17s %8s %8s %8s %8s";
118
119            long nativeMax = Debug.getNativeHeapSize() / 1024;
120            long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
121            long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
122            Runtime runtime = Runtime.getRuntime();
123            long dalvikMax = runtime.totalMemory() / 1024;
124            long dalvikFree = runtime.freeMemory() / 1024;
125            long dalvikAllocated = dalvikMax - dalvikFree;
126
127
128            Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
129            Debug.getMemoryInfo(memInfo);
130
131            final int nativeShared = memInfo.nativeSharedDirty;
132            final int dalvikShared = memInfo.dalvikSharedDirty;
133            final int otherShared = memInfo.otherSharedDirty;
134
135            final int nativePrivate = memInfo.nativePrivateDirty;
136            final int dalvikPrivate = memInfo.dalvikPrivateDirty;
137            final int otherPrivate = memInfo.otherPrivateDirty;
138
139            printRow(ps, formatString, "", "native", "dalvik", "other", "total");
140            printRow(ps, formatString, "size:", nativeMax, dalvikMax, "N/A", nativeMax + dalvikMax);
141            printRow(ps, formatString, "allocated:", nativeAllocated, dalvikAllocated, "N/A",
142                    nativeAllocated + dalvikAllocated);
143            printRow(ps, formatString, "free:", nativeFree, dalvikFree, "N/A",
144                    nativeFree + dalvikFree);
145
146            printRow(ps, formatString, "(Pss):", memInfo.nativePss, memInfo.dalvikPss,
147                    memInfo.otherPss, memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss);
148
149            printRow(ps, formatString, "(shared dirty):", nativeShared, dalvikShared, otherShared,
150                    nativeShared + dalvikShared + otherShared);
151            printRow(ps, formatString, "(priv dirty):", nativePrivate, dalvikPrivate, otherPrivate,
152                    nativePrivate + dalvikPrivate + otherPrivate);
153            ps.print("\n\n\n");
154            ps.flush();
155            ps.close();
156            out.flush();
157            out.close();
158        } catch (IOException e) {
159            Log.e(LOGTAG, e.getMessage());
160        }
161    }
162
163    // A convenient method to be called by another activity.
164    private void runTestAndWaitUntilDone(TestShellActivity activity, String url, int timeout) {
165        activity.setCallback(new TestShellCallback() {
166            public void finished() {
167                synchronized (LoadTestsAutoTest.this) {
168                    mFinished = true;
169                    LoadTestsAutoTest.this.notifyAll();
170                }
171            }
172
173            public void timedOut(String url) {
174            }
175        });
176
177        mFinished = false;
178        Intent intent = new Intent(Intent.ACTION_VIEW);
179        intent.setClass(activity, TestShellActivity.class);
180        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
181        intent.putExtra(TestShellActivity.TEST_URL, url);
182        intent.putExtra(TestShellActivity.TIMEOUT_IN_MILLIS, timeout);
183        intent.putExtra(TestShellActivity.RESULT_FILE, LOAD_TEST_RESULT);
184        activity.startActivity(intent);
185
186        // Wait until done.
187        synchronized (this) {
188            while(!mFinished) {
189                try {
190                    this.wait();
191                } catch (InterruptedException e) { }
192            }
193        }
194    }
195
196    public void copyRunnerAssetsToCache() {
197        try {
198            String out_dir = getActivity().getApplicationContext()
199                .getCacheDir().getPath() + "/";
200
201            for( int i=0; i< LOAD_TEST_RUNNER_FILES.length; i++) {
202                InputStream in = getActivity().getAssets().open(
203                        LOAD_TEST_RUNNER_FILES[i]);
204                OutputStream out = new FileOutputStream(
205                        out_dir + LOAD_TEST_RUNNER_FILES[i]);
206
207                byte[] buf = new byte[2048];
208                int len;
209
210                while ((len = in.read(buf)) >= 0 ) {
211                    out.write(buf, 0, len);
212                }
213                out.close();
214                in.close();
215            }
216        }catch (IOException e) {
217          e.printStackTrace();
218        }
219
220    }
221
222}
223