文件申請

本頁面提供相關指引,說明如何使用 Cloud Shell 從 GitHub golang-samples/profiler 存放區下載及執行範例。您也可以在Google Cloud以外執行這些範例。如要瞭解此情況下所需的額外設定步驟,請參閱「剖析在 Google Cloud Platform 以外環境執行的應用程式 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 服務。分析器快速入門導覽課程會使用這個範例。

    如要啟動 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 專案設定錯誤」。