Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Optimizing unit tests that use caching
Nid_fr
#1 Posted : Friday, November 7, 2014 9:38:51 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 9/1/2014(UTC)
Posts: 5
Location: France

Hi all,

We've been happily using NCrunch for a while now and our test catalog has grown dramatically. So much that we are now having performance issues, making continuous testing a lot less continuous :(

So I've been profiling unit tests, optimized a few here and there but the main issue seems to be that anything that uses caches to work smoothly is going to face a big performance hit when using NCrunch because of the way it aggressively recycles things. Caches get invalidated between tests and in the end it runs costly code multiple times instead of just once when using a traditional test runner. Using multiple processes and threads amplifies the problem.

Mocking frameworks for example are costly. Creating a mock usually means generating a proxy with Castle, and it has a large footprint on the first call, around ~100-200ms for mocks of big interfaces (YMMV). Subsequent calls are almost instantaneous though. Anything that needs to generate code will have the same issue, think Regex compilation, serialization frameworks, etc.


For the moment I've worked around the problem by adding a category to exclude theses tests from continuous testing while still running it on the software factory, but it's a bit unsatisfactory.

Is there a way to solve this ? Some way to force NCrunch to run a specific set of tests without recycling anything for example ? I've tried a few things like [Isolated] but it didn't seem to do what I wanted.

Cheers,

J.

Remco
#2 Posted : Friday, November 7, 2014 10:03:03 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,970

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Hi, thanks for posting!

At the moment NCrunch doesn't have any feature that specifically caters towards this .. but I can think of a way that you can greatly improve the efficiency of the engine when working with these specific tests. It involves separating the tests out from the rest of the suite and instructing the engine to avoid executing them in parallel, as this would increase the number of task runners in use for the tests and therefore increase caching cost. Try the following:

1. Turn up your process pool size to a reasonably high number (5-10 depending upon how much parallel execution you perform and the number of test projects involved)
2. Move all the tests that require this caching into their own test project
3. Mark this test project with an assembly-level ExclusivelyUsesAttribute so that they are all marked as using the same resource

.. In theory, NCrunch should then start up one task runner process that targets these tests, and it will run them all using this one process. If the process pool size isn't set high enough, NCrunch may occasionally recycle this process depending upon the sequence of tests in the processing queue.
Mharlin
#3 Posted : Friday, January 9, 2015 1:41:11 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/27/2011(UTC)
Posts: 3
Location: Göteborg, Sweden

Was thanked: 1 time(s) in 1 post(s)
How do you set a test project to be assembly-level ExclusivelyUsesAttribute?
Remco
#4 Posted : Friday, January 9, 2015 9:12:17 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,970

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Hi,

Just declare the attribute at assembly-level inside the AssemblyInfo.cs file in your test project. For example:

[assembly: NCrunch.Framework.ExclusivelyUses("Resource")]

.. It will then be automatically added by NCrunch to all tests within the assembly.
Mharlin
#5 Posted : Tuesday, January 13, 2015 9:31:33 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/27/2011(UTC)
Posts: 3
Location: Göteborg, Sweden

Was thanked: 1 time(s) in 1 post(s)
Thanks Remco, worked like a charm :-)
1 user thanked Mharlin for this useful post.
Remco on 1/13/2015(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.038 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download