android中查看 activity, fragment 的渲染时间.
访问量: 4170
在你的fragment中, 加上log:
$ adb logcat -v threadtime
这样的话, 就可以看到 时间戳了. (如果不加上 -v threadtime, 就看不到时间戳)
下面是我们项目中的一个app 的log:
adb logcat -v threadtime 08-20 20:59:12.120 985 1201 I InputDispatcher: Delivering touch to (15594): action: 0x0, toolType: 1 08-20 20:59:12.120 15594 15594 D ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 08-20 20:59:12.220 985 1201 I InputDispatcher: Delivering touch to (15594): action: 0x1, toolType: 1 08-20 20:59:12.220 15594 15594 I Timeline: Timeline: Activity_launch_request id:com.tuling time:31150008 08-20 20:59:12.260 15594 15594 D TuLingBaseActivity: WebViewActivity 08-20 20:59:12.270 15594 15594 D Activity: performCreate Call secproduct feature valuefalse 08-20 20:59:12.270 15594 15594 D Activity: performCreate Call debug elastic valuetrue 08-20 20:59:12.270 15594 15594 D MyAddressFragment: in onAttach activity 08-20 20:59:12.270 15594 15594 D MyAddressFragment: onCreate 08-20 20:59:12.270 15594 15594 D MyAddressFragment: == onCreateView 08-20 20:59:12.270 15594 15594 D MyAddressFragment: onCreateView 08-20 20:59:12.290 15594 15594 W art : Attempt to remove local handle scope entry from IRT, ignoring 08-20 20:59:12.290 15594 15594 W AwContents: onDetachedFromWindow called when already detached. Ignoring 08-20 20:59:12.300 15594 15594 D MyAddressFragment: http://h5.touring.com.cn/#!/addresses?uuid=oKB7ww_FSqOs1H9elPWOmGBBCRHM&client=android 08-20 20:59:12.300 15594 15594 D MyAddressFragment: onActivityCreated 08-20 20:59:12.300 15594 15594 D MyAddressFragment: onStart 08-20 20:59:12.300 15594 15594 D MyAddressFragment: onResume 08-20 20:59:12.310 15594 15807 D MyAddressFragment: shouldInterceptRequest:http://h5.touring.com.cn/#!/addresses?uuid=oKB7ww_FSqOs1H9elPWOmGBBCRHM&client=android 08-20 20:59:12.350 15594 15594 D MyAddressFragment: onPageStarted:http://h5.touring.com.cn/#!/addresses?uuid=oKB7ww_FSqOs1H9elPWOmGBBCRHM&client=android 08-20 20:59:12.350 15594 15594 D MyAddressFragment: onPageStarted:http://h5.touring.com.cn/#!/addresses?uuid=oKB7ww_FSqOs1H9elPWOmGBBCRHM&client=android 08-20 20:59:12.420 15594 15594 W BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 15594 08-20 20:59:12.420 15594 15594 D MyAddressFragment: doUpdateVisitedHistory:http://h5.touring.com.cn/#!/addresses?uuid=oKB7ww_FSqOs1H9elPWOmGBBCRHM&client=android 08-20 20:59:12.440 15594 15594 I Timeline: Timeline: Activity_idle id: android.os.BinderProxy@147ede85 time:31150219 08-20 20:59:12.780 15594 15594 V ActivityThread: updateVisibility : ActivityRecord{3bb7928a token=android.os.BinderProxy@226da944 {com.tuling/com.tuling.MainActivity}} show : false 08-20 20:59:12.840 15594 15594 I chromium: [INFO:CONSOLE(8)] "==================uuid===oKB7ww_FSqOs1H9elPWOmGBBCRHM", source: http://h5.touring.com.cn/tuling/app.74eaf92fc442f592a2f6.js (8) 08-20 20:59:12.850 15594 15594 I chromium: [INFO:CONSOLE(8)] "oKB7ww_FSqOs1H9elPWOmGBBCRHM", source: http://h5.touring.com.cn/tuling/app.74eaf92fc442f592a2f6.js (8) 08-20 20:59:12.960 15594 15807 D MyAddressFragment: shouldInterceptRequest:http://h5.touring.com.cn/tuling/22.2adee880e5fda75772ba.js 08-20 20:59:12.960 15594 15594 I chromium: [INFO:CONSOLE(39)] "android----------params().client------------", source: http://h5.touring.com.cn/ (39) 08-20 20:59:12.960 15594 15594 I chromium: [INFO:CONSOLE(39)] "android----------params().client------------", source: http://h5.touring.com.cn/ (39) 08-20 20:59:12.960 15594 15594 I chromium: [INFO:CONSOLE(76)] "hide footer start", source: http://h5.touring.com.cn/ (76) 08-20 20:59:13.140 15594 15807 D MyAddressFragment: shouldInterceptRequest:http://h5.touring.com.cn/interface/shopping_addresses/user_address_list?uuid=oKB7ww_FSqOs1H9elPWOmGBBCRHM 08-20 20:59:13.150 15594 15594 D MyAddressFragment: onPageFinished: http://h5.touring.com.cn/#!/addresses?uuid=oKB7ww_FSqOs1H9elPWOmGBBCRHM&client=android 08-20 20:59:13.500 15594 15594 I chromium: [INFO:CONSOLE(1)] "-- show --", source: http://h5.touring.com.cn/tuling/22.2adee880e5fda75772ba.js (1)
第一次: 59:13.500 - 59:12.120 = 1.380 s
第二次: 10.540 - 9.260 = 1.280 s
3: 29.530 - 28.260 = 1.270 s
4: 55.300 - 54.080 = 1.220 s
5: 19.720 - 16.350 = 1.370 s
6: 34.220 - 32.710 = 1.510 s (开始从 android studio 调试了)
7: 23.290 - 21.980 = 1.310 s
优化后:
1. 21.38 - 20.13 = 1.25 s
2. 16.30 - 14.99 = 1.31 s
3. 55.18 - 53.69 = 1.49 s
附: 下面是源代码:
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { Log.d(TAG, "== onCreateView"); return super.onCreateView(inflater, container, savedInstanceState); } @Override public void setup() { setTitle("我的地址"); setRightButtonText("新建"); } @Override public void triggerRightBtn() { WebViewActivity.show(getActivity(), PageUrls.NEW_ADDRESS, "新建地址", WebViewActivity.FRAGMENT_MY_ADDRESS_NEW, true); } @Override public boolean overrideUrlLoading(WebView view, String url) { // 编辑时跳转到编辑页面(创建页面) Pattern pattern = Pattern.compile("addresses/\\d*/edit"); Matcher matcher = pattern.matcher(url); if (matcher.find()) { Log.d(TAG, "Edit address: " + url); WebViewActivity.show(getActivity(), url, "编辑地址", WebViewActivity.FRAGMENT_EDIT_ADDRESS, true); return true; } else { return false; } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); setInterceptRequest(false); if (Activity.RESULT_OK == resultCode) { refreshWebView(); } } @Override public void onAttach(Activity activity){ super.onAttach(activity); Log.d(TAG, "in onAttach activity"); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate"); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.d(TAG, "onActivityCreated"); } @Override public void onStart() { super.onStart(); Log.d(TAG, "onStart"); } @Override public void onResume() { super.onResume(); Log.d(TAG, "onResume"); } @Override public void onPause() { super.onPause(); Log.d(TAG, "onPause"); } @Override public void onStop() { super.onStop(); Log.d(TAG, "onStop"); } @Override public void onDestroyView() { super.onDestroyView(); Log.d(TAG, "onDestroyView"); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } @Override public void onDetach() { super.onDetach(); Log.d(TAG, "onDetach"); }