pkg/profile

Timing profiling?

Opened this issue · 7 comments

Hi. I hope everyone is having a great new year so far.

I was told by a colleague I could use this package for determining how much time is being spent in each function during an application's run. Is this correct and, if so, how? I saw nothing in the documentation which makes this clear if so.

Thanks in advance.

The CPU profile is the one to start with.

If you are using linux you might also find the perf tool (comes with your linux disto, not this project) useful

@davecheney Thanks for the quick reply. :-) Does the defer profile.Start().Stop() need to be placed in every function I want to time test?

If you are using cpu profiling in a go test, support for generating a profile is built into the go test runner, use go test -cpuprofile=c.p

Sorry. I’m not understanding what you are saying. Let me simplify. I have a project with multiple Go files and I want to know how much time The application spends in each function. Can I do that with this package and, if so, how?

I think if you want to profile how much time is spent in the various parts of the program over time, start a profile at the top of main, run your program under the expected load, then control c it and investigate the profile.

if you want to micro benchmark a particular function, write a go benchmark for that function the you can use the go test -cpuprofile flag to obtain a profile of the function under test

That's curious. Most profiling tools I have used allow One to run the application as a whole, perhaps with some additional aspects in order to provide the requisite functionality, and produce that information at the end. Oh, well.

This package lets you turn on profiling for a section of time then emit a profile. The defer start().stop() example is just that, an example. You could do something like this

func main() {
setup()
application()
}

func application() {
stop := profile.Start()
run application
stop()
}