アプリケーションコードのパフォーマンスを測定する

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. (オプション) 独自のパフォーマンスプロファイリングカテゴリを作成します。詳細は パフォーマンスプロファイリングカテゴリの作成 を参照してください。

    パフォーマンスプロファイリングカテゴリを作成したくない場合は、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. (オプション) 独自のパフォーマンスプロファイリングカテゴリを作成します。詳細は パフォーマンスプロファイリングカテゴリの作成 を参照してください。

    パフォーマンスプロファイリングカテゴリを作成したくない場合は、ProfilingMacros::KZ_PROFILING_CATEGORY_GENERIC のジェネリックカテゴリを使用できます。

  2. 測定したい関数の冒頭で、ProfilingMacros::kzProfileFunction マクロを呼び出します。

    ProfilingMacros::kzProfileFunction マクロは、この関数内で静的プロファイラーを作成し、デフォルトプロファイラーレジストリにプロファイラーを登録します。関数が呼び出されるたびに、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 のパフォーマンスを測定する