Skip to content

Instantly share code, notes, and snippets.

@vas6ili
Created April 29, 2013 07:53
Show Gist options
  • Save vas6ili/5480262 to your computer and use it in GitHub Desktop.
Save vas6ili/5480262 to your computer and use it in GitHub Desktop.
Gives a nice XML representation of NHibernate session factory statistics object
using System;
using System.Linq;
using System.Xml.Linq;
using NHibernate;
namespace NHibernate
{
public static class SessionFactoryExtensions
{
public static XElement GetStatisticsXml(this ISessionFactory sessionFactory)
{
if (sessionFactory == null)
throw new ArgumentNullException("sessionFactory");
var stats = sessionFactory.Statistics;
if (!stats.IsStatisticsEnabled)
return new XElement("NHibernateStatistics", "Session factory statistics is disabled");
return new XElement(
"NHibernateStatistics",
new XElement("StartTime", stats.StartTime),
new XElement("SessionCloseCount", stats.SessionCloseCount),
new XElement("SessionOpenCount", stats.SessionOpenCount),
new XElement("ConnectCount", stats.ConnectCount),
new XElement("TransactionCount", stats.TransactionCount),
new XElement("SuccessfulTransactionCount", stats.SuccessfulTransactionCount),
new XElement("CloseStatementCount", stats.CloseStatementCount),
new XElement("FlushCount", stats.FlushCount),
new XElement("OptimisticFailureCount", stats.OptimisticFailureCount),
new XElement("PrepareStatementCount", stats.PrepareStatementCount),
new XElement("Entities",
new XAttribute("EntityDeleteCount", stats.EntityDeleteCount),
new XAttribute("EntityFetchCount", stats.EntityFetchCount),
new XAttribute("EntityInsertCount", stats.EntityInsertCount),
new XAttribute("EntityLoadCount", stats.EntityLoadCount),
new XAttribute("EntityUpdateCount", stats.EntityUpdateCount),
from e in stats.EntityNames
let s = stats.GetEntityStatistics(e)
select new XElement("Entity",
new XAttribute("RoleName", e),
new XAttribute("DeleteCount", s.DeleteCount),
new XAttribute("FetchCount", s.FetchCount),
new XAttribute("InsertCount", s.InsertCount),
new XAttribute("LoadCount", s.LoadCount),
new XAttribute("OptimisticFailureCount", s.OptimisticFailureCount),
new XAttribute("UpdateCount", s.UpdateCount)
)
),
new XElement("Collections",
new XAttribute("CollectionFetchCount", stats.CollectionFetchCount),
new XAttribute("CollectionLoadCount", stats.CollectionLoadCount),
new XAttribute("CollectionRecreateCount", stats.CollectionRecreateCount),
new XAttribute("CollectionRemoveCount", stats.CollectionRemoveCount),
new XAttribute("CollectionUpdateCount", stats.CollectionUpdateCount),
from c in stats.CollectionRoleNames
let s = stats.GetCollectionStatistics(c)
select new XElement("Collection",
new XAttribute("RoleName", c),
new XAttribute("FetchCount", s.FetchCount),
new XAttribute("LoadCount", s.LoadCount),
new XAttribute("RecreateCount", s.RecreateCount),
new XAttribute("RemoveCount", s.RemoveCount),
new XAttribute("UpdateCount", s.UpdateCount)
)
),
new XElement("Queries",
new XAttribute("QueryCacheHitCount", stats.QueryCacheHitCount),
new XAttribute("QueryCacheMissCount", stats.QueryCacheMissCount),
new XAttribute("QueryCachePutCount", stats.QueryCachePutCount),
new XAttribute("QueryExecutionCount", stats.QueryExecutionCount),
new XElement("QueryExecutionMaxTime", stats.QueryExecutionMaxTime),
new XElement("QueryExecutionMaxTimeQueryString", stats.QueryExecutionMaxTimeQueryString),
from q in stats.Queries
let s = stats.GetQueryStatistics(q)
select new XElement("Query",
new XText(q),
new XAttribute("CacheHitCount", s.CacheHitCount),
new XAttribute("CacheMissCount", s.CacheMissCount),
new XAttribute("CachePutCount", s.CachePutCount),
new XAttribute("ExecutionAvgTime", s.ExecutionAvgTime),
new XAttribute("ExecutionCount", s.ExecutionCount),
new XAttribute("ExecutionMaxTime", s.ExecutionMaxTime),
new XAttribute("ExecutionMinTime", s.ExecutionMinTime),
new XAttribute("ExecutionRowCount", s.ExecutionRowCount)
)
),
new XElement("SecondLevelCache",
new XAttribute("SecondLevelCacheHitCount", stats.SecondLevelCacheHitCount),
new XAttribute("SecondLevelCacheMissCount", stats.SecondLevelCacheMissCount),
new XAttribute("SecondLevelCachePutCount", stats.SecondLevelCachePutCount),
from r in stats.SecondLevelCacheRegionNames
let s = stats.GetSecondLevelCacheStatistics(r)
select new XElement("SecondLevelCacheRegion",
new XAttribute("Name", r),
new XAttribute("CategoryName", s.CategoryName),
new XAttribute("ElementCountInMemory", s.ElementCountInMemory),
new XAttribute("ElementCountOnDisk", s.ElementCountOnDisk),
new XAttribute("HitCount", s.HitCount),
new XAttribute("MissCount", s.MissCount),
new XAttribute("PutCount", s.PutCount),
new XAttribute("SizeInMemory", s.SizeInMemory)
)
)
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment