将Dapper与企业库数据访问访问块进行比较,以便通过存储过程获取数据。使用Dapper时,我发现整体性能优势约为40%,这有点令人惊讶。
然而,当比较迭代和从IEnumerable(IDataRecord)与IEnumerable(动态)获取数据时,IEnumerable(IDataRecord)大约快一个数量级。这种行为是否被很好地理解和预期,或者有什么东西不在这里吗?
结果:
的IEnumerable(IDataRecord)
IEnumerable(动态) - 使用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"];
}
}
}
答案 0 :(得分:2)
你实际上似乎两次运行相同的测试;输入(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
缓存功能的组合使第二次测试看起来更快”
请注意,dapper的dynamic
方面仅供ad-hoc使用。如果你想要精简版的最佳面,你可以使用Query<T>
,而不是dynamic
。
特别是,AFAIK没有精简版支持 dynamic
API上的字符串索引器。该对象为成员访问实现了IDictionary<string,object>
,但您需要明确强制转换为使用它 - 如果user["PropName"]
被键入为user
,则无法dynamic
(如果我错了,请告诉我!)。
实际上,未发布的“git”代码(针对dynamic
API)明显快于当前的“nuget”实现 - 但这与此特定问题相关。