angularjs and karma performance memory problems

Give me the answer now: switch Karma to run Firefox not Chrome

More details
Our team is building a pretty complex angularjs app.
We write unit tests and run them using Karma.
The unit tests are run on a file change watch and just run in the background. We have almost 2000 unit tests.
Its all been working great until recently when the test run gets much slower after each run. Its now at a point that by the 3rd karma run, karma grounds to a halt i.e. 1 run 15 secs, 2nd run 30 secs, 3rd run 1min +
I should not the app itself is not exhibiting significant memory issues (although profiling and tuning is needed) just the unit tests.


Step 1 - find out whats happening with our Karma tests?
Using google Chrome Task Manager, monitor the Karma browser tab.
The monitor shows us memory and cpu (you can also right click and add more columns). This is a rough measure but we could see karma tab:
- memory almost doubled each run 300mb, 600mb, 800mb.
- cpu was maxing out after 3rd run

Step 2 - "its memory stupid"
Ok so to me clearly memory usage is an issue and the cpu may be maxed out trying to deal with memory issues by 3/4th run.
So we need to free resources. Well lots of our tests create new scope but never free it
i.e. scope = $rootScope.$new();
Maybe thats the cause.
So I edited most all of our tests to add: scope.$destroy(); if we previously did a new.
I did measure after updating many of our tests.
I did not notice a material difference in memory usage. This should have been a warning sign.
But I really felt this had to fix it.
Well most all of our tests which new memory now destroy it.
But this change made no difference (bugger!)

Step 3 - selectively run the tests and narrow down the problem
I modified our karma conf to only run subsets of tests; first the filters tests then I added services tests. I'm now running more than half the unit tests and they consistently run under 8 secs. Memory usgae looks good too, it goes up to 300MB but then falls back to approx 250mb
So the problem must be in directives or controllers.

Step 4 - "come out, come out, where-ever you are!"
I ran controller tests only (400+ tests) MB is Karma tab memory and k is javascript memory:
 run 1 5 secs 191MB 125.5k
 run 2 6 secs 288MB 219.6k
 run 3 6.5 secs 377MB 303k
 run 4 6.4 secs 464MB 387k
 run 5 5.4 secs 557MB 472k
 run 6 6.6 secs 501MB 421k
 run 7 5.3 secs 500MB 420k
 run 8 6.4 secs 494MB 413k
When idle it falls back to 378MB and 298k.
Yes memory usage is not good but these tests on their own are not dying.
Running all tests together compounds the problem


Step 5 - is there a limit in Chrome?
What I had noticed from runs is that once tab memory in task manager goes over ~800MB then the unit tests slow to a crawl. Is this some kind of special limit or setting?
I did not find an answer to that question but I did learn more about chrome memory issues e.g. this is very similar to what I see and here as well as others.
So why not try a different browser for Karma?

Step 6 - good old Firefox
I switched Karma to use Firefox: browsers: ['Firefox']
Re-running all the tests is much improved (previously Chrome died at run 3)
run 1 21 secs
run 2 24 secs
run 3 24 secs
run 4 20 secs
run 5 24 secs
run 6 21 secs
run 7 24 secs
run 8 24.5 secs
run 9 20 secs

I have not taken Firefox memory usage measurements but its clearly its managing memory better than Chrome.
That may be good enough for our needs (hopefully).


FYI this is a useful post on stackoverflow about debugging Karma tests which includes an answer from Vojta!

Comments

  1. While I was in online Angularjs training I was introduced to this website by our instructors. Great insight about the subject through presentations along with nice content on this site purely related to data science and cloud. Thanks a lot for the info...

    ReplyDelete
  2. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    workday studio online training

    ReplyDelete
  3. Thank you a bunch for sharing this with all of us you actually realize what you are talking about! Bookmarked. Please also seek advice from my site =). We could have a hyperlink change contract between us! superior safety

    ReplyDelete

Post a Comment

Popular posts from this blog

deep dive into Material UI TextField built by mui

angular js protractor e2e cheatsheet

react-router v6.4+ loaders, actions, forms and more