How to check & improve performance in Go program

This article is about giving a brief overview about the various techniques used to improve the performance of Go program.
After writing your code in Go, before doing anything first thing is to ask question: does the program build at all?
This is because there is no point in doing anything further if a binary can’t be built from your Go program.
So we check if it builds by the command:
go build .
This should create a binary in go/bin
Once this is checked, next is to check if your Go program gives the correct output. This can be checked by writing tests. Then we execute the test file:
go test .
Now that the program successfully builds AND gives the correct expected output, then it makes sense to check its performance. So now we can ask the question: How fast does it run and how much memory does it allocate?
Answer to this lies in benchmark.
Benchmark in Go
Let’s take an example of this following code:
Important thing to notice is the presence of pointers and passing by reference. Because of this we would expect the pointer aa to be passed to the function f1() which would place aa in heap instead of stack. This means there will be some heap allocation.
Its corresponding benchamark can be written as following:
Now that the benchmark is written, it can be run using the command:
- go test -run=XXX -bench . -benchmem
It then gives result like this:
Benchmark shows the average time taken to run an operation. Also it shows the amount of heap allocation per operation. As expected before, there are some allocations per operation due to the passing of a pointer as discussed before.
Now that we know how long the program takes to execute and how much memory it allocates, we can go one level deeper.
Can anything be done to make it more efficient and possibly faster than it is now?
There are 3 types of analysis one can perform to get more information about the program and to find the bottlenecks based on 3 key aspects of programs in general:
Aspect Analysis
————- ——————
Heap allocation -> escape analysis
Throughput -> cpu profiling
Latency -> tracer
Escape Analysis in Go
Escape analysis to know more about heap allocation can be performed by the following command:
- go build -gcflags=-m <filename>.go
This will give details about inlining & which code lines result in escape of variables to heap. In our example the result will be something like this:
CPU profiling in Go
CPU profiling is performed to get more idea about what functions are running in CPU at in the lifespan of a Go program. So it is handy to find out which one of your functions (if any) are responsible for slowing down your overall Go program. Although it doesn’t give any idea about if a Goroutine is ‘sleeping’ or waiting for some other function to get executed.
There are two steps to get the profiling output. First is the create a binary having profiling information with the command:
- go test -run=XXX -cpuprofile cpu.prof -bench .
Note that profiling is run on the benchmark we wrote before. Also the profile file is named ‘cpu.prof’. Next is to visualize the output using the command:
- go tool pprof cpu.prof
This opens a terminal and then if you enter ‘pdf‘ and press enter then it creates a pdf file showing visualization. Click on the link below to see the output.
Using Tracer in Go
Tracer gives idea about the whole timeline of the Go program. Therefore the information it gives can be bit too much, so it should be used if all other methods to check Go performance haven’t helped much. In that case Tracer gives full information about what happens from beginning to end. Tracer is integrated to the scheduler which is responsible for deciding which of the goroutines actually get executed in CPU and which have to wait. Therefore the tracer is able to write nanosecond level log. For example, every time scheduler takes a goroutine out of the cpu, it writes a log of this event.
There are two steps to get output of the tracer. First, generate the trace file using the command:
- go test -trace trace.out <pkgName> -run XXX -bench <BenchmarkFunctionName>
Then view the trace in browser:
- go tool trace trace.out
Once you click to see the trace, it will show the whole timeline of the processes including garbage collections. The overall output can look something like this:
CPU profiling vs Tracer
Tracer stores the history of all goroutines. Therefore, Tracer gives full event context and not just whats happening at the cpu level at a particular time. Although sometimes it can be an overkill to keep trying to find out the bottleneck which in the end may not be too important.
Conclusion
It is good to be aware of all of the above functionalities available at our disposal to make sure we have fine-tuned the performance of our Go program. Not each of these is required for all cases, but it depends on the type of program and how important it is to get to the absolute bottom of the bottleneck.
Interested in learning more Tech? Click here to take my FREE course on Kubernetes. Don’t worry if you don’t know it already, this is for beginners!
