2

将 Dapper 与 Enterprise Library Data Access Access 块进行比较,以通过存储过程获取数据。使用 Dapper 时,我看到了大约 40% 的整体性能优势,这有点令人惊讶。

但是,在比较迭代并从 IEnumerable(IDataRecord) 与 IEnumerable(dynamic) 获取数据时,IEnumerable(IDataRecord) 大约快一个数量级。这种行为是否很好理解并且可以预期,或者这里有什么不对的地方?

结果:

IEnumerable(IDataRecord) 在此处输入图像描述

IEnumerable(dynamic) - 使用 dapperObject.propertyName 在此处输入图像描述

现在有趣的部分是,当使用 dapperObject["propertyName"] 时,性能与 IDataRecord 相当。完全不是我所期望的。 在此处输入图像描述

分析代码的相关部分

using System;
using System.Collections.Generic;
using System.Linq;
using Dapper.DataAccess;
using System.Data;
using tophat;

namespace Dapper.TestRunner
{
    class Program
    {
        static void Main(string[] args)
        {
            var connectionString = "data source=WEBDBdev3,1866; User id=hsbmhw;Password=gEner4Y&M;Persist Security Info='true'; initial catalog=myhomeworks;";
            //The following uses Tophat to create a singleton connection instance.
            Database.Install<SqlServerConnectionFactory>(connectionString, ConnectionScope.ByRequest);
            DapperTest();
            DapperTest2();
            EnterpriseLibraryIDataRecordTest();

        }

        private static void DapperTest()
        {
            for (int i = 0; i < 100; i++)
            {
                IEnumerable<dynamic> users = MyRepository.GetUsersDapper();
                PopulateBusinessObjectsDynamic(users);
            }
        }

        private static void DapperTest2()
        {
            for (int i = 0; i < 100; i++)
            {
                IEnumerable<dynamic> users = MyRepository.GetUsersDapper();
                PopulateBusinessObjectsDynamic2(users);
            }
        }

        private static void EnterpriseLibraryIDataRecordTest()
        {
            for (int i = 0; i < 100; i++)
            {
                IEnumerable<IDataRecord> users = MyRepository.GetUsersEntlib();
                PopulateBusinessObjectsIDataRecord(users);
            }
        }

        private static void PopulateBusinessObjectsDynamic(IEnumerable<dynamic> users)
        {
            foreach (var user in users)
            {
                BusinessObject bo = new BusinessObject(user);
            }
        }

        private static void PopulateBusinessObjectsDynamic2(IEnumerable<dynamic> users)
        {
            foreach (var user in users)
            {
                BusinessObject bo = new BusinessObject(user);
            }
        }


        private static void PopulateBusinessObjectsIDataRecord(IEnumerable<IDataRecord> users)
        {
            foreach (var user in users)
            {
                BusinessObject bo = new BusinessObject(user);
            }
        }
    }



    public class BusinessObject
    {

        public DateTime CreateDate { get; set; }
        public String CreateDateString { get; set; }
        public String FirstName { get; set; }
        public bool IsApproved { get; set; }
        public bool IsLockedOut { get; set; }
        public DateTime LastActivityDate { get; set; }
        public DateTime LastLoginDate { get; set; }
        public String LastName {get;set;}
        public String Organization{get;set;}
        public int OrganizationId{get;set;}
        public int PersonId{get;set;}
        public String ProfileLastUpdatedBy{get;set;}
        public DateTime ProfileLastUpdatedDate{get;set;}
        public String RoleName{get;set;}
        public long RowNumber{get;set;}
        public int TotalCount{get;set;}
        public Guid UserId{get;set;}
        public string UserName {get;set;}
        public string UserStatus{get;set;}

        public BusinessObject(dynamic user)
        {
            CreateDate=user.CreateDate;
            CreateDateString = user.CreateDateString;
            FirstName = user.FirstName;
            IsApproved = user.IsApproved;
            IsLockedOut = user.IsLockedOut;
            LastActivityDate= user.LastActivityDate;
            LastLoginDate = user.LastLoginDate;
            LastName = user.LastName;
            Organization = user.organization;
            OrganizationId=user.organization_id;
            PersonId = user.party_id;
            ProfileLastUpdatedBy = user.ProfileLastUpdatedBy;
            ProfileLastUpdatedDate = user.ProfileLastUpdatedDate;
            RoleName = user.RoleName;
            RowNumber = user.RowNumber;
            TotalCount = user.TotalCount;
            UserId = user.UserId;
            UserName= user.UserName;
            UserStatus = user.UserStatus;
        }

