using pprof visualize performance your code

using pprof visualize performance your code

· json · rss
#golang 
View url →

About

If you want to know how the Go run in your machine, you should try Go tool named Pprof. It can easy to demonstrate the time consume or memory consume in your Go code.

So first you need add some lines to your code just like this:

var profile = flag.String("profile", "", "write cpu file to file")

func main() {

	flag.Parse()

	if *profile != "" {

		f, err := os.Create(*profile)

		if err != nil {

			return

		}

		pprof.StartCPUProfile(f)

		defer pprof.StopCPUProfile()

	}

...

Then you can do usually, build and run it. But the different is you need give the parameter when you run the compiled code : ./main -profile=profile.prof. After the program finish, you will find the result file named profile.prof.

You have two method to check it.

  1. go tool pprof main profile.prof


This method will provide an interactive environment to check the performance data in command line.


also you can generate svg image with command web, but it need you install some modules, you can follow the instruction

2. go tool pprof -http=:8080 profile.prof

It will open a web server. More easy to use like this.

Hope you can improve your code with the performance tool's help.