Optimizing Local Application Performance with Google Cloud Profiler : A Practical Guide to Effective Profiling and Leak Detection

Oredata Engineering
3 min readApr 16, 2024

--

Introduction

Google Cloud Profiler stands as an exemplary tool to enhance the performance of your applications by detecting weak spots and system leakages. Continuous monitoring for applications is a must for the projects that requires high availability. Cases such as Memory or CPU leakages leads your application to be unavailable or takes much more time to respond to your client.

For a comprehensive understanding of the types of profiling available and the programming languages supported by Cloud Profiler, checkout the following resource: About Cloud Profiler.

In this post, I will guide you through a practical example where Cloud Profiler was instrumental in identifying a memory leak within a Go application.

Implementing Profiling in Your Code

First lets see how we implemented the profiling to our code:

cfg := profiler.Config{
Service: "go-service",
ServiceVersion: "1.0.13",
// ProjectID must be set if not running on GCP.
ProjectID: "PROJECT-ID-HERE",
DebugLogging: true,
// For OpenCensus users:
// To see Profiler agent spans in APM backend,
// set EnableOCTelemetry to true
// EnableOCTelemetry: true,
}

// Profiler initialization, best done as early as possible.
if err := profiler.Start(cfg); err != nil {
// TODO: Handle error.
}

Step 1: Configuration

To begin, you are afforded the flexibility to customize the ServiceName and ServiceVersion according to your preferences. Prior to running your application locally, it is imperative to execute the following command: gcloud auth application-default login.

Ensure that your terminal’s project setting aligns with the ProjectID configuration value.

Upon successful local authentication, you may launch your application and send one or multiple requests to observe the application’s response. In due course, Cloud Profiler will generate profiling data, and a notification will appear in your terminal, signaling the availability of the profiling results as you can see below.

Now you can stop your application and analyze your results in google cloud profiler.

As depicted in the illustration above, the red lines represent the configuration parameters I specified in the main.go function during the initial setup. At this juncture, you can meticulously analyze and refine the results based on your specific requirements.

As illustrated in the image above, we have the opportunity to observe that memory usage is significantly high, with the majority of it stemming from a specific function (Marshal). In this scenario, we concluded that the library we use for parsing JSON data in our service is slow. Therefore, we decided to remove it from the go routine and handle the JSON data directly, passing the desired data as a parameter to the function after processing. After changing the code according to this, We profile our application again and get the results below.

So the consumption in Marshal function decreased significantly as you can see above.

Conclusion

To wrap this up, Cloud Profiler is a simpler and more user friendly solution to monitor your applications health and detect your performance issues. You can easily implemented to your application rather than using framework or language specific tools. You can also use this solution with Cloud Tools such as GKE, App Engine or Compute Engine. Supported Environments

Links below for more info about the Cloud Profiling:

– Profiling concepts | Cloud Profiler | Google Cloud

– Quickstart: Measure app performance | Cloud Profiler | Google Cloud

Author: Zeyd Bora, Software Engineer, Oredata

Oredata is a premier Google Cloud Partner specialized in

  • Cloud Migration Services
  • Data & Analytics Services
  • Infrastructure Services
  • Google Workspace

If you are interested joining us, feel free to apply our job openings: https://www.linkedin.com/company/oredata/jobs/

--

--

Oredata Engineering

Discover analytics and cloud solutions with Oredata, a Google Cloud Managed Service Provider Partner. Transform your business 👉 www.oredata.com