        public BusinessObject(bool x, dynamic user)
        {
            CreateDate = user["CreateDate"];
            CreateDateString = user["CreateDateString"];
            FirstName = user["FirstName"];
            IsApproved = user["IsApproved"];
            IsLockedOut = user["IsLockedOut"];
            LastActivityDate = user["LastActivityDate"];
            LastLoginDate = user["LastLoginDate"];
            LastName = user["LastName"];
            Organization = user["organization"];
            OrganizationId = user["organization_id"];
            PersonId = user["party_id"];
            ProfileLastUpdatedBy = user["ProfileLastUpdatedBy"];
            ProfileLastUpdatedDate = user["ProfileLastUpdatedDate"];
            RoleName = user["RoleName"];
            RowNumber = user["RowNumber"];
            TotalCount = user["TotalCount"];
            UserId = user["UserId"];
            UserName = user["UserName"];
            UserStatus = user["UserStatus"];
        }

        public BusinessObject(IDataRecord user)
        {
            CreateDate = (DateTime)user["CreateDate"];
            CreateDateString = (string)user["CreateDateString"];
            FirstName = (string)user["FirstName"];
            IsApproved = (bool)user["IsApproved"];
            IsLockedOut = (bool)user["IsLockedOut"];
            LastActivityDate = (DateTime)user["LastActivityDate"];
            LastLoginDate = (DateTime)user["LastLoginDate"];
            LastName = (string)user["LastName"];
            Organization = (string)user["organization"];
            OrganizationId = (int)user["organization_id"];
            PersonId = (int)user["party_id"];
            ProfileLastUpdatedBy = (string)user["ProfileLastUpdatedBy"];
            ProfileLastUpdatedDate = (DateTime)user["ProfileLastUpdatedDate"];
            RoleName = (string)user["RoleName"];
            RowNumber = (long)user["RowNumber"];
            TotalCount = (int)user["TotalCount"];
            UserId = (Guid)user["UserId"];
            UserName = (string)user["UserName"];
            UserStatus = (string)user["UserStatus"];
        }
    }
}
4

1 回答 1

3

实际上,您似乎两次运行相同的测试;输入 ( users) 是相同的,从这里开始:

    private static void DapperTest()
    {
        for (int i = 0; i < 100; i++)
        {
            IEnumerable<dynamic> users = MyRepository.GetUsersDapper();
            PopulateBusinessObjectsDynamic(users);
        }
    }

    private static void DapperTest2()
    {
        for (int i = 0; i < 100; i++)
        {
            IEnumerable<dynamic> users = MyRepository.GetUsersDapper();
            PopulateBusinessObjectsDynamic2(users);
        }
    }

实际的“我们做什么”是一样的,在这里:

    private static void PopulateBusinessObjectsDynamic(IEnumerable<dynamic> users)
    {
        foreach (var user in users)
        {
            BusinessObject bo = new BusinessObject(user);
        }
    }

    private static void PopulateBusinessObjectsDynamic2(IEnumerable<dynamic> users)
    {
        foreach (var user in users)
        {
            BusinessObject bo = new BusinessObject(user);
        }
    }

所以......我必须得出结论“JIT、数据缓存(在数据库服务器上)程序集加载/验证/融合、连接池和dynamic缓存功能的组合使第二个测试看起来更快”。

请注意,dynamicdapper 的一面无论如何都仅供临时使用。如果您想要 dapper 的最佳外观,您会使用Query<T>,而不是dynamic.

特别是,AFAIK 没有构建 dapper支持dynamicAPI上的字符串索引器。该对象实现IDictionary<string,object>了成员访问,但您需要显式转换为该对象才能使用它 -user["PropName"]如果user输入为dynamic(如果我错了,请告诉我!)。

碰巧的是,未发布的“git”代码(用于dynamicAPI)明显快于当前的“nuget”实现——但这与这个特定问题有点相切。

于 2013-05-22T08:50:13.223 回答