Skip to content

Commit

Permalink
Test and fix for NH-3141
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerKratz authored and hazzik committed Oct 21, 2013
1 parent c787b4d commit 183e05f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3141/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NHibernate.Test.NHSpecificTest.NH3141
{
public class Entity
{
public virtual int Id { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3141/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH3141">
<class name="Entity">
<id name="Id" type="int">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Diagnostics;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3141
{
[TestFixture]
public class ProxyIdPerformanceTest : BugTestCase
{
private int id;

protected override void OnSetUp()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
id = (int) s.Save(new Entity());
tx.Commit();
}
}

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.CreateQuery("delete from Entity e").ExecuteUpdate();
tx.Commit();
}
}

[Test, Explicit("No logical test - just to compare before/after fix")]
public void ShouldUseIdDirectlyFromProxy()
{
var proxyEntity = CreateInitializedProxy();

const int loop = 1000000;
var watch = new Stopwatch();
watch.Start();
const int dummyValue = 0;
for (var i = 0; i < loop; i++)
{
dummyValue.CompareTo(proxyEntity.Id);
}
watch.Stop();

//before fix: 2.2s
//after fix: 0.8s
Console.WriteLine(watch.Elapsed);
}

private Entity CreateInitializedProxy()
{
using (var s = OpenSession())
using (s.BeginTransaction())
{
var proxyEntity = s.Load<Entity>(id);
NHibernateUtil.Initialize(proxyEntity);
return proxyEntity;
}
}
}
}
3 changes: 3 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,8 @@
<Compile Include="MappingByCode\IntegrationTests\NH3105\Model.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3105\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3436\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3141\Entity.cs" />
<Compile Include="NHSpecificTest\NH3141\ProxyIdPerformanceTest.cs" />
<Compile Include="NHSpecificTest\NH941\Domain.cs" />
<Compile Include="NHSpecificTest\NH941\Fixture.cs" />
<Compile Include="NHSpecificTest\NH941\FixtureUsingList.cs" />
Expand Down Expand Up @@ -3014,6 +3016,7 @@
<EmbeddedResource Include="NHSpecificTest\NH3132\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3139\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2812\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3141\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2664\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2214\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2960\Mappings.hbm.xml" />
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Proxy/Poco/BasicLazyInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public virtual object Invoke(MethodInfo method, object[] args, object proxy)
{
return IdentityEqualityComparer.GetHashCode(proxy);
}
else if (IsUninitialized && IsEqualToIdentifierMethod(method))
else if (IsEqualToIdentifierMethod(method))
{
return Identifier;
}
Expand Down

0 comments on commit 183e05f

Please sign in to comment.