blob: a80fc13b81fc51b562adbc1a6ce951d84c68e3b3 [file] [log] [blame]
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -08001/*
2 * Copyright (C) 2011 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.mediaframeworktest;
18
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -070019import java.io.BufferedWriter;
20import java.io.File;
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -080021import java.io.FileOutputStream;
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -070022import java.io.FileWriter;
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -070023import java.io.IOException;
24import java.io.InputStream;
25import java.io.Writer;
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -080026
27import android.os.Debug;
28import android.os.Environment;
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -070029import android.util.Log;
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -080030
31/**
32 *
33 * Utilities for media framework test.
34 *
35 */
36public class MediaTestUtil {
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -080037
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -070038 private static String TAG = "MediaTestUtil";
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -070039 private static final String STORAGE_PATH = Environment.getExternalStorageDirectory().toString();
40 private int mStartMemory = 0;
41 private int mStartPid = 0;
42 private Writer mOutput = null;
43 private String mTestName = null;
44 private String mProcessName = null;
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -080045
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -070046 public MediaTestUtil(String memoryOutFileName, String testName, String processName)
47 throws Exception {
48 File memoryOut = new File(memoryOutFileName);
49 mOutput = new BufferedWriter(new FileWriter(memoryOut, true));
50 mProcessName = processName;
51 mTestName = testName;
52 mStartPid = getPid();
53 mStartMemory = getVsize();
54 }
55
56 // Catpure the heapdump for memory leaksage analysis
57 public static void getNativeHeapDump(String name) throws Exception {
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -080058 System.gc();
59 System.runFinalization();
60 Thread.sleep(1000);
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -070061 FileOutputStream o = new FileOutputStream(STORAGE_PATH + '/' + name + ".dump");
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -080062 Debug.dumpNativeHeap(o.getFD());
63 o.close();
64 }
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -070065
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -070066 private void validateProcessStatus() throws Exception {
67 int currentPid = getPid();
68 //Process crash
69 if (mStartPid != currentPid) {
70 mOutput.write(mProcessName + " died. Test failed\n");
71 }
72 }
73
74 private int getPid() {
75 String memoryUsage = null;
76 int pidvalue = 0;
77 memoryUsage = captureMemInfo();
78 String[] poList2 = memoryUsage.split("\t|\\s+");
79 String pid = poList2[1];
80 pidvalue = Integer.parseInt(pid);
81 Log.v(TAG, "PID = " + pidvalue);
82 return pidvalue;
83 }
84
85 private String captureMemInfo() {
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -070086 String cm = "ps ";
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -070087 cm += mProcessName;
88 Log.v(TAG, cm);
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -070089 String memoryUsage = null;
90
91 int ch;
92 try {
93 Process p = Runtime.getRuntime().exec(cm);
94 InputStream in = p.getInputStream();
95 StringBuffer sb = new StringBuffer(512);
96 while ((ch = in.read()) != -1) {
97 sb.append((char) ch);
98 }
99 memoryUsage = sb.toString();
100 } catch (IOException e) {
101 Log.v(TAG, e.toString());
102 }
103 String[] poList = memoryUsage.split("\r|\n|\r\n");
104 String memusage = poList[1].concat("\n");
105 return memusage;
106 }
107
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -0700108 private int getVsize() {
109 String memoryUsage = captureMemInfo();
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -0700110 String[] poList2 = memoryUsage.split("\t|\\s+");
111 String vsize = poList2[3];
112 int vsizevalue = Integer.parseInt(vsize);
113 Log.v(TAG, "VSIZE = " + vsizevalue);
114 return vsizevalue;
115 }
116
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -0700117 // Write the startup media memory mOutput to the file
118 public void getStartMemoryLog() throws Exception {
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -0700119 String memusage = null;
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -0700120 mStartMemory = getVsize();
121 mOutput.write(mTestName + '\n');
122 mOutput.write("Start memory : " + mStartMemory + "\n");
123 memusage = captureMemInfo();
124 mOutput.write(memusage);
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -0700125 }
126
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -0700127 // Write the ps mediaserver mOutput to the file
128 public void getMemoryLog() throws Exception {
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -0700129 String memusage = null;
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -0700130 memusage = captureMemInfo();
131 mOutput.write(memusage);
132 mOutput.flush();
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -0700133 }
134
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -0700135 public void getMemorySummary() throws Exception {
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -0700136 int endMemory = 0;
137 int memDiff = 0;
138
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -0700139 endMemory = getVsize();
140 memDiff = endMemory - mStartMemory;
141
142 mOutput.write("End Memory :" + endMemory + "\n");
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -0700143 if (memDiff < 0) {
144 memDiff = 0;
145 }
Yu Shan Emily Lau75afb972011-03-17 20:36:37 -0700146 mOutput.write(mTestName + " total diff = " + memDiff);
147 mOutput.write("\n\n");
148 validateProcessStatus();
149 mOutput.flush();
150 mOutput.close();
Yu Shan Emily Lau9dab78472011-03-16 15:44:22 -0700151 }
152}