问题
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)
}
}
- run a project
- start profiler, wait till graph became normal
- on button Click startTest() , you will see graph became normal to high
- select area that covers graph high area and see start time and end time
- 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