pkg / profile

Simple profiling for Go
BSD 2-Clause "Simplified" License
1.99k stars 124 forks source link

Timing profiling? #58

Open FrankDMartinez opened 3 years ago

FrankDMartinez commented 3 years ago

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.

davecheney commented 3 years ago

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

FrankDMartinez commented 3 years ago

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

davecheney commented 3 years ago

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

FrankDMartinez commented 3 years ago

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?

davecheney commented 3 years ago

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

FrankDMartinez commented 3 years ago

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.

davecheney commented 3 years ago

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() }