微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]
由于这周比较忙,所以本来想做的性能测试,一直没时间,想想还是今天给补上吧
由于很多人都担心性能问题,封装之后跟Dapper的性能差距是多少,今天我给出我的测试方法,仅供参考.
- 创建IDbConnection;(DapperLambda 已经把IDbConnection封装在DbContext,所以创建的是DbContext)
public class DBHelper
{
private static string localStr = "server=(local);User ID=sa;Password=password01!;Database=LocalDB;Persist Security Info=True;Pooling=true;Max Pool Size=700";
public static DbContext GetContext()
{
return new DbContext().ConnectionString(localStr);
}
public static System.Data.IDbConnection GetDapperConnection()
{
return new System.Data.SqlClient.SqlConnection(localStr);
}
}
2.主要针对几个增删改查,几个做比较,但是用到Dapper比较简单,都是执行SQL+参数。。
1).查询语句的比较
public static long DapperSelect()
{
Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var conn = DBHelper.GetDapperConnection())
{
var item = conn.Query<Models.MobileForTest>("SELECT * FROM MobileForTest mft WHERE mft.ID=@ID", new { ID = r.Next(, ) });
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaSQLSelect()
{ Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var context = DBHelper.GetContext())
{
var item = context.Sql("SELECT * FROM MobileForTest mft WHERE mft.ID=@ID", new { ID = r.Next(, ) }).QueryMany<MobileForTest>();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaSelectByID()
{ Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var context = DBHelper.GetContext())
{
var item = context.Select<MobileForTest>(r.Next(, ));
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaSelectByLambda()
{ Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var context = DBHelper.GetContext())
{
var item = context.Select<MobileForTest>(p => p.ID == r.Next(, )).QueryMany();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
原本计划是起四个线程,在各自线程里调用相应的方法500次,取总耗时时间,发现线程启动的顺序,对测试的结果有明显的影响。因此改成同步的调用每个方法500次,取平均时长。测试结果如下如。

跟预期差不多是一致。
2.单条数据插入的
public static long DapperSQLInsert()
{
Stopwatch timer = new Stopwatch();
timer.Start();
using (var conn = DBHelper.GetDapperConnection())
{
conn.Execute(@"INSERT INTO [dbo].[MobileForTest]
([MobileHolder]
,[MobilePhone]
,[Status])
VALUES
(@MobileHolder
,@MobilePhone
,@Status)", new { MobileHolder = "InsterWithTran", MobilePhone = "", Status = });
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaSQLInsert()
{
Stopwatch timer = new Stopwatch();
timer.Start();
using (var context = DBHelper.GetContext())
{
context.Sql(@"INSERT INTO [dbo].[MobileForTest]
([MobileHolder]
,[MobilePhone]
,[Status])
VALUES
(@MobileHolder
,@MobilePhone
,@Status)").Parameter("MobileHolder", "DapperLambdaSQLInsert")
.Parameter("MobilePhone", "")
.Parameter("Status", ).Execute();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaInsert()
{
List<MobileForTest> ls = new List<MobileForTest>();
Stopwatch timer = new Stopwatch();
timer.Start();
using (var context = DBHelper.GetContext())
{
context.Insert<MobileForTest>(new MobileForTest { MobileHolder = "DapperLambdaInsert", MobilePhone = "", Status = }).Execute();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
循环500次执行,取平均耗时。

3.更新方法测试
public static long DapperSQLUpdate()
{
Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var conn = DBHelper.GetDapperConnection())
{
conn.Execute("UPDATE MobileForTest SET MobileHolder = @MobileHolder WHERE ID=@ID", new { MobileHolder = "DapperSQLUpdate", ID = r.Next(, ) });
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaSQLUpdate()
{
Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var context = DBHelper.GetContext())
{
context.Sql("UPDATE MobileForTest SET MobileHolder = @MobileHolder WHERE ID=@ID", new { MobileHolder = "DapperLambdaSQLUpdate", ID = r.Next(, ) }).Execute();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaUpdate()
{
Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var context = DBHelper.GetContext())
{
context.Update<MobileForTest>().Set(new { MobileHolder = "DapperLambdaUpdate" }).Where(p => p.ID == r.Next(, )).Execute();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}

总体来说,测试的结果还在预期范围之类,在使用方便和些许的性能中各位抉择。如果有时间的话,考虑换掉Dapper,再重新比较一下。坚持。。。。
微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]的更多相关文章
- 微型 ORM 的第一篇 DapperLambda发布
引言:因为接触过多个ORM,但使用的时候都遇到了各自的一些不够理想的地方,从最早开始开始公司自己分装的,到后面用EF,以及Dapper和DapperExtensions 到现在用的FluentDat ...
- FluentData微型ORM
最近在帮朋友做一个简单管理系统,因为笔者够懒,但是使用过的NHibernate用来做这中项目又太不实际了,索性百度了微型ORM,FluentData是第一个跳入我眼睛的词.简单的了解下FluentDa ...
- ORM增删改查并发性能测试2
前言 上一篇<ORM增删改查并发性能测试>出现了点小失误,有的输出SQL日志的代码没有禁用,数据库连接字符串可能有问题.统一环境,统一代码后,重新写一篇. 这次重点是并发性能测试,真不是为 ...
- ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...
- 视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
说好的给园子里的朋友们录制与<ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库> 这篇博客相对应的视频,由于一个月一来没有时 ...
- 【译】微型ORM:PetaPoco【不完整的翻译】
PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...
- 【译】微型ORM:PetaPoco
PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...
- 【译】微型ORM:PetaPoco【不完整的翻译】(转)
出处:http://www.cnblogs.com/youring2/archive/2012/06/04/2532130.html PetaPoco是一款适用于.Net 和Mono的微小.快速.单文 ...
- 微型ORM:PetaPoco 学习资料整理
github地址:https://github.com/CollaboratingPlatypus/PetaPoco petapoco 实体中字段去掉关联(类似于EF中的NotMap) 微型ORM:P ...
随机推荐
- 求解printf函数?
求大神解释一下下面的代码为什么答案不是1 2,而是1 0. #include <stdio.h> int ans = 0; int a() { ans = 1; return ans++; ...
- Hadoop-Yarn-HA集群搭建(搭建篇)
1.前提条件 我学习过程是一块一块深入的,在把hdfs基本弄懂以及HA成功的情况开始尝试搭建yarn的,建议在搭建前先去看一下转载的原理篇,懂了原理后搭建会很快的,再次强调一下hdfs我默认已经搭建成 ...
- ruby on rails出现的问题ActiveModel::ForbiddenAttributesError
首先分清楚我们在搞rails时.看资料和所使用的环境的版本号是否同样.看的资料是rails3.2,电脑配置的环境是4.0,就会出现这样的安全防范措施的问题. 这类问题大多出如今new或者create两 ...
- UI标签库专题十三:JEECG智能开发平台 ckfinder(ckfinder插件标签)
1. ckfinder(ckfinder插件标签) 1.1. 參数 属性名 类型 描写叙述 是否必须 默认值 name string 属性名称 是 null value string 默认值 否 nu ...
- 为何与0xff进行与运算
为何与0xff进行与运算 在剖析该问题前请看如下代码 public static String bytes2HexString(byte[] b) { String ret = "" ...
- 查询SQL中字符串里有多少个逗号
select len(@str)-len(replace(@str,',',''))
- 关于EJB 时间注解与oracle数据库时间格式
EJB中Temporal是时间注解,其中TemporalType是时间注解的枚举类型其中包括 TemporalType类型,请看源码/*** Type used to indicate a speci ...
- Matlab工程
1.matlab设置默认路径 在原来的默认路径(bin)下创建一个名为startup.m的文件,内容为相对路径 cd ..\..\WorkSpace\ 或绝对路径 cd F:\Program\MATL ...
- PHP 提交checkbox表单时 判断复选框是否被选中
function GetTitleImgPath(){ $titleImgPath = ""; if (isset($_POST["titlecheckbox" ...
- OpenCV学习(1)OpenCV简介
简介 OpenCV的全称是:Open Source Computer Vision Library,OpenCV是一个开源的跨平台的计算机视觉库,可以运行在Linux.Windows和Mac OS操作 ...