說明文件應用程式

本頁面提供使用 Cloud Shell 下載及執行 GitHub golang-samples/profiler 存放區範例的指引。您也可以在Google Cloud以外執行這些範例。如要瞭解這種情況下需要採取的額外設定步驟,請參閱「剖析在 Google Cloud以外環境執行的應用程式」。

如果您選擇執行這些程式,就能快速建立剖析資料,用於探索 Cloud Profiler 介面的功能。您也可以修改這些範例的副本,然後使用 Cloud Profiler 介面重新部署並分析變更的影響。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Cloud Profiler API.

    Enable the API

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

    Go to project selector

  5. Enable the Cloud Profiler API.

    Enable the API

  6. 啟動 Cloud Shell

    在專案的 Google Cloud 控制台頁面頂端,按一下「啟用 Cloud Shell」

    啟動 Cloud Shell。

    系統會在主控台底部的新頁框中開啟 Cloud Shell 工作階段,並顯示兩則訊息和指令列提示。第一則訊息會列出 Cloud Shell 工作階段的 Google Cloud 專案。第二則訊息會說明如何變更工作階段專案。殼層工作階段可能需要幾秒鐘的時間才能完成初始化:

    Cloud Shell 工作階段。

    讓程式進行剖析

    在 Cloud Shell 中擷取 Go 範例套件:

    git clone https://github.com/GoogleCloudPlatform/golang-samples.git
    

    套件擷取作業需要一些時間才能完成。

    profiler_quickstart

    範例 profiler_quickstart 已設定為執行 hello-profiler 服務。Profiler 快速入門導覽課程會使用這個範例。

    如要啟動 hello-profiler,請執行下列步驟:

    1. 切換至 profiler_quickstart 目錄:

      cd ~/gopath/src/github.com/GoogleCloudPlatform/golang-samples/profiler/profiler_quickstart
      
    2. 執行下列指令來啟動服務:

      go run main.go
      

    啟動服務後幾秒,Cloud Shell 工作階段就會顯示 profiler has started 訊息。每次將剖析資料上傳到專案時,系統都會顯示新訊息。 Google Cloud

    如要停止服務,請輸入 Ctrl-C。

    hotapp

    範例 hotapp 使用無限迴圈,呼叫兩個函式,然後呼叫 Go 調度器。這個範例支援設定旗標 -local_work。當這個標記為 true 時,會將以 for 迴圈形式呈現的工作新增至個別函式。根據預設,這項旗標會設為 false

    如要執行 hotapp 範例,請按照下列步驟操作:

    1. 切換至 hotapp 目錄:

      cd ~/gopath/src/github.com/GoogleCloudPlatform/golang-samples/profiler/hotapp
      
    2. 執行下列指令來啟動服務:

      go run main.go
      

      如果您想產生與 Cloud Profiler 說明文件中顯示的資料一致的剖析資料,請使用以下叫用:

      go run main.go -service=docdemo-service -local_work -skew=75 -version=1.75.0
      

    啟動服務後幾秒,Cloud Shell 工作階段就會顯示 profiler has started 訊息。每次將剖析資料上傳到專案時,系統都會顯示新訊息。 Google Cloud

    如要停止服務,請輸入 Ctrl-C。

    比較設定檔一節使用了 docdemo-service 的兩種不同部署方式。在某個部署作業中,skewversion 已如先前所述設定。在第二次部署中,skew 設為 25version 則設為 1.25.0

    疑難排解

    如果您在啟動服務後收到權限遭拒錯誤訊息,請參閱 Google Cloud 專案設定的錯誤