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.
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.
即使对于简单的应用,您也可以借助过滤条件隐藏不感兴趣的帧,以便更清楚地查看感兴趣的帧。例如,在示例代码的性能剖析屏幕截图中,灰色帧略大于其下方的第一个 main 帧。为什么?是不是发生了其他一些变化,但由于 main 调用堆栈耗用了绝大部分资源,这些变化未能立即显现出来?如需了解在应用的 main 例程外部发生的状况,请添加一个隐藏 main 例程的调用堆栈的过滤条件。只有 0.227% 的资源消耗是在 main 外部发生的:
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[],[],null,["# Quickstart: Measure app performance\n\nMeasure app performance\n=======================\n\nThis document shows you how to set up and use Cloud Profiler. You use\nthe Cloud Shell to\ndownload a sample Go program, run it with profiling enabled, and then\nuse the Profiler interface to explore the captured data.\n\nIf you plan to profile applications that are running outside of\nGoogle Cloud, then you need to perform some configuration steps that aren't\ndiscussed in this document. For information about these steps, see\n[Profiling applications running outside Google Cloud](/profiler/docs/profiling-external).\n\nBefore you begin\n----------------\n\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n\n\n Enable the required API.\n\n\n [Enable the API](https://console.cloud.google.com/flows/enableapi?apiid=cloudprofiler.googleapis.com&redirect=https://console.cloud.google.com)\n\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n\n\n Enable the required API.\n\n\n [Enable the API](https://console.cloud.google.com/flows/enableapi?apiid=cloudprofiler.googleapis.com&redirect=https://console.cloud.google.com)\n\n1. \n2.\n\n\n Enable the required API.\n\n\n [Enable the API](https://console.cloud.google.com/flows/enableapi?apiid=cloudprofiler.googleapis.com&redirect=https://console.cloud.google.com)\n3. To open the Cloud Shell, in the Google Cloud console toolbar, click **Activate Cloud Shell** :\n\n \u003cbr /\u003e\n\n After a few moments, a Cloud Shell session opens inside the\n Google Cloud console:\n\n\u003cbr /\u003e\n\nGet a program to profile\n------------------------\n\nThe sample program, `main.go`, is in the `golang-samples` repository on\nGitHub. To get it, in the Cloud Shell, run the following command\nto retrieve the package of Go samples: \n\n git clone https://github.com/GoogleCloudPlatform/golang-samples.git\n\nThe package retrieval takes a few moments to complete.\n\nProfile the code\n----------------\n\nIn the Cloud Shell, go\nto the directory of sample code for Profiler in the retrieved\npackage: \n\n cd golang-samples/profiler/profiler_quickstart\n\nThe `main.go` program creates a CPU-intensive workload to provide data to\nthe profiler. In the Cloud Shell, run the following command to\nstart the program, and leave it running: \n\n go run main.go\n\nThis program is designed to load the CPU as it runs. It is configured\nto use Profiler, which collects profiling data from the program\nas it runs and periodically saves it.\n\nA few seconds after you start the program, you see the message\n`profiler has started`. In about a minute, two more messages are displayed: \n\n successfully created profile CPU\n start uploading profile\n\nThese messages indicate that a profile was created and uploaded to your\nCloud Storage project. The program continues to emit the last two messages,\nabout one time per minute, for as long as it runs.\n\nIf you receive a permission denied error message after starting the service,\nsee\n[Errors with your Google Cloud project configuration](/profiler/docs/troubleshooting#errors-configuration).\n\nProfiler interface\n------------------\n\nA few moments after you start the application, Profiler\ndisplays the initial profile data. The interface offers an array of controls\nand a flame graph for exploring the profiling data:\n\nIn addition to time controls, there are options that let you choose the set\nof profile data to use. When you are profiling multiple applications, you use\n**Service** to select the origin of the profiled data.\n**Profile type** lets you choose the kind\nof profile data to display. **Zone name** and **Version** let you restrict\ndisplay to data from [Compute Engine zones](/compute/docs/regions-zones) or versions of\nthe application. **Weight** lets you select profiles captured during peak\nresource consumption.\n\nTo refine how the flame graph displays the profiles you've selected to analyze,\nyou add filters. In the previous screenshot, the filter bar\n*filter_list* shows\none filter. This filter option is `Metric` and the filter value is `CPU time`.\n\n### Exploring the data\n\nThe flame graph displays the call stacks of the program. The flame graph\nrepresents each function with a frame.\nThe width of the frame represents the proportion of resource consumption\nby that function.\nThe top frame represents the entire program. This frame always shows 100%\nof the resource consumption. This frame also lists how many profiles are\naveraged together in this graph.\n\nThe sample program doesn't have a complicated set of call stacks;\nin the preceding screenshot, you see 5 frames:\n\n- The gray frame represents the entire executable, which accounts for 100% of the resources being consumed.\n- The green `main` frame is the Go `runtime.main`.\n- The orange `main` frame is the `main` routine of the sample program.\n- The orange `busyloop` frame is a routine called from the sample's `main`.\n- The orange `main.load` frame is a routine called from the sample's `main`.\n\nThe filter selector lets you do things like filter out functions that match\nsome name. For example, if there is a standard library of utility functions,\nyou can remove them from the graph. You can also remove call stacks\noriginating at a certain method or simplify the graph in other ways.\nThe `main.go` application is simple, so there isn't much to\nfilter out.\n\nEven for a simple application, filters let you hide uninteresting frames so\nthat you can more clearly view interesting frames. For example, in the\nprofiling screenshot for the sample code, the gray frame is slightly\nlarger than the first `main` frame under it. Why? Is there something else\ngoing on that's not immediately apparent because the `main` call stack\nconsumes such an overwhelming percentage of the resources?\nTo view what is occurring outside of the application's `main` routine,\nadd a filter that hides the call stack of the `main` routine.\nOnly 0.227% of the resource consumption occurs outside of `main`:\n\nFor more information on filtering and other ways to explore the profiling\ndata, see [Select the profiles to analyze](/profiler/docs/selecting-profiles).\n| **Note:** If the Profiler agent hasn't uploaded any profiles when you start the interface, Profiler displays the message `No data to show`. The message is automatically replaced with the Profiler interface after profile data is available.\n\nWhat's next\n-----------\n\nFor general information about profiling, see the following documents:\n\n- [Cloud Profiler overview](/profiler/docs/about-profiler)\n- [Profiling concepts](/profiler/docs/concepts-profiling)\n- Read our resources about [DevOps](/devops) and explore [DORA's research program](https://www.devops-research.com/research.html).\n\nFor information on running the Cloud Profiler agent, see:\n\n\u003c!-- --\u003e\n\n- [Profiling Go applications](/profiler/docs/profiling-go)\n- [Profiling Java applications](/profiler/docs/profiling-java)\n- [Profiling Node.js applications](/profiler/docs/profiling-nodejs)\n- [Profiling Python applications](/profiler/docs/profiling-python)\n- [Profiling applications running outside Google Cloud](/profiler/docs/profiling-external)"]]