LoadTestsAutoTest.java revision 309f464807fcf7e3055657e6f6d2de7882218fba
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 android.app.Instrumentation;
20import android.content.Intent;
21
22import android.util.Log;
23
24import android.os.Bundle;
25import android.os.Debug;
26import android.os.Debug.MemoryInfo;
27import android.test.ActivityInstrumentationTestCase2;
28
29import com.android.dumprendertree.TestShellActivity;
30import com.android.dumprendertree.TestShellCallback;
31
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 = "/sdcard/load_test_result.txt";
42    private boolean mFinished;
43    static final String LOAD_TEST_RUNNER_FILES[] = {
44        "run_page_cycler.py"
45  };
46
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    }
59
60    // Invokes running of layout tests
61    // and waits till it has finished running.
62    public void runPageCyclerTest() {
63        LayoutTestsAutoRunner runner = (LayoutTestsAutoRunner) getInstrumentation();
64
65        if (runner.mTestPath == null) {
66            Log.e(LOGTAG, "No test specified");
67            return;
68        }
69
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.
77        dumpMemoryInfo();
78
79        // Kill activity
80        activity.finish();
81    }
82
83    private void dumpMemoryInfo() {
84        try {
85            Log.v(LOGTAG, "Dumping memory information.");
86
87            FileOutputStream out = new FileOutputStream(LOAD_TEST_RESULT, true);
88            PrintStream ps = new PrintStream(out);
89
90            MemoryInfo mi = new MemoryInfo();
91            Debug.getMemoryInfo(mi);
92
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);
108
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();
123        } catch (IOException e) {
124            Log.e(LOGTAG, e.getMessage());
125        }
126    }
127
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) {
133                    mFinished = true;
134                    LoadTestsAutoTest.this.notifyAll();
135                }
136            }
137        });
138
139        mFinished = false;
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);
147
148        // Wait until done.
149        synchronized (this) {
150            while(!mFinished) {
151                try {
152                    this.wait();
153                } catch (InterruptedException e) { }
154            }
155        }
156    }
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
184}
185