"import" and const calculations are compile-time activities, whereas init() 
and global var assignment are run-time activities.

How does the time to compile your code to an executable, compare to running 
the tests? If compiling the code is fast but running the tests is slow, 
that rules out any problems with "import".

If it takes "dozens of seconds" to compile your code, that doesn't sound 
right unless either your codebase is huge, or you're running in a very slow 
environment (e.g. virtualization without hardware support like VT-x).

If it takes "dozens of seconds" to start your test suite, that doesn't 
sound right unless you're accidentally compiling and testing all your 
dependent libraries as well as your own code.  Or it could be that your 
test *is* starting quickly, but the first test runs very slowly - maybe 
it's opening network connections or doing DNS resolutions which time out, 
or something like that.

One thing you can try is to use strace to see system calls:

    strace -f -s128 go test ...etc...

This might give you some clues, especially if you see a long pause in the 
strace output (what system call did it do just before the pause?)

Another idea is to do a binary chop.  First, make a copy of your project 
and remove all the tests except for one "hello world" test; see if it's 
fast.  If it is, that shows that it's either building or running your own 
test code which is slow, not the importing of third-party libraries.  Then 
do the same but cut out only the first half of the tests, then repeat 
cutting out only the second half of the tests.  When you've found which 
half is slow, cut that in half again - and so on.

On Wednesday, 31 August 2022 at 08:00:41 UTC+1 ag9920 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. 
>
> 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.
>
> 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/b0ad7306-7c2f-40e6-a631-cb7ee9a4cb6en%40googlegroups.com.

Reply via email to