Benchmarks.NHibernate.cs 2.1 KB
Newer Older
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 = "NHibernate: SQL", OperationsPerInvoke = Iterations)]
        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 = "NHibernate: HQL", OperationsPerInvoke = Iterations)]
        public Post HQL()
        {
            Step();
            return _hql.CreateQuery(@"from Post as p where p.Id = :id")
                .SetInt32("id", i)
                .List<Post>()[0];
        }

        [Benchmark(Description = "NHibernate: Criteria", OperationsPerInvoke = Iterations)]
        public Post Criteria()
        {
            Step();
            return _criteria.CreateCriteria<Post>()
                .Add(Restrictions.IdEq(i))
                .List<Post>()[0];
        }

        [Benchmark(Description = "NHibernate: LINQ", OperationsPerInvoke = Iterations)]
        public Post LINQ()
        {
            Step();
            return _linq.Query<Post>().First(p => p.Id == i);
        }

        [Benchmark(Description = "NHibernate: Get<T>", OperationsPerInvoke = Iterations)]
        public Post Get()
        {
            Step();
            return _get.Get<Post>(i);
        }
    }
}