Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timing profiling? #58

Open
FrankDMartinez opened this issue Apr 2, 2021 · 7 comments
Open

Timing profiling? #58

FrankDMartinez opened this issue Apr 2, 2021 · 7 comments

Comments

@FrankDMartinez
Copy link

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
Copy link
Member

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
Copy link
Author

@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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants