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

cmd/pprof: Feature: profiling support for regular tests #23811

Closed
dbkaplun opened this issue Feb 13, 2018 · 6 comments
Closed

cmd/pprof: Feature: profiling support for regular tests #23811

dbkaplun opened this issue Feb 13, 2018 · 6 comments

Comments

@dbkaplun
Copy link

It would be great if I could profile tests (or code in general) without having to write new code.

Currently, to profile tests via -cpuprofile, I have to contort my tests to get picked up by the profiler. I have to change all Test* functions to be called Benchmark*. To do so, I run something like this:

find . \
  -iname '*_test.go' \
  -not -path '*/vendor/*' \
    | xargs sed -Ei 's/(func )Test(\w+[(]t \*testing\.)T/\1Benchmark\2B/g'

Clearly not ideal.

However, the output that this generates is exactly what I'm looking for. The function graph is perfectly generated as I would expect. It would be great if I could easily profile tests without jumping through hoops.

Thanks for considering!

@hyangah
Copy link
Contributor

hyangah commented Feb 13, 2018

The -cpuprofile flag should work with Test* functions and there is no need to change them to be Benchmark functions. One caveat is that if the test function ends very quickly, it's possible that the execution is not sampled at all. (The current cpu profile rate is 100Hz.)

Maybe that's why renaming Test functions to Benchmark functions cause to capture the samples - benchmark framework may call the benchmark functions multiple times under the hood, so some samples are captured.

@dbkaplun
Copy link
Author

With go tool pprof cpu.prof, running web gives me this: (go version go1.9.2 darwin/amd64)
screen shot 2018-02-13 at 5 36 56 pm

Exact command:

go test \
  -timeout 20m \
  -v \
  -coverprofile="coverage-\${pkg//\//-}.out" \
  -cpuprofile cpu.prof -bench .  "\$pkg";

@hyangah
Copy link
Contributor

hyangah commented Feb 13, 2018

The -bench flag used in the profile makes only Benchmark* functions run.
Also, see my previous comment - if the tested function is a trivial function to run, it's unlikely that any meaningful sample is collected.

@dbkaplun
Copy link
Author

@hyangah have you tried it yourself? I get the exact same empty output with this command:

go test \
  -timeout 20m \
  -v \
  -coverprofile="coverage-\${pkg//\//-}.out" \
  -cpuprofile cpu.prof  "\$pkg";

The tests are non-trivial, many of which take a few seconds to run.

@hyangah
Copy link
Contributor

hyangah commented Feb 13, 2018

Yes, here is the profile output from tests in src/runtime/trace
something

@dbkaplun
Copy link
Author

The .prof file was being overwritten by another .prof for a package with no tests.

@mikioh mikioh changed the title Feature request: profiling support for regular tests cmd/pprof: Feature request: profiling support for regular tests Feb 21, 2018
@dbkaplun dbkaplun changed the title cmd/pprof: Feature request: profiling support for regular tests cmd/pprof: Feature: profiling support for regular tests May 4, 2018
@golang golang locked and limited conversation to collaborators May 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants