cordova-cli icon indicating copy to clipboard operation
cordova-cli copied to clipboard

Improve startup speed/performance of CLI

Open janpio opened this issue 6 years ago • 11 comments

Even with telemetry turned off, our CLI seems a bit slow sometimes:

C:\Users\Jan>cordova -v | gnomon
   1.1532s   8.1.2 ([email protected])
   0.0014s

     Total   1.1572s

C:\Users\Jan>node -v | gnomon
   0.0116s   v10.15.1
   0.0010s

     Total   0.0135s

C:\Users\Jan>npm -v | gnomon
   0.6859s   6.4.1
   0.0016s

     Total   0.6893s

C:\Users\Jan>ngrok -v | gnomon
   0.0131s   ngrok version 2.2.8
   0.0017s

     Total   0.0157s

(Yes, I am working on a not very powerful machine here) (ionic is even slower with >7 seconds - but that's no excuse ;) )

janpio avatar Feb 13 '19 19:02 janpio

@janpio Can we get an update with Cordova CLI 9 on the same computer.

Running cordova -v | gnomon five times on my macOS computer, has an average of 0.47674s

erisu avatar Mar 23 '19 08:03 erisu

No improvement:

E:\Projects           
λ cordova -v | gnomon 
   1.5790s   9.0.0    
   0.0019s            
                      
     Total   1.5849s  
                      
E:\Projects           
λ cordova -v | gnomon 
   1.6072s   9.0.0    
   0.0019s            
                      
     Total   1.6105s  
                      
E:\Projects           
λ cordova -v | gnomon 
   1.5453s   9.0.0    
   0.0026s            
                      
     Total   1.5522s  
                      
E:\Projects           
λ cordova -v | gnomon 
   1.5194s   9.0.0    
   0.0019s            
                      
     Total   1.5253s  
                      
E:\Projects           
λ node -v | gnomon    
   0.0117s   v10.15.1 
   0.0016s            
                      
     Total   0.0159s  

(Windows 10)

janpio avatar Mar 25 '19 11:03 janpio

It seems like telemetry negatively affects this:

$ cordova -v | gnomon
   1.3160s   9.0.0
   0.0005s   
             
     Total   1.3167s
$ CI=1 cordova -v | gnomon
   0.4831s   9.0.0
   0.0005s   
             
     Total   0.4842s

dpogue avatar Mar 25 '19 20:03 dpogue

My timings were with telemetry already disabled, with it on it adds ~0.2 to ~0.5 seconds.

The relevant bit for me here was how cordova -v is slower than other Node based CLIs like npm or ngrok.

janpio avatar Mar 25 '19 21:03 janpio

I did some profiling and about half of the time is spent by require('insight'). When you drill down a bit, you can see that the actual culprit that causes the long loading time is Rx.js which is required by inquirer, a dependency of insight.

Roughly the other half is spent on loading cordova-lib. There's a few things I wanted to do about that for some time, but I haven't gotten to it. The Problem with improving the loading time there is that a lot of modules require way more than they would have to. I have some WIP to improve that, but it's quite some work since test doubles are affected when changing requires etc.

My times for cordova -v are as follows:

  • cordova@latest: 1050 ms
  • Without loading insight: 700ms
  • Without loading insight and cordova-lib: 190ms
  • Running an empty bin/cordova: 100ms

raphinesse avatar Apr 10 '19 23:04 raphinesse

Follow-up: inquirer already fixed the problem by only importing what they use from rxjs. But insight does still use an older version of inquirer. Edit: tested with the latest version of inquirer but it only improves the loading time by around 100ms. They are working on a major refactor that should improve it even more though.

For cordova-lib cordova.serve was the biggest offender regarding module load time. However, this is to be taken with a grain of salt because of Node's module caching. Meaning if you don't load cordova.serve, other modules that share the same dependencies could take longer to load, since their dependencies haven't been cached by loading cordova.serve.

raphinesse avatar Apr 11 '19 12:04 raphinesse

Well the question in the title is answered. Should we close this or rename it to something more actionable?

raphinesse avatar Apr 11 '19 20:04 raphinesse

Renamed to a task that benefits of both the measurements and your analysis.

janpio avatar Apr 11 '19 20:04 janpio

See also the discussion from a while back at https://github.com/apache/cordova-discuss/pull/70 (and subsequent PR to remove lazy-loading from cordova-lib: https://github.com/apache/cordova-lib/pull/562)

dpogue avatar Apr 17 '19 04:04 dpogue

Thanks for the links @dpogue! I think the reason while lazy loading did not bring any real benefits as it was being dropped is that you basically load almost everything of lib, once you load something. That can be improved. I have some work in progress regarding that lying around. Don't know when I'll get to continue it though.

raphinesse avatar Apr 17 '19 07:04 raphinesse