Fran Hoey
Fran Hoey

Reputation: 905

Nancy.Testing seems slow, is there anything I'm should be doing to improve performance?

I love Nancy.Testing, excellent way to test my application. But, I'm finding it's quite slow. One of my test fixtures has 26 tests using the Browser object and it's taking about 1m20sec.

I'm creating a single ConfigurableBootstrapper and Browser objects in the test fixture setup and I'm reusing them for each request (per test fixture). I've tried just loading a single Module rather than all discoverable, but it doesn't make any difference.

I do have a lot of Mocks for my repository interfaces that are loaded into the ConfigurableBootstrapper, surely once they are loaded it shouldn't affect speed? Also, most of the tests use the css selectors, is that known to be slow?

The Environment in a nut shell: Test framework: Nunit Mock framework: Moq Bootstrapper: ConfigutableBootstrapper Nancy Version: 0.23 Test Runner: Resharper/Teamcity

Is there anything should be doing to do to speed up the tests?

Upvotes: 2

Views: 443

Answers (2)

Alexander Petrovskiy
Alexander Petrovskiy

Reputation: 309

I had the same problem. My fourteen tests took more than three minutes. It was so slow that before writing more unit tests I began searching for a solution. This one inspired me to find what is a cause of slowness. Before optimization of tests, my code was:

var browser = new Browser(new DefaultNancyBootstrapper());

After optimization my code became

var browser = new Browser(with => with.Module(new SomeModule()));

or

var browser = new Browser(with => with.Modules(typeof(SomeModule), typeof(AnotherModule)));

That's all. Those tests taking 180+ seconds now need only 3,8 seconds.

Upvotes: 1

Fran Hoey
Fran Hoey

Reputation: 905

Got the answer to this. The problem was the number of dependencies being loaded.

I had the following lines in the ConfigurableBootstrapper

with.AllDiscoveredModules();
with.EnableAutoRegistration();

This loads the whole universe into the test instance.

I removed these lines and manually added the dependencies needed almost test by test. I also did some refactoring in my application to reduce the number of dependencies injected to satisfy each request. E.g. chances are if you are editing a customer record you don't need the products repository, so I split down several classes to be more focused (it was a code smell anyway)

Testing time was reduced from 8 minutes to 1.5 minutes

Word is you can go further:

with.DisableAutoRegistrations();

Upvotes: 4

Related Questions