NHibernate的大資料量插入的簡單效能測試
阿新 • • 發佈:2019-02-20
[ActiveRecord]
publicclass GuidTestObject
{
[PrimaryKey(PrimaryKeyType.GuidComb)]
publicvirtual Guid Id { get; set; }
[Property]
publicvirtualstring Description { get; set; }
}
[TestFixture]
publicclass PerformanceTest : DatabaseTestFixtureBase
{
[SetUp]
publicvoid TestInitialize()
{
InitializeFramework();
}
[TearDown]
publicvoid TestCleanup()
{
DisposeUnitOfWork();
}
private IList<GuidTestObject> CreateObjects(int count)
{
IList<GuidTestObject > list =new List<GuidTestObject>(count);
for (int i =0; i < count; i++)
{
list.Add(new GuidTestObject{Description = Guid.NewGuid().ToString()});
}
return list;
}
[Test]
[TestCategory("Performance")]
publicvoid Insert60000DataWithADO()
{
// My result is 00:00:12.1994805 on a T7250, 2g laptop machine. var watch =new Stopwatch();
watch.Start();
using (var transaction = UnitOfWork.Current.BeginTransaction())
{
for (int i =0; i <60000; i++)
{
Repository<GuidTestObject>.ExecuteNoneQuery("insert into GuidTestObjects values(@Id, @Description)",
new[]
{
new Parameter("Id", Guid.NewGuid()),
new Parameter("Description",
Guid.NewGuid().ToString())
});
}
transaction.Commit();
}
watch.Stop();
Console.WriteLine(watch.Elapsed);
}
[Test]
[TestCategory("Performance")]
publicvoid Insert60000DataWithNHBatchSize1()
{
// My result is 00:00:20.0452743 on a T7250, 2g laptop machine. show_sql=false, adonet.batch_size=1
var objects = CreateObjects(60000);
var watch =new Stopwatch();
watch.Start();
using (var transaction = UnitOfWork.Current.BeginTransaction())
{
foreach (var o in objects)
{
Repository<GuidTestObject>.Save(o);
}
transaction.Commit();
}
watch.Stop();
Console.WriteLine(watch.Elapsed);
}
[Test]
[TestCategory("Performance")]
publicvoid Insert60000DataWithNHBatchSize100()
{
// My result is 00:00:08.8320574 on a T7250, 2g laptop machine. show_sql=false, adonet.batch_size=100
var objects = CreateObjects(60000);
var watch =new Stopwatch();
watch.Start();
using (var transaction = UnitOfWork.Current.BeginTransaction())
{
foreach (var o in objects)
{
Repository<GuidTestObject>.Save(o);
}
transaction.Commit();
}
watch.Stop();
Console.WriteLine(watch.Elapsed);
}
[Test]
[TestCategory("Performance")]
publicvoid Insert60000DataWithNHWithStatelessSession()
{
// My result is 00:00:04.8881699 on a T7250, 2g laptop machine. show_sql=false, adonet.batch_size=100
var objects = CreateObjects(60000);
var watch =new Stopwatch();
watch.Start();
using (var session = UnitOfWork.GetSessionFactoryFor(typeof(GuidTestObject)).OpenStatelessSession())
{
using (var transaction = session.BeginTransaction())
{
foreach (var o in objects)
{
session.Insert(o);
}
transaction.Commit();
}
}
watch.Stop();
Console.WriteLine(watch.Elapsed);
}
}