Member-only story
How to Profile a Golang gRPC Server Using pprof
Find out where you’re fast and where you’re slow

Profiling, with the goal of server performance that’s better than yesterday, has become a crucial part of software development. This piece discusses profiling a gRPC server in Golang with thepprof
tool. If you don’t have prior knowledge of Golang or the gRPC server, get familiar with them before reading this piece.
What is pprof?
Before diving into the details of how to use pprof, let’s get acquainted with it. Here’s their own definition from their website:
pprof is a tool for visualisation and analysis of profiling data. pprof collects the data to generate a report which can be used to visualise and analyse the data. It generates textual as well graphical reports.
Analyzing these reports can help you to detect slow functions in the code. Once we know the time-consuming components of the system, we can optimize to enhance overall performance.
Let’s look at the installation first, then the changes required in the code, then we’ll learn how to generate the report and audit it.
Installation
To install and build it, we’ll use the go get
tool:
go get -u github.com/google/pprof
Code Integration
Once you’ve set up your gRPC server, add these imports in your main file of gRPC server:
import(
"runtime"
"net/http"
_ "net/http/pprof"
)
Then start a new HTTP server along with gRPC server:
go func() {
http.ListenAndServe(":{http_port}", nil)
}()
In this example, we’re using port 8024.
Finally, add the following line at the top of the main function to get where goroutines block waiting on synchronization primitives:
runtime.SetBlockProfileRate(1)
If you want to profile an HTTP server, this is not required.
That’s it — code integration is complete1