| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.gameperformance; |
| 17 |
|
| 18 |
import java.io.BufferedReader; |
| 19 |
import java.io.FileWriter; |
| 20 |
import java.io.IOException; |
| 21 |
import java.io.InputStreamReader; |
| 22 |
|
| 23 |
import android.app.Instrumentation; |
| 24 |
import android.os.AsyncTask; |
| 25 |
import android.os.ParcelFileDescriptor; |
| 26 |
import android.util.Log; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class ATraceRunner extends AsyncTask<Void, Integer, Boolean>{ |
| 35 |
private final static String TAG = "ATraceRunner"; |
| 36 |
|
| 37 |
|
| 38 |
public interface Delegate { |
| 39 |
public void onProcessed(boolean success); |
| 40 |
} |
| 41 |
|
| 42 |
private final Instrumentation mInstrumentation; |
| 43 |
private final String mOutput; |
| 44 |
private final int mTimeInSeconds; |
| 45 |
private final String mCategory; |
| 46 |
private final Delegate mDelegate; |
| 47 |
|
| 48 |
public ATraceRunner(Instrumentation instrumentation, |
| 49 |
String output, |
| 50 |
int timeInSeconds, |
| 51 |
String category, |
| 52 |
Delegate delegate) { |
| 53 |
mInstrumentation = instrumentation; |
| 54 |
mOutput = output; |
| 55 |
mTimeInSeconds = timeInSeconds; |
| 56 |
mCategory = category; |
| 57 |
mDelegate = delegate; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
protected Boolean doInBackground(Void... params) { |
| 62 |
BufferedReader bufferedReader = null; |
| 63 |
FileWriter writer = null; |
| 64 |
try { |
| 65 |
|
| 66 |
final String cmd = "atrace -t " + mTimeInSeconds + " " + mCategory; |
| 67 |
Log.i(TAG, "Running atrace... " + cmd); |
| 68 |
writer = new FileWriter(mOutput); |
| 69 |
final ParcelFileDescriptor fd = |
| 70 |
mInstrumentation.getUiAutomation().executeShellCommand(cmd); |
| 71 |
bufferedReader = new BufferedReader( |
| 72 |
new InputStreamReader(new ParcelFileDescriptor.AutoCloseInputStream(fd))); |
| 73 |
String line; |
| 74 |
while ((line = bufferedReader.readLine()) != null) { |
| 75 |
writer.write(line); |
| 76 |
writer.write("\n"); |
| 77 |
} |
| 78 |
Log.i(TAG, "Running atrace... DONE"); |
| 79 |
return true; |
| 80 |
} catch (IOException e) { |
| 81 |
Log.i(TAG, "atrace failed", e); |
| 82 |
return false; |
| 83 |
} finally { |
| 84 |
Utils.closeQuietly(bufferedReader); |
| 85 |
Utils.closeQuietly(writer); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
protected void onPostExecute(Boolean result) { |
| 91 |
mDelegate.onProcessed(result); |
| 92 |
} |
| 93 |
|
| 94 |
} |