i have a performance issue with NHibernate 3.3 and Firebird 2.5.1. I created a very simple example with ASP.NET MVC and a local (!) Firebird Database.
But following Code takes about 1sec for execution ?!?
var startTickCountWrite = Environment.TickCount;
IRepository<Project> repository = facade.ProjectRepository(null);
for (int i = 1; i <= 250; ++i)
{
var myProject = new Project { ProjectId = i };
repository.Insert(myProject);
}
repository.Commit();
var endTickCountWrite = Environment.TickCount;
If I place the commit() inside the for-loop it takes about 5sec!
Behind Repository and facade is nothing special. I just forward the Project to ISession.Insert.
Project has only ID and ProjectID as properties.
Can anyone tell me whats going wrong?
Thanks,
Andreas
250 objects in 1 second does not sound shockingly slow. For evaluating performance it would also be good to provide some comparison – how long does it take to execute the same amount of SQL directly against Firebird?
NHibernate can in some circumstances batch INSERT statements, but I don’t know if this works on Firebird. http://nhibernate.info/doc/nh/en/index.html#performance-batch-updates The choice of identity generator can also have an effect. Some generators force NHibernate to execute the INSERT statement immediately, which will prevent the use of batching.
From a design point of view you might want to change some things:
IRepository.Insert() is typically named Add(), since it mimics a collection interface (and Insert on collections, if present, usually take an index parameter which is not relevant here of course). Also, Commit() seems out-of-place on the repository, since you will typically have multiple repository instances involved, that share the same transaction and session.
For accurate time measurements, you can use Stopwatch from System.Diagnostics, so you don’t need to convert the value yourself.