[关闭]
@mSolo 2015-04-17T00:05:29.000000Z 字数 6106 阅读 1724

Android 测试学习笔记(五)

Android 测试 TDD 持续集成 CI


Testing and Profiling Performance

Timing logger

  1. private static final String TAG = "TemperatureTag";
  2. TimingLogger timings = new TimingLogger(TAG, "onTextChanged");
  3. timings.addSplit("starting conversion");
  4. ...
  5. timings.addSplit("finish conversion");
  6. timings.dumpToLog();

Launching the performance test

  1. public class LaunchPerformanceBase extends Instrumentation {
  2. private static final String TAG = "LaunchPerformanceBase";
  3. protected Bundle results;
  4. protected Intent intent;
  5. public LaunchPerformanceBase() {
  6. this.results = new Bundle();
  7. this.intent = new Intent(Intent.ACTION_MAIN);
  8. this.intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  9. setAutomaticPerformanceSnapshots();
  10. }
  11. // Launches intent {@link #intent}, and waits for idle before returning.
  12. protected void launchApp() {
  13. startActivitySync(intent);
  14. waitForIdleSync();
  15. }
  16. @Override
  17. public void finish(int resultCode, Bundle results) {
  18. Log.v(TAG, "Test results = " + results);
  19. super.finish(resultCode, results);
  20. }
  21. }
  1. public class TemperatureConverterActivityLaunchPerformance
  2. extends LaunchPerformanceBase {
  3. @Override
  4. public void onCreate(Bundle arguments) {
  5. super.onCreate(arguments);
  6. String className = "com.blundell.tut.TemperatureConverterActivity";
  7. intent.setClassName(BuildConfig.APPLICATION_ID, className);
  8. start();
  9. }
  10. @Override
  11. public void onStart() {
  12. super.onStart();
  13. launchApp();
  14. finish(Activity.RESULT_OK, results);
  15. }
  16. }
  1. defaultConfig {
  2. // other code
  3. testInstrumentationRunner "com.blundell.tut.launchperf.TemperatureConverterActivityLaunchPerformance"
  4. }
  1. $ 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

Using the Traceview and dmtracedump platform tools

  1. $ adb shell am start -n com.blundell.tut/.TemperatureConverterActivity
  2. $ adb shell am profile com.blundell.tut start /mnt/sdcard/tc.trace
  3. $ adb shell am profile com.blundell.tut stop
  4. $ adb pull /mnt/sdcard/tc.trace /tmp/tc.trace
  5. $ traceview /tmp/tc.trace
  1. @Override
  2. public void onTextChanged(CharSequence input, int start, int before, int count) {
  3. if (!destinationEditNumber.hasWindowFocus() ||
  4. destinationEditNumber.hasFocus() || input == null) {
  5. return;
  6. }
  7. String str = input.toString();
  8. if ("".equals(str)) {
  9. destinationEditNumber.setText("");
  10. return;
  11. }
  12. if (BENCHMARK_TEMPERATURE_CONVERSION) {
  13. Debug.startMethodTracing();
  14. }
  15. try {
  16. double temp = Double.parseDouble(str);
  17. double result = (option == Option.C2F)
  18. ? TemperatureConverter.celsiusToFahrenheit(temp)
  19. : TemperatureConverter.fahrenheitToCelsius(temp);
  20. String resultString = String.format("%.2f", result);
  21. destinationEditNumber.setNumber(result);
  22. destinationEditNumber.setSelection(resultString.length());
  23. } catch (NumberFormatException ignore) {
  24. // WARNING this is generated whilst numbers are being entered,
  25. // for example just a '-', so we don't want to show the error just yet
  26. } catch (Exception e) {
  27. sourceEditNumber.setError("ERROR: " + e.getLocalizedMessage());
  28. }
  29. if (BENCHMARK_TEMPERATURE_CONVERSION) {
  30. Debug.stopMethodTracing();
  31. }
  32. }
  1. $ adb pull /mnt/sdcard/dmtrace.trace /tmp/dmtrace.trace
  2. $ traceview /tmp/dmtrace.trace

Benchmarks

Caliper microbenchmarks

其它测试策略和框架

  1. buildscript {
  2. dependencies {
  3. classpath 'com'.shazam.fork:fork-gradle-plugin:0.10.0'
  4. }
  5. }
  6. apply plugin: 'fork'
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注