Skip to content

Commit 183e05f

Browse files
RogerKratzhazzik
authored andcommitted
Test and fix for NH-3141
1 parent c787b4d commit 183e05f

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH3141
2+
{
3+
public class Entity
4+
{
5+
public virtual int Id { get; set; }
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH3141">
5+
<class name="Entity">
6+
<id name="Id" type="int">
7+
<generator class="native"/>
8+
</id>
9+
</class>
10+
</hibernate-mapping>
11+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Diagnostics;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH3141
6+
{
7+
[TestFixture]
8+
public class ProxyIdPerformanceTest : BugTestCase
9+
{
10+
private int id;
11+
12+
protected override void OnSetUp()
13+
{
14+
using (var s = OpenSession())
15+
using (var tx = s.BeginTransaction())
16+
{
17+
id = (int) s.Save(new Entity());
18+
tx.Commit();
19+
}
20+
}
21+
22+
protected override void OnTearDown()
23+
{
24+
using (var s = OpenSession())
25+
using (var tx = s.BeginTransaction())
26+
{
27+
s.CreateQuery("delete from Entity e").ExecuteUpdate();
28+
tx.Commit();
29+
}
30+
}
31+
32+
[Test, Explicit("No logical test - just to compare before/after fix")]
33+
public void ShouldUseIdDirectlyFromProxy()
34+
{
35+
var proxyEntity = CreateInitializedProxy();
36+
37+
const int loop = 1000000;
38+
var watch = new Stopwatch();
39+
watch.Start();
40+
const int dummyValue = 0;
41+
for (var i = 0; i < loop; i++)
42+
{
43+
dummyValue.CompareTo(proxyEntity.Id);
44+
}
45+
watch.Stop();
46+
47+
//before fix: 2.2s
48+
//after fix: 0.8s
49+
Console.WriteLine(watch.Elapsed);
50+
}
51+
52+
private Entity CreateInitializedProxy()
53+
{
54+
using (var s = OpenSession())
55+
using (s.BeginTransaction())
56+
{
57+
var proxyEntity = s.Load<Entity>(id);
58+
NHibernateUtil.Initialize(proxyEntity);
59+
return proxyEntity;
60+
}
61+
}
62+
}
63+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,8 @@
10971097
<Compile Include="MappingByCode\IntegrationTests\NH3105\Model.cs" />
10981098
<Compile Include="MappingByCode\IntegrationTests\NH3105\Fixture.cs" />
10991099
<Compile Include="NHSpecificTest\NH3436\Fixture.cs" />
1100+
<Compile Include="NHSpecificTest\NH3141\Entity.cs" />
1101+
<Compile Include="NHSpecificTest\NH3141\ProxyIdPerformanceTest.cs" />
11001102
<Compile Include="NHSpecificTest\NH941\Domain.cs" />
11011103
<Compile Include="NHSpecificTest\NH941\Fixture.cs" />
11021104
<Compile Include="NHSpecificTest\NH941\FixtureUsingList.cs" />
@@ -3014,6 +3016,7 @@
30143016
<EmbeddedResource Include="NHSpecificTest\NH3132\Mappings.hbm.xml" />
30153017
<EmbeddedResource Include="NHSpecificTest\NH3139\Mappings.hbm.xml" />
30163018
<EmbeddedResource Include="NHSpecificTest\NH2812\Mappings.hbm.xml" />
3019+
<EmbeddedResource Include="NHSpecificTest\NH3141\Mappings.hbm.xml" />
30173020
<EmbeddedResource Include="NHSpecificTest\NH2664\Mappings.hbm.xml" />
30183021
<EmbeddedResource Include="NHSpecificTest\NH2214\Mappings.hbm.xml" />
30193022
<EmbeddedResource Include="NHSpecificTest\NH2960\Mappings.hbm.xml" />

src/NHibernate/Proxy/Poco/BasicLazyInitializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public virtual object Invoke(MethodInfo method, object[] args, object proxy)
7373
{
7474
return IdentityEqualityComparer.GetHashCode(proxy);
7575
}
76-
else if (IsUninitialized && IsEqualToIdentifierMethod(method))
76+
else if (IsEqualToIdentifierMethod(method))
7777
{
7878
return Identifier;
7979
}

0 commit comments

Comments
 (0)