Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to profile benchmarks using the pprof tool?

I want to profile my benchmarks generated by go test -c, but the go tool pprof needs a profile file usually generated inside the main function like this:

func main() {
    flag.Parse()
    if *cpuprofile != "" {
        f, err := os.Create(*cpuprofile)
        if err != nil {
            log.Fatal(err)
        }
        pprof.StartCPUProfile(f)
        defer pprof.StopCPUProfile()
    }

How can I create a profile file within my benchmarks ?

like image 863
Salah Eddine Taouririt Avatar asked Apr 13 '14 21:04

Salah Eddine Taouririt


People also ask

How can you view the profile output in CPU Pprof in the browser?

To view all available profiles, open http://localhost:6060/debug/pprof/ in your browser.

What is profiling and benchmarking?

A benchmark measures your system's performance. This can help determine a system's capacity, show you which changes matter and which don't, or show how your application performs with different data. In contrast, profiling helps you find where your application spends the most time or consumes the most resources.

What is Pprof?

pprof is a tool for visualization and analysis of profiling data. pprof reads a collection of profiling samples in profile. proto format and generates reports to visualize and help analyze the data. It can generate both text and graphical reports (through the use of the dot visualization package).


1 Answers

As described in http://golang.org/cmd/go/#hdr-Description_of_testing_flags you can specify the profile file using the flag -cpuprofile.

For example

go test -cpuprofile cpu.out
like image 138
simon Avatar answered Oct 26 '22 07:10

simon