测量您的应用程序代码的性能

Kanzi 性能分析系统使您能够测量 Kanzi 应用程序的性能。Kanzi 使用 MainLoopProfiler 分析所有主循环任务,包括您添加的自定义任务。

在这里,您可以了解如何测量代码在 Kanzi 应用程序中的性能。要测量 Kanzi Engine 在应用程序启动期间不同部分的性能以及 Kanzi 在主循环中运行的任务的性能,使用 Profiling build 中的性能分析器。请参阅 测量 Kanzi Engine 的性能。

要测量应用程序节点的性能,您可以:

创建性能剖析类别

要为函数和范围的性能测量分组,创建您自己的性能剖析类别。

要创建性能分析类别,使用 ProfilingMacros::kzProfilingCreateCategory 宏定义类别,并使用 ProfilingMacros::kzProfilingRegisterCategory 宏注册类别。

例如:

// Define a performance profiling category MY_PROFILING_CATEGORY.
// Set the compile-time state of the category to enabled, name the category ``MyProfilingCategory``,
// and set the sample buffer size of the category to 200.
#define MY_PROFILING_CATEGORY kzProfilingCreateCategory(KZ_PROFILING_ENABLED_CATEGORY, "MyProfilingCategory", 200)
// Register the performance profiling category MY_PROFILING_CATEGORY and set its initial runtime state to enabled.
// The runtime state of a performance profiling category controls whether profilers in the category collect samples.
// You can control the runtime state of performance profiling categories in the application configuration.
kzProfilingRegisterCategory(MY_PROFILING_CATEGORY, KZ_PROFILING_ENABLED_CATEGORY)

请参阅 ProfilingCategoryFilter。

您可以使用您创建的性能剖析类别剖析如下性能:

测量范围的性能

您可以使用 Kanzi 分析宏来测量应用程序代码中任何范围的执行时间:

要测量范围的性能:

  1. (可选) 创建您自己的性能分析类别。请参阅 创建性能剖析类别。

    如果您不想创建性能剖析类别,您可以使用通用 (Generic) 类别 ProfilingMacros::KZ_PROFILING_CATEGORY_GENERIC。

  2. 在要测量性能的范围开头,调用 ProfilingMacros::kzProfileScope 宏。

    ProfilingMacros::kzProfileScope 宏创建一个分析器,并在默认分析器注册表中注册该分析器。每次进入该范围时,Kanzi 会向分析器添加一个新样本,其中包括执行此范围所花费的时间。

    例如,要测量 滚动视图示例 中 if (!m_interpolationActive) 块的执行时间:

    // Activate interpolation playback by adding it to the code.
    if (!m_interpolationActive)
    {
        // kzProfileScope() measures the time it takes to execute this scope.
        // The profiler created by kzProfileScope() is assigned to the KZ_PROFILING_CATEGORY_GENERIC category and registered in the Default Profiler Registry.
        // The name of the profiler is "Activate interpolation playback".
        // Every time this scope is entered, that is when "!m_interpolationActive" is true, Kanzi adds a new sample to the profiler.
        kzProfileScope(KZ_PROFILING_CATEGORY_GENERIC, "Activate interpolation playback");
        getDomain()->getRootTimelineClock()->addTimelinePlayback(m_interpolationPlayback);
        m_interpolationActive = true;
    }
    
  3. 记录性能分析数据。请参阅 记录 Kanzi Engine 性能分析数据。

    例如,当您退出应用程序时,Kanzi 记录器会从所有函数和范围分析器中收集数据。

  4. 构建并运行您的应用程序。请参阅 部署 Kanzi 应用程序。

Kanzi 将性能测量信息写入日志。

info:profiling> Generic::Activate interpolation playback:
info:profiling>   Total duration [ns]: 34279
info:profiling>   Number of intervals: 1
info:profiling>   Average duration [ns]: 34279
info:profiling>   Longest duration [ns]: 34279
info:profiling>   Shortest duration [ns]: 34279

测量函数的性能

您可以使用 ProfilingMacros::kzProfileFunction 宏来测量应用程序代码中函数的执行时间。

要测量函数的性能:

  1. (可选) 创建您自己的性能分析类别。请参阅 创建性能剖析类别。

    如果您不想创建性能剖析类别,您可以使用通用 (Generic) 类别 ProfilingMacros::KZ_PROFILING_CATEGORY_GENERIC。

  2. 在要测量性能的函数开头,调用 ProfilingMacros::kzProfileFunction 宏。

    ProfilingMacros::kzProfileFunction 宏在此函数中创建一个静态分析器,并在 Default Profiler Registry 中注册该分析器。每次调用该函数时,Kanzi 会向分析器添加一个新样本,其中包括执行此函数所花费的时间。

    例如,要测量 滚动视图示例 中滚动的性能,请在 ScrollViewConcept::ScrolledMessage 消息的处理程序中调用 ProfilingMacros::kzProfileFunction 宏。

    // Handler for the ScrollView3D::ScrolledMessage message from the Scroll View node.
    // Updates input values for the zoom animation.
    auto scrollToken = m_scrollView->addMessageHandler(ScrollView3D::ScrolledMessage, [this](ScrollView3D::ScrollMessageArguments& messageArguments) {
        // kzProfileFunction() measures the time it takes to execute the function.
        // The profiler created by kzProfileFunction() is assigned to the KZ_PROFILING_CATEGORY_GENERIC category and registered in the Default Profiler Registry.
        // The name of the profiler is provided by the compiler and includes the name of this function. See KZ_PROFILING_THIS_FUNCTION_NAME.
        // Every time the function is called, Kanzi adds a new sample to the profiler.
        kzProfileFunction(KZ_PROFILING_CATEGORY_GENERIC);
    
        ...
    }
    
  3. 记录性能分析数据。请参阅 记录 Kanzi Engine 性能分析数据。

    例如,当您退出应用程序时,Kanzi 记录器会从所有函数和范围分析器中收集数据。

  4. 构建并运行您的应用程序。请参阅 部署 Kanzi 应用程序。

Kanzi 将性能测量信息写入日志。

在日志中,Number of intervals 显示了函数被调用的次数。

info:profiling> Generic::auto __thiscall ScrollViewApplication::onProjectLoaded::<lambda_921241ce020d51bc4ca5a6d3e6fa81a0>::operator ()(class kanzi::ScrollViewConcept::ScrollMessageArguments &) const:
info:profiling>   Total duration [ns]: 962007
info:profiling>   Number of intervals: 273
info:profiling>   Average duration [ns]: 3523
info:profiling>   Longest duration [ns]: 47407
info:profiling>   Shortest duration [ns]: 1823

记录应用程序代码性能剖析数据

要分析应用程序代码的性能,请记录性能分析数据。您可以记录从您创建的分析器和分析器注册表中获取的每个样本和汇总数据。请参阅 记录 Kanzi Engine 性能分析数据。

另请参阅

测量应用程序性能

测量 Kanzi Engine 的性能