How to test the Custom view performance

大憨熊 提交于 2020-12-09 06:43:15

问题


I want to test my Custom component UI rendering performance. I used the following test case to check the rendering performance.

private long getLayoutTime(int layoutRes) {
        final Context targetContext = getInstrumentation().getTargetContext();
        final LayoutInflater layoutInflater = LayoutInflater.from(targetContext);

        final long startTime = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            final View view = layoutInflater.inflate(layoutRes, null);
            view.setLayoutParams(new ViewGroup.LayoutParams(0, 0));

            view.measure(View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            final int measuredHeight = view.getMeasuredHeight();
            final int measuredWidth = view.getMeasuredWidth();

            view.layout(0, 0, measuredWidth, measuredHeight);
        }
        return System.currentTimeMillis() - startTime;
    }

Using of this code I can test the layout render timing. So that I changed the layout design for better performance. Now I am creating a Custom view class with multiple layouts and components like images, textviews and etc. I will attach the class at run time and components will create on the run time based on the server response. I will not attach this custom component in XML. Now I want to test the rendering performance of this custom view. Please suggest me any tools or any way to calculate the UI rendering time for the custom view.

My Profiler image.


回答1:


you can easily measure performance by using android studio built-in tool profiler( at the bottom left panel). important: customer view performance depends on a running device here is a sample I have tested for redmi 7a

device:redmi 7a RAM:2GB

 private fun startTest() {
    for (x in 0..10000) {
        val textview = TextView(this)?.apply { text = "Dummy Text $x" }
        mLinearLayout?.addView(textview)
    }
}
  1. run a project
  2. start profiler, wait till graph became normal
  3. on button Click startTest() , you will see graph became normal to high
  4. select area that covers graph high area and see start time and end time
  5. use a simple calculator and measure diff

my test case result: adding textview 10000 times in linearlayout takes 32.411s (device info given above)



来源:https://stackoverflow.com/questions/63481456/how-to-test-the-custom-view-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!