Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automated profiling/unit testing the NHibernate behaviors

I'm using NHibernate quite intensively for few months and I find it very powerful, but for me this is the kind of tool that can sometimes hurt you. I had a lot of situations that relatively simple change in mapping, in LINQ query or in any code that use my entities led to unexpected behaviors. My most "spectacular success" is having the read operation that did N+1 deletes and inserts just because I was sorting my components collection in code instead of in the database.

Thanks to NHProf I can track some of issues like this, but I feel I can't be sure that after refactoring or small requirement changes my data access layer still behaves correctly and efficiently. Testing/profiling it manually after each change is somehow painful. What I'll expect here is some kind of way to unit test the NHibernate's behavior under the hood, i.e. what SQL is generated, how many queries are done, how many entities loaded, how many tables joined etc.

I know that it may be a bit off the main ORM purpose, but for applications that need to perform well, that low-level knowledge seems to me to be more important than the abstraction.

Are there any implementations similiar to this idea? How can I automate profiling my NHibernate application?

like image 478
NOtherDev Avatar asked May 23 '11 19:05

NOtherDev


1 Answers

Something I have used for this purpose is Statistics.

You can get an IStatistics instance from your session factory, and get lots of performance indicators, including number of queries executed, cache misses, execution times, and many more.

With this, you can set unit/integration tests that perform the adequate calls and verify those indicators are inside the expected reference parameters.

like image 172
Diego Mijelshon Avatar answered Oct 14 '22 05:10

Diego Mijelshon