Nhibernate Session Statistics: not exchangeable

122 views
Skip to first unread message

Peter Schojer

unread,
Oct 18, 2013, 3:26:28 AM10/18/13
to nhibernate-...@googlegroups.com
Hi,
we were recently looking for a way to do db profiling to identify expensive operations in our application. We use NhProfiler internally but very often you need the performance data in a productive system, also when having many instances of your application it is convenient to have some sort of Performanceoverview showing how much database load is caused by which instance (so we can't NhProf for this).
Now Nhibernate comes with a very extensive performance profiler which gives you nearls every detail you could possibly need but the statistcs impl has some severe disadvantages:
- it is a global statistics thus requiring locking, ie you cannot enable it on a productive system
- you cannot extend it or exchange it with your own implementation

I worked around the latter restriction by extending the session factory to store two statistics members:
        [NonSerialized] 
        private IStatistics statistics;
        [NonSerialized]
        private IStatisticsImplementor statisticsImpl;

And I added a public InitStatistics function:
public void InitStatistics(IStatistics stat, IStatisticsImplementor impl)
        {
            statistics = stat;
            statisticsImpl = impl;
        }

Initially those members are set to the default statistics impl:
private void Init()
        {
            var impl = new StatisticsImpl(this);
            statistics = impl;
            statisticsImpl = impl;
            queryPlanCache = new QueryPlanCache(this);
        }

IStatistics itself was extended with two properties:
- public TimeSpan TotalQueryExecutionTime { getprivate set; } - public long TotalRowsLoaded { getprivate set; }

While this might work for us, I think that a proper solution should allow the session statistics to be configured via the hibernate.cfg.xml file.

So having solved that, how to avoid the performance penalty associated with global locking?

We are working in a Web Context and we want to identify the web requests that consume the most Database time. We store for each request a performance entry in the database (aggregated per page).
Those entries are then transferred to the performance center, the global statistics for the application is then created by simple summing up all rows.
From an implementation point of view we need two Session statistics classes to realise that:
- one decorator that gets another sessionStatistic from HttpContext
- a simpler version of the current SessionStatistics file without the lock

So,
any chance that this could be integrated in Nhibernate 4.0, at  least the configurable session statistics part?
Reply all
Reply to author
Forward
0 new messages