HOW TO become a PRO using NHibernate Profiler 1.0

September 15, 2009 | by Maciej Greń

View Comments

How to handle persistence concerns? We like to use the open source solution NHibernate, the well known object-relational mapper for .NET. Last Friday’s release of NHibernate Profiler gained a lot of attention. Let’s have a look what goodies we can get.

What it can do

Let’s make a quick rundown on main features of NHibernate Profiler:

  • SQL formatting and syntax highlighting (the built in formatting feature of NHibernate should be disabled for this, I’ve seen it break this functionality a bit).
  • session factory statistics (you need to enable it in your NHibernate configuration)
  • session usage and activity
  • view loaded entities
  • query execution time (it reports both the overall time and the time spent in the DB, you can see how much time is consumed by NHibernate only)
  • stack trace for query (and you can open the call site in Visual Studio right from the profiler)
  • alerts that validate your usage against best practices and points out common mistakes
  • diffing sessions
  • works with the original Java Hibernate as well
  • and you can also save the profiler output to a file and view it later or send it to someone for inspection

Alerts are probably the most important feature. Right now NHibernate Profiler can report on the following issues:

  • Select N+1
  • Too many database calls per session
  • Unbounded result set
  • Excessive / Large number of rows returned
  • Use of implicit transactions
  • Superfluous <many-to-one> update – use inverse=’true’
  • Large number of individual writes
  • Transaction disposed without explicit rollback / commit
  • Using a single session in multiple threads
  • Too many cache calls per session

You can tweak the thresholds that trigger the alerts in order to better match your application’s particulars.

I think it is worth the money for the SQL formatting feature alone. Just compare what you get with NHProf with totally unreadable SQL from SQL Server Profiler.

MS SQL Server Profiler

MS SQL Server Profiler

SQL formatting & highlighting in NHibernate Profiler

NHibernate Profiler

You will also find alerts extremely useful, no matter whether you are an NHibernate beginner or a seasoned developer. For beginners it is a great way of learning about and preventing common mistakes. For experienced devs, it will help them validate that they use NHibernate according to best practices. After all, it is a pretty complex beast.

And there is this little gem also:
gem
With some voodoo magic NHProf is able to pick up the HTTP request URL in my Castle Monorail web application and assign it to session created in that request. It’s a great feature and it increases readability a lot.

Comparing with SQL Server Profiler

People will probably tend to compare NHibernate Profiler with SQL Server Profiler which is included in paid versions of Micrososft SQL Server. In my opinion, these two are not competitors of any kind. I see them more like complementary tools and you should probably get acquainted with both of them.

From SQL Server Profiler you get more information regarding the server operation, e.g. CPU usage and number of I/O operations, on the other hand, NHibernate Profiler will give you plenty of information about your particular usage of NHibernate itself.
Also remember that SQL Server Profiler is not available in popular and free SQL Server Express. For that matter, NHibernate Profiler will allow you to profile applications that use all RDBMS engines supported by NHibernate (and that is quite a long list), not MS SQL Server only.

Final words

NHibernate Profiler is a commercial project, led by Oren Eini, one of the main developers in NHibernate project. This should be a good indication of its usefulness and quality. If you were smart, you would have already bought a copy for a much discounted price when NHProf had still been in beta period (we had!). The current price is 200€ for a single seat license. It seems rather reasonable, especially when you compare it to the price of other software targeting developers.

All in all, I can only highly recommend it. NHibernate Profiler is a truly indispensable tool for any company or developer working with NHibernate.

Share your opinion and experience with us below or meet us on Twitter: @GOYELLO.

Here at Goyello we are not afraid to use open source software. We choose the best tool for the job without prejudice against free software.

In many of our projects persistence concerns are handled by NHibernate, the well known object-relational mapper for .NET. In NHibernate world, the big news recently is the release of NHibernate Profiler last Friday.

What it can do

Let’s make a quick rundown on NHibernate Profiler main features:

· SQL formatting and syntax highlighting (the built in formatting feature of NHibernate should be disabled for this, I’ve seen it break this functionality a bit).

· session factory statistics (you need to enable it in your NHibernate configuration)

· session usage and activity

· view loaded entities

· query execution time (it reports both the overall time and the time spent in the DB, you can see how much time is consumed by NHibernate only)

· stack trace for query (and you can open the call site in Visual Studio right from the profiler)

· alerts that validate your usage against best practices and point out common mistakes

· diffing sessions

· works with the original Java Hibernate as well

· and you can also save the profiler output to a file and view it later or send it to someone for inspection

Alerts are probably the most important feature. Right now NHibernate Profiler can report on the following issues:

· Select N+1

· Too many database calls per session

· Unbounded result set

· Excessive / Large number of rows returned

· Use of implicit transactions

· Superfluous <many-to-one> update – use inverse=’true’

· Large number of individual writes

· Transaction disposed without explicit rollback / commit

· Using a single session in multiple threads

· Too many cache calls per session

You can tweak the thresholds that trigger the alerts in order to better match your application’s particulars.

I think it is worth the money for the SQL formatting feature alone. Just compare what you get with NHProf with totally unreadable SQL from SQL Server Profiler.

<image or two />

You will also find alerts extremely useful, no matter whether you are an NHibernate beginner or a seasoned developer. For beginners it is a great way of learning about and preventing common mistakes. For experienced devs, it will help them validate that they use NHibernate according to best practices. After all, it is a pretty complex beast.

And there is this little gem also:

<image>

With some voodoo magic NHProf is able to pick up the HTTP request URL in my Castle Monorail web application and assign it to session created in that request. It’s a great feature and it increases readability a lot.

Comparing with SQL Server Profiler

People will probably tend to compare NHibernate Profiler with SQL Server Profiler which is included in paid versions of Micrososft SQL Server. In my opinion these two are not competitors of any kind. I see them more like complementary tools and you should probably get acquainted with both of them.

From SQL Server Profiler you get more information regarding the server operation, e.g. CPU usage and number of I/O operations, on the other hand, NHibernate Profiler will give you plenty of information about your particular usage of NHibernate itself.

Also remember that SQL Server Profiler is not available in popular and free SQL Server Express. For that matter, NHibernate Profiler will allow you to profile applications that use all RDBMS engines supported by NHibernate (and that is quite a long list), not MS SQL Server only.

Final words

NHibernate Profiler is a commercial project, lead by Oren Eini, one of the main developers in NHibernate project. This should be a good indication of its usefulness and quality. If you had been smart, you would have already bought a copy for a much discounted price when NHProf had still been in beta period (we had!). The current price is 200€ for a single seat license. It seems rather reasonable, especially when you compare it to price of other software targeting developers.

All in all, I can only highly recommend it. NHibernate Profiler is a truly indispensable tool for any company or developer working with NHibernate.

  • Hi,
    Can you contact me regarding:
    "(the built in formatting feature of NHibernate should be disabled for this, I’ve seen it break this functionality a bit)."

    I don't see this in my own tests, and I would definitely like to see this issue resolved.
  • maciejgren
    Hi,
    Thanks for your reply.
    I have sent you an answer by email.
blog comments powered by Disqus