forked from DapperLib/Dapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmarks.NHibernate.cs
69 lines (62 loc) · 1.91 KB
/
Benchmarks.NHibernate.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using BenchmarkDotNet.Attributes;
using Dapper.Tests.Performance.NHibernate;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Linq;
using NHibernate.Transform;
using NHibernate.Util;
using System.Linq;
namespace Dapper.Tests.Performance
{
public class NHibernateBenchmarks : BenchmarkBase
{
private IStatelessSession _sql, _hql, _criteria, _linq, _get;
[Setup]
public void Setup()
{
BaseSetup();
_sql = NHibernateHelper.OpenSession();
_hql = NHibernateHelper.OpenSession();
_criteria = NHibernateHelper.OpenSession();
_linq = NHibernateHelper.OpenSession();
_get = NHibernateHelper.OpenSession();
}
[Benchmark(Description = "SQL")]
public Post SQL()
{
Step();
return _sql.CreateSQLQuery(@"select * from Posts where Id = :id")
.SetInt32("id", i)
.SetResultTransformer(Transformers.AliasToBean<Post>())
.List<Post>()[0];
}
[Benchmark(Description = "HQL")]
public Post HQL()
{
Step();
return _hql.CreateQuery(@"from Post as p where p.Id = :id")
.SetInt32("id", i)
.List<Post>()[0];
}
[Benchmark(Description = "Criteria")]
public Post Criteria()
{
Step();
return _criteria.CreateCriteria<Post>()
.Add(Restrictions.IdEq(i))
.List<Post>()[0];
}
[Benchmark(Description = "LINQ")]
public Post LINQ()
{
Step();
return _linq.Query<Post>().First(p => p.Id == i);
}
[Benchmark(Description = "Get<T>")]
public Post Get()
{
Step();
return _get.Get<Post>(i);
}
}
}