On Wed, Aug 31, 2022 at 9:01 AM ag9920 <jingong.w...@gmail.com> wrote:

> Hi, recently I've been trying to make my unit test faster. It seems too
> much time were spent on initialization. After removing all init() function
> in relevant packages. The speed was still very slow.It even takes dozens of
> seconds before entering my real unit test function.
>
> So I take a look at all the possible factors that might slow down the
> testing. The only possible reason I can think of is the time cost on
> import. Golang needs to import all packages recursively. And in my
> scenario, that's roughly dozens of packages.Maybe initializing const, var
> takes too much time.
>

"dozens of packages" is not a lot. It's very few, actually. It seems
extremely unlikely to me, that if you observe dozens of seconds of startup
time it has anything to do with importing those packages by themselves. IMO
that is only really explainable by something waiting on I/O or sleeping or
something like that in the initialization path.


> Is there any solution that could help me figured out the reason? I didn't
> find any tools that could tell me the time cost on import several packages.
>

You could probably just do a CPU profile. If you exit immediately from your
test functions, then anything appearing in the profile would necessarily be
where the initialization is spent.
You can also try out tracing your test program
<https://blog.gopheracademy.com/advent-2017/go-execution-tracer/>.
Personally, I found that a bit harder to set up and interpret, but it
should give you an exact answer.


> And if that's the case,  import a package does take much time, is it still
> possible for me to speed up my unit test?
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/cd58955e-0c4b-4d56-afd7-1153d7be06dcn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/cd58955e-0c4b-4d56-afd7-1153d7be06dcn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfE%3D1NiYauCtrORRm2hqy3fKC65Ab4%2BuqMBv4EsruGVDCQ%40mail.gmail.com.

Reply via email to