Go アプリを最適化する

このチュートリアルでは、プロファイル データを収集するように構成された、意図的に非効率的な Go アプリケーションをデプロイします。Profiler のインターフェースを使用して、プロファイル データを表示し、実行可能な最適化を特定します。次に、アプリケーションを変更してデプロイし、変更の効果を評価します。

始める前に

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Cloud Profiler API を有効にします。

    API を有効にするために必要なロール

    API を有効にするには、serviceusage.services.enable 権限を含む Service Usage 管理者 IAM ロール(roles/serviceusage.serviceUsageAdmin)が必要です。詳しくは、ロールを付与する方法をご覧ください。

    API の有効化

  3. Cloud Shell を開くには、 Google Cloud コンソールのツールバーで [Cloud Shell をアクティブにする] をクリックします。

    Cloud Shell をアクティブにします。

    しばらくすると、Google Cloud コンソール内で Cloud Shell セッションが開きます。

    Cloud Shell セッション。

サンプル アプリケーション

主な目的は、サーバーで処理できる 1 秒あたりのクエリ数を最大化することです。もう 1 つの目的は、不要なメモリ割り当てを削除することで、メモリ使用量を低減することです。

サーバーは gRPC フレームワークを使用して、単語またはフレーズを受け取り、その単語またはフレーズがシェイクスピアの作品に出現する回数を返します。

サーバーが処理できる平均の秒間クエリ数は、サーバーの負荷テストによって決まります。テストのラウンドごとに、クライアント シミュレータが呼び出され、20 件のシーケンシャル クエリを発行するように指示されます。ラウンドが完了すると、クライアント シミュレータによって送信されたクエリの数、経過時間、秒間クエリ数が表示されます。

サーバーコードは意図的に非効率になっています。

サンプル アプリケーションの実行

サンプル アプリケーションをダウンロードして実行します。

  1. Cloud Shell で、次のコマンドを実行します。

    git clone https://github.com/GoogleCloudPlatform/golang-samples.git
    cd golang-samples/profiler/shakesapp
    
  2. バージョンを 1 に設定し、ラウンド数を 15 に設定してアプリケーションを実行します。

    go run . -version 1 -num_rounds 15
    

    1~2 分後にプロファイル データが表示されます。プロファイル データは次の例のようになります。

    CPU 時間使用量の最初のフレームグラフ。

    スクリーンショットで、[プロファイルの種類] が CPU time に設定されていることが確認できます。これは、フレームグラフに CPU 使用率のデータが表示されていることを示しています。

    Cloud Shell に出力されるサンプル出力を以下に示します。

    $ go run . -version 1 -num_rounds 15
    2020/08/27 17:27:34 Simulating client requests, round 1
    2020/08/27 17:27:34 Stackdriver Profiler Go Agent version: 20200618
    2020/08/27 17:27:34 profiler has started
    2020/08/27 17:27:34 creating a new profile via profiler service
    2020/08/27 17:27:51 Simulated 20 requests in 17.3s, rate of 1.156069 reqs / sec
    2020/08/27 17:27:51 Simulating client requests, round 2
    2020/08/27 17:28:10 Simulated 20 requests in 19.02s, rate of 1.051525 reqs / sec
    2020/08/27 17:28:10 Simulating client requests, round 3
    2020/08/27 17:28:29 Simulated 20 requests in 18.71s, rate of 1.068947 reqs / sec
    ...
    2020/08/27 17:44:32 Simulating client requests, round 14
    2020/08/27 17:46:04 Simulated 20 requests in 1m32.23s, rate of 0.216849 reqs / sec
    2020/08/27 17:46:04 Simulating client requests, round 15
    2020/08/27 17:47:52 Simulated 20 requests in 1m48.03s, rate of 0.185134 reqs / sec
    

    Cloud Shell の出力には、各イテレーションの経過時間と平均リクエスト率が表示されます。アプリケーションの起動時に、「Simulated 20 requests in 17.3s, rate of 1.156069 reqs / sec」というエントリは、サーバーで 1 秒につき約 1 件のリクエストが実行されていることを示します。最後のラウンドでは、「Simulate 20 requests in 1m48.03s, rate of 0.185134 reqs / second」というエントリは、サーバーで 5 秒ごとに約 1 件のリクエストが実行されていることを示しています。