Back to blog

Golang Profiling: The Basics and a Quick Tutorial

Ofer Dekel

Product Manager, Intel Granulate

What is Golang Profiling? 

Golang profiling is the process of collecting and analyzing data about the performance of a Go program. Profiling can help you identify bottlenecks and inefficiencies in your code, and it can be an important tool for optimizing the performance of your program.

There are several types of profiling that you can perform in Go:

  • CPU profiling: CPU profiling measures the amount of time spent executing different parts of your code. It can help you identify functions or sections of code that are taking too long to execute.
  • Memory profiling: Memory profiling measures the amount of memory being used by your program, and it can help you identify leaks or areas where memory is being used inefficiently.
  • Concurrency profiling: Concurrency profiling measures the performance of your program when using multiple goroutines or channels. It can help you identify issues related to race conditions or deadlocks.

Profiling in Go is typically done using the Google-developed open source library pprof, which is part of the Go standard library. The Go runtime generates profiling data in a pprof-compatible format. You will need to collect this data during testing, and then use pprof to filter it and visualize code paths. 

This is part of a series of articles about Golang performance.

In this article:

The Importance of Profiling in Go

Performance is an important aspect of software development, and profiling is a valuable tool for understanding and improving the performance of Go applications. There are several reasons why profiling is important:

  • Identify bottlenecks: Profiling can help you identify functions or sections of code that are taking too long to execute or using too many resources. This can be especially useful for finding bottlenecks in large and complex programs.
  • Optimize performance: Once you have identified bottlenecks, you can use the information from profiling to optimize the performance of your program. This may involve refactoring your code, using different data structures or algorithms, or optimizing memory usage.
  • Improve user experience: Poor performance can negatively impact the user experience, especially in applications that rely on real-time data or need to handle large amounts of data. By optimizing the performance of your program, you can improve the overall user experience.

It is worth noting that profiling is just one tool in the toolkit for optimizing the performance of Go applications. It is important to use a combination of techniques, such as choosing the right data structures and algorithms, minimizing allocations, and using concurrency effectively, to achieve the best results.

Related content: Read our guide to Golang performance testing (coming soon)

New call-to-action

What is pprof? 

pprof is a tool for profiling Go programs. It is part of the Go standard library and can be used to generate detailed profiles of Go programs, including CPU, memory, and concurrency profiles. It reads profiling samples in the profile.proto format.

To use pprof, you first need to enable profiling in your Go program by calling the StartCPUProfile function. This will begin collecting data about the execution of your program. You can then run your program as normal, and when you are ready to generate the profile, you can call the StopCPUProfile function to stop profiling and save the data to a file.

Once you have saved the profile data to a file, you can use the pprof command line to analyze the data. The pprof tool can generate a variety of reports, including a summary of the CPU usage, a list of the functions that are using the most CPU time, and a flame graph that visualizes the call tree of your program.

pprof is a powerful tool for understanding the performance of Go programs, and it is an essential tool for anyone working on optimizing the performance of Go applications.

Quick Tutorial: Go Profiling With pprof 

To perform profiling of a Go program using pprof, follow these steps:

  1. Enable profiling in your Go program by calling the runtime.StartCPUProfile function at the beginning of your program. This will start collecting data about the execution of your program.
  1. Run your program as normal.
  1. When you are ready to stop profiling and generate the profile data, call the runtime.StopCPUProfile function. This will stop profiling and save the data to a file.
  1. Run the pprof tool on the command line, passing the name of the profile data file as an argument. For example:

    pprof -http=:8080 myprogram.prof

    This will start a web server on port 8080 and display a summary of the profile data.
  1. Open a web browser and navigate to http://localhost:8080/. This will display a summary of the profile data, including a list of the functions that are using the most CPU time and a flame graph that visualizes the call tree of your program.
  1. You can use the pprof tool to generate a variety of different reports. For example, you can use the –text flag to generate a text-based report, or the –svg flag to generate an SVG graph. You can also use the –top flag to display a list of the functions that are using the most CPU time.
New call-to-action
  1. Here is an example of how you might enable profiling in your Go program:
package main

import (
“fmt”
“os”
“runtime/pprof”

)

func main() {

// Start profiling
f, err := os.Create(“myprogram.prof”)
if err != nil {

fmt.Println(err)
return

}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()

// Run your program here

fmt.Println(“Done!”)

}
  1. This code will start profiling when the program starts, and it will stop profiling and save the data to a file when the program exits.

That’s it! You just ran a simple Go program and profiled it using pprof.

Optimize application performance.

Save on cloud costs.

Start Now
Back to blog