@mSolo
2015-04-17T00:05:29.000000Z
字数 6106
阅读 1724
Android
测试
TDD
持续集成
CI
private static final String TAG = "TemperatureTag";
TimingLogger timings = new TimingLogger(TAG, "onTextChanged");
timings.addSplit("starting conversion");
...
timings.addSplit("finish conversion");
timings.dumpToLog();
adb shell setprop log.tag.TemperatureTag VERBOSE
public class LaunchPerformanceBase extends Instrumentation {
private static final String TAG = "LaunchPerformanceBase";
protected Bundle results;
protected Intent intent;
public LaunchPerformanceBase() {
this.results = new Bundle();
this.intent = new Intent(Intent.ACTION_MAIN);
this.intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
setAutomaticPerformanceSnapshots();
}
// Launches intent {@link #intent}, and waits for idle before returning.
protected void launchApp() {
startActivitySync(intent);
waitForIdleSync();
}
@Override
public void finish(int resultCode, Bundle results) {
Log.v(TAG, "Test results = " + results);
super.finish(resultCode, results);
}
}
public class TemperatureConverterActivityLaunchPerformance
extends LaunchPerformanceBase {
@Override
public void onCreate(Bundle arguments) {
super.onCreate(arguments);
String className = "com.blundell.tut.TemperatureConverterActivity";
intent.setClassName(BuildConfig.APPLICATION_ID, className);
start();
}
@Override
public void onStart() {
super.onStart();
launchApp();
finish(Activity.RESULT_OK, results);
}
}
defaultConfig {
// other code
testInstrumentationRunner "com.blundell.tut.launchperf.TemperatureConverterActivityLaunchPerformance"
}
$ adb shell am instrument -w com.blundell.tut.test/com.blundell.tut.launchperf.TermeratureConverterActivityLaunchPerformance
IINSTRUMENTATION_RESULT: other_pss=7866
INSTRUMENTATION_RESULT: global_alloc_count=4009
INSTRUMENTATION_RESULT: java_allocated=7271
INSTRUMENTATION_RESULT: execution_time=347
INSTRUMENTATION_RESULT: gc_invocation_count=0
INSTRUMENTATION_RESULT: native_pss=0
INSTRUMENTATION_RESULT: received_transactions=-1
INSTRUMENTATION_RESULT: other_shared_dirty=7128
INSTRUMENTATION_RESULT: native_shared_dirty=0
INSTRUMENTATION_RESULT: java_free=4845
INSTRUMENTATION_RESULT: java_size=12116
INSTRUMENTATION_RESULT: global_freed_size=155012
INSTRUMENTATION_RESULT: java_pss=1095
INSTRUMENTATION_RESULT: pre_sent_transactions=-1
INSTRUMENTATION_RESULT: java_private_dirty=884
INSTRUMENTATION_RESULT: pre_received_transactions=-1
INSTRUMENTATION_RESULT: other_private_dirty=6228
INSTRUMENTATION_RESULT: native_private_dirty=0
INSTRUMENTATION_RESULT: cpu_time=120
INSTRUMENTATION_RESULT: sent_transactions=-1
INSTRUMENTATION_RESULT: native_allocated=10430
INSTRUMENTATION_RESULT: java_shared_dirty=8360
INSTRUMENTATION_RESULT: global_freed_count=1949
INSTRUMENTATION_RESULT: native_free=14145
INSTRUMENTATION_RESULT: native_size=10430
INSTRUMENTATION_RESULT: global_alloc_size=372992
INSTRUMENTATION_CODE: -1
adb shell pm list instrumentation
$ adb shell am start -n com.blundell.tut/.TemperatureConverterActivity
$ adb shell am profile com.blundell.tut start /mnt/sdcard/tc.trace
$ adb shell am profile com.blundell.tut stop
$ adb pull /mnt/sdcard/tc.trace /tmp/tc.trace
$ traceview /tmp/tc.trace
@Override
public void onTextChanged(CharSequence input, int start, int before, int count) {
if (!destinationEditNumber.hasWindowFocus() ||
destinationEditNumber.hasFocus() || input == null) {
return;
}
String str = input.toString();
if ("".equals(str)) {
destinationEditNumber.setText("");
return;
}
if (BENCHMARK_TEMPERATURE_CONVERSION) {
Debug.startMethodTracing();
}
try {
double temp = Double.parseDouble(str);
double result = (option == Option.C2F)
? TemperatureConverter.celsiusToFahrenheit(temp)
: TemperatureConverter.fahrenheitToCelsius(temp);
String resultString = String.format("%.2f", result);
destinationEditNumber.setNumber(result);
destinationEditNumber.setSelection(resultString.length());
} catch (NumberFormatException ignore) {
// WARNING this is generated whilst numbers are being entered,
// for example just a '-', so we don't want to show the error just yet
} catch (Exception e) {
sourceEditNumber.setError("ERROR: " + e.getLocalizedMessage());
}
if (BENCHMARK_TEMPERATURE_CONVERSION) {
Debug.stopMethodTracing();
}
}
$ adb pull /mnt/sdcard/dmtrace.trace /tmp/dmtrace.trace
$ traceview /tmp/dmtrace.trace
traceview 各栏目解析
Column Description Name The name of the method, including its package name, in the form we just described, which is by using /(slash) as the delimiter. Also, the parameters and the return type are displayed. Incl Cpu Time% The inclusive time, as a percentage of the total time, used by the method. This includes all its children. Incl Cpu Time The inclusive time, in milliseconds, used by the particular method. This includes the method and all its children. Excl Cpu Time% The exclusive time, as a percentage of the total time, used by the method. This excludes all its children. Excl Cpu Time The exclusive time, in milliseconds. This is the total time spent in the particular method. It excludes all its children. Incl Real Time% Inclusive time plus the waiting time of the process to execute as a percentage (waiting for I/O). Incl Real Time Inclusive time plus the waiting time of the process to execute. Excl Real Time% Exclusive time plus the waiting time of the process to execute as a percentage (waiting for I/O). Excl Real Time Exclusive time plus the waiting time of the process to execute. Calls+Recur This column shows the number of calls for the particular method and the number of recursive calls. Calls/Total The number of calls compared with the total number of calls made to this method. Cpu Time/Call The time of every call in milliseconds.
Dmtracedump
dmtracedump –t 40 –g dmtrace.png /tmp/dmtrace.trace
dmtracedump –h /tmp/dmtrace.trace > dmtrace.html
–t
: include only those child nodes that take up a fair amount of CPU time (such as your foreground app code) compile 'net.trajano.caliper:caliper:1.1.1'
buildscript {
dependencies {
classpath 'com'.shazam.fork:fork-gradle-plugin:0.10.0'
}
}
apply plugin: 'fork'