【问题标题】:How to test the Custom view performance如何测试自定义视图性能
【发布时间】:2020-12-08 09:30:33
【问题描述】:

我想测试我的自定义组件 UI 渲染性能。我使用以下测试用例来检查渲染性能。

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;
    }

使用此代码,我可以测试布局渲染时间。所以我改变了布局设计以获得更好的性能。现在我正在创建一个具有多个布局和组件(如图像、文本视图等)的自定义视图类。我将在运行时附加该类,组件将根据服务器响应在运行时创建。我不会在 XML 中附加这个自定义组件。现在我想测试这个自定义视图的渲染性能。请向我推荐任何工具或任何方法来计算自定义视图的 UI 渲染时间。

我的 Profiler 图片。

【问题讨论】:

    标签: android automated-tests android-view android-custom-view


    【解决方案1】:

    您可以使用 android studio 内置工具分析器(位于左下方面板)轻松测量性能。 重要:客户视图性能取决于正在运行的设备这是我为 Redmi 7a 测试过的示例

    设备:redmi 7a 内存:2GB

     private fun startTest() {
        for (x in 0..10000) {
            val textview = TextView(this)?.apply { text = "Dummy Text $x" }
            mLinearLayout?.addView(textview)
        }
    }
    
    1. 运行项目
    2. 启动分析器,等待图形恢复正常
    3. 在按钮上单击 startTest() ,您将看到图形从正常变为高
    4. 选择覆盖图形高区域的区域并查看开始时间和结束时间
    5. 使用简单的计算器并测量差异

    我的测试用例结果: 在线性布局中添加 10000 次 textview 需要 32.411 秒(上面给出的设备信息)

    【讨论】:

    • 我会检查并通知您。
    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 2019-01-05
    相关资源
    最近更新 更多