rogpeppe / go-internal

Selected Go-internal packages factored out from the standard library
BSD 3-Clause "New" or "Revised" License
823 stars 67 forks source link

testscript: application code doesn't show with `go test -cpuprofile` #253

Closed sentriz closed 1 month ago

sentriz commented 1 month ago

hi there, i'm wondering if it's expected that application code referenced by custom programs in testscript.RunMain() show up in profiles with go test -cpuprofile? i don't see any. which I think makes sense, since testscript seems to create copies of the TestMain binary and exec.Cmd them, where it then switches on os.Args[0]

but is it possible to somehow profile my application code when using testscript? would be great to do a -cpuprofile then open up in pprof

thanks!

mvdan commented 1 month ago

The short answer is that this won't work out of the box. We used to pass -coverprofile flags to child processes for exec to have full support for code coverage in testscript, much like you suggest here for -cpuprofile (or others like -memprofile), but we were thankfully able to delete it when Go started doing it themselves: https://go.dev/doc/build-cover

The old code we had to do this ourselves was very hacky and buggy, and a maintenance burden. So I would rather not get into that again.

If this is something useful to you for integration tests with testscript, it should be useful for all kinds of integration tests, so much like https://go.dev/doc/build-cover, I would suggest to propose or implement it in upstream Go so that it works out of the box with go test -cpuprofile. Note that, just like coverage profiles, pprof profiles (cpu or memory) can be merged as long as it's for the same binary, I think.

sentriz commented 1 month ago

thanks! makes sense - ill take that upstream then :+1: