# Golang Profiling
Tags: go, profiling
Published: 2022-02-22 05:48:04

## Golang Profiling

Profiling is a form of analyzing the program for optimizable code or functions. In software engineering, it is an essential task since optimization is a key factor when developing an application. 
Avoiding memory leaks and optimizing for better performance is almost always a target for enterprise-level software.

Profiling is an important task that cannot be avoided for larger applications. Profiling helps us understand CPU and memory intensive code and helps us write better code for optimization

To create any profile first we need to have a test file. Here we are going to use the Fibonacci function to see profiles of it

```
// main.go
package main
 
func Fib2(n int) uint64 {
    if n == 0 {
        return 0
    } else if n == 1 {
        return 1
    } else {
        return Fib2(n-1) + Fib2(n-2)
    }
}
 
func main() {
    // fmt.Println(Fib2(30)) // 832040
}
```

Now, the test file is:

```
// main_test.go
package main
 
import "testing"
 
func TestGetVal(t *testing.T) {
    for i := 0; i < 1000; i++ {             // running it a 1000 times
        if Fib2(30) != 832040 {
            t.Error("Incorrect!")
        }
    }
}
```

Now when we run the go test we got the below output

```
output:

go test
Pass
Ok
```

It took almost 7.25s to complete. Now let’s create a CPU profile. We will use this command shown below to generate a profile file.

```
go test -cpuprofile cpu.prof -bench . 
```

which will return the below output

```
PS C:\work\latest\go-tutorial\profile> go test -cpuprofile cpu.prof -bench .
PASS
ok      profile 8.052s
```

Now, we will view it using the pprof tool. The command will be:

```
go tool pprof cpu.prof
```

When we run the above commands we got the below output

```
PS C:\work\latest\go-tutorial\profile> go tool pprof cpu.prof
Type: cpu
Time: Feb 22, 2022 at 10:53am (IST)
Duration: 7.64s, Total samples = 4.86s (63.60%)
Entering interactive mode (type "help" for commands, "o" for options")
```

Typing help will show all commands available. We will run the following command

```
top5 -cum
```

The topN function shown top N entries and the -cum flag shows the cumulative time taken

```
(pprof) top5 -cum
Showing nodes accounting for 4700ms, 96.71% of 4860ms total
Dropped 33 nodes (cum <= 24.30ms)
Showing top 5 nodes out of 15
      flat  flat%   sum%        cum   cum%
    4700ms 96.71% 96.71%     4700ms 96.71%  profile.Fib2
         0     0% 96.71%     4700ms 96.71%  profile.TestGetVal
         0     0% 96.71%     4700ms 96.71%  testing.tRunner
         0     0% 96.71%       90ms  1.85%  runtime.mcall
         0     0% 96.71%       90ms  1.85%  runtime.park_m
```

based on the above results we will optimize our code.

To create a memory profile we simply use this command:

```
go test -memprofile mem.prof -bench . 
```

which will return the below output

```
PS C:\work\latest\go-tutorial\profile> go test -memprofile mem.prof -bench .
PASS
ok      profile 0.644s
```

We can generate both profiles (main, test) using the following command

```
go test -cpuprofile cpu.prof -memprofile mem.prof -bench . 
```

which will return the below output

```
PS C:\work\latest\go-tutorial\profile> go test -cpuprofile cpu.prof -memprofile mem.prof -bench .
PASS
ok      profile 0.655s 
```