Skip to content

Add a generic batcher for insert/update/delete statements, usable with PostgreSQL and others #1588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Mar 10, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/NHibernate.Test/Ado/BatcherFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private void FillDb()
{
s.Save(new VerySimple {Id = 1, Name = "Fabio", Weight = 119.5});
s.Save(new VerySimple {Id = 2, Name = "Fiamma", Weight = 9.8});
s.Save(new VerySimple {Id = 3, Name = "Roberto", Weight = 98.8 });
tx.Commit();
}
}
Expand All @@ -74,11 +75,14 @@ public void OneRoundTripUpdate()
{
var vs1 = s.Get<VerySimple>(1);
var vs2 = s.Get<VerySimple>(2);
var vs3 = s.Get<VerySimple>(3);
vs1.Weight -= 10;
vs2.Weight -= 1;
vs3.Weight -= 5;
Sfi.Statistics.Clear();
s.Update(vs1);
s.Update(vs2);
s.Update(vs3);
tx.Commit();
}

Expand Down Expand Up @@ -154,9 +158,11 @@ public void OneRoundTripDelete()
{
var vs1 = s.Get<VerySimple>(1);
var vs2 = s.Get<VerySimple>(2);
var vs3 = s.Get<VerySimple>(3);
Sfi.Statistics.Clear();
s.Delete(vs1);
s.Delete(vs2);
s.Delete(vs3);
tx.Commit();
}

Expand Down
191 changes: 191 additions & 0 deletions src/NHibernate.Test/Ado/GenericBatchingBatcherFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using NHibernate.AdoNet;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.Ado
{
[TestFixture]
public class GenericBatchingBatcherFixture : TestCase
{
protected override string MappingsAssembly => "NHibernate.Test";

protected override IList Mappings => new[] {"Ado.VerySimple.hbm.xml"};

protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.BatchStrategy, typeof(GenericBatchingBatcherFactory).AssemblyQualifiedName);
configuration.SetProperty(Environment.GenerateStatistics, "true");
configuration.SetProperty(Environment.BatchSize, "1000");
}

protected override bool AppliesTo(Dialect.Dialect dialect)
{
return !(dialect is FirebirdDialect) &&
!(dialect is Oracle8iDialect) &&
!(dialect is MsSqlCeDialect);
}

[Test]
public void MassiveInsertUpdateDeleteTest()
{
var totalRecords = 1000;
BatchInsert(totalRecords);
BatchUpdate(totalRecords);
BatchDelete(totalRecords);

DbShoudBeEmpty();
}

[Test]
public void BatchSizeTest()
{
using (var sqlLog = new SqlLogSpy())
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
s.SetBatchSize(5);
for (var i = 0; i < 20; i++)
{
s.Save(new VerySimple { Id = 1 + i, Name = $"Fabio{i}", Weight = 1.45 + i });
}
tx.Commit();

var log = sqlLog.GetWholeLog();
Assert.That(FindAllOccurrences(log, "Batch commands:"), Is.EqualTo(4));
}
Cleanup();
}

// Demonstrates a 50% performance gain with SQL-Server, around 40% for PostgreSQL,
// around 15% for MySql, but around 200% performance loss for SQLite.
// (Tested with databases on same machine for all cases.)
[Theory, Explicit("This is a performance test, to be checked manually.")]
public void MassivePerformanceTest(bool batched)
{
if (batched)
{
// Bring down batch size to a reasonnable value, otherwise performances are worsen.
cfg.SetProperty(Environment.BatchSize, "50");
}
else
{
cfg.SetProperty(Environment.BatchStrategy, typeof(NonBatchingBatcherFactory).AssemblyQualifiedName);
cfg.Properties.Remove(Environment.BatchSize);
}
RebuildSessionFactory();

try
{
// Warm up
MassiveInsertUpdateDeleteTest();

var chrono = new Stopwatch();
chrono.Start();
MassiveInsertUpdateDeleteTest();
Console.WriteLine($"Elapsed time: {chrono.Elapsed}");
}
finally
{
Configure(cfg);
RebuildSessionFactory();
}
}

private void BatchInsert(int totalRecords)
{
Sfi.Statistics.Clear();
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
for (var i = 0; i < totalRecords; i++)
{
s.Save(new VerySimple {Id = 1 + i, Name = $"Fabio{i}", Weight = 1.45 + i});
}
tx.Commit();
}
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
}

public void BatchUpdate(int totalRecords)
{
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
var items = s.Query<VerySimple>().ToList();
Assert.That(items.Count, Is.EqualTo(totalRecords));

foreach (var item in items)
{
item.Weight += 5;
s.Update(item);
}

Sfi.Statistics.Clear();
tx.Commit();
}
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
}

public void BatchDelete(int totalRecords)
{
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
var items = s.Query<VerySimple>().ToList();
Assert.That(items.Count, Is.EqualTo(totalRecords));

foreach (var item in items)
{
s.Delete(item);
}

Sfi.Statistics.Clear();
tx.Commit();
}
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
}

private void DbShoudBeEmpty()
{
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
var items = s.Query<VerySimple>().ToList();
Assert.That(items.Count, Is.EqualTo(0));

tx.Commit();
}
}

private void Cleanup()
{
using (var s = Sfi.OpenSession())
using (s.BeginTransaction())
{
s.CreateQuery("delete from VerySimple").ExecuteUpdate();
s.Transaction.Commit();
}
}

private int FindAllOccurrences(string source, string substring)
{
if (source == null)
{
return 0;
}
int n = 0, count = 0;
while ((n = source.IndexOf(substring, n, StringComparison.InvariantCulture)) != -1)
{
n += substring.Length;
++count;
}
return count;
}
}
}
6 changes: 6 additions & 0 deletions src/NHibernate.Test/Async/Ado/BatcherFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public async Task OneRoundTripInsertsAsync()
{
await (s.SaveAsync(new VerySimple {Id = 1, Name = "Fabio", Weight = 119.5}, cancellationToken));
await (s.SaveAsync(new VerySimple {Id = 2, Name = "Fiamma", Weight = 9.8}, cancellationToken));
await (s.SaveAsync(new VerySimple {Id = 3, Name = "Roberto", Weight = 98.8 }, cancellationToken));
await (tx.CommitAsync(cancellationToken));
}
}
Expand All @@ -86,11 +87,14 @@ public async Task OneRoundTripUpdateAsync()
{
var vs1 = await (s.GetAsync<VerySimple>(1));
var vs2 = await (s.GetAsync<VerySimple>(2));
var vs3 = await (s.GetAsync<VerySimple>(3));
vs1.Weight -= 10;
vs2.Weight -= 1;
vs3.Weight -= 5;
Sfi.Statistics.Clear();
await (s.UpdateAsync(vs1));
await (s.UpdateAsync(vs2));
await (s.UpdateAsync(vs3));
await (tx.CommitAsync());
}

Expand Down Expand Up @@ -166,9 +170,11 @@ public async Task OneRoundTripDeleteAsync()
{
var vs1 = await (s.GetAsync<VerySimple>(1));
var vs2 = await (s.GetAsync<VerySimple>(2));
var vs3 = await (s.GetAsync<VerySimple>(3));
Sfi.Statistics.Clear();
await (s.DeleteAsync(vs1));
await (s.DeleteAsync(vs2));
await (s.DeleteAsync(vs3));
await (tx.CommitAsync());
}

Expand Down
Loading