Skip to content

Instantly share code, notes, and snippets.

@icamys
Last active October 3, 2019 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icamys/1de8eb5059f0d86af36233d4ac2f197e to your computer and use it in GitHub Desktop.
Save icamys/1de8eb5059f0d86af36233d4ac2f197e to your computer and use it in GitHub Desktop.
Golang performance profiling tips with pprof

Here are some tips on profiling golang code with pprof.

Starting profiling server

Run a profiling server in your code on port 20020:

import (
    "net/http"
    _ "net/http/pprof"
    // ...
)

func main() {
    port := 20020

    go func() {
        err := http.ListenAndServe("localhost:"+strconv.Itoa(port), nil)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
    }()

    // ...
}

Memory allocation

  1. Start you program

  2. Connect to your profiling server

    go tool pprof http://localhost:20020/debug/pprof/allocs
  3. Create a memory-allocation graph

    (pprof) svg
    Generating report in profile001.svg
    
  4. Show memory allocations in a specific function, find function by name, for ex. parseRegistrant

    (pprof) list parseRegistrant
    ...
    

CPU usage

First of all we are interested in checking if cpu is intensevely used for garbage collection.

Now we'll take a look at a 30-second CPU profile:

go tool pprof http://localhost:20020/debug/pprof/profile?seconds=30

Note: the profiling slice will be effectively cut only if the program is executing any operations, the program that sleeps will not be profiled

@DarkMristov
Copy link

Missing port definition in - port = 20020

@icamys
Copy link
Author

icamys commented Oct 3, 2019

@DarkMristov fixed! thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment