微型 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 ...
随机推荐
- Yii中的CCheckBoxColumn在widget中的用法
'columns'=>array( array( 'class'=>'CCheckBoxColumn', 'id'=>'us ...
- Wormholes(SPFA+Bellman)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36860 Accepted: 13505 Descr ...
- Android创建启动画面[转]
每个Android应用启动之后都会出现一个Splash启动界面,显示产品的LOGO.公司的LOGO或者开发者信息.如果应用程序启动时间比较长,那么启动界面就是一个很好的东西,可以让用户耐心等待这段枯燥 ...
- hadoop中汉字与英文字符混合的keyword做为combine的key的问题
近期,须要将汉字与字符的非常合串作为combine的输出的key, 这样做是希望,利用hadoop的归并来依照key进行分组,然后,在reduce阶段,拿到的都是一个一个组. 可是,发现,这样的,汉字 ...
- div footer标签css实现位于页面底部固定
Web页面的“footer”部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,让你的页面看上去很不好看,特别是现在宽屏越来越多,这种现象更是常见,本文将介绍两种解决方案,需要了解的朋友可以 ...
- 火星A+B(字符串整形转化,进制)
Description 读入两个不超过25位的火星正整数A和B,计算A+B.需要注意的是:在火星上,整数不是单一进制的,第n位的进制就是第n个素数.例如:地球上的10进制数2,在火星上记为“1,0”, ...
- eclipse - tomcat 远程调试
步骤:前提是tomcat上应用是eclipse打包部署上去的,代码一致. 1,在机器A上部署应用remote-debug之前,需要为机器A上的tomcat配置调试端口.在${tomcat}/bin下加 ...
- prob5 of 140
#include<stdio.h>int main(){ int n,i=1,j=1; double s=1,s1=0;; //scanf("%d",&n); ...
- Storm基础理论
Storm流式计算基础 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB" ...
- Android 开发中常用小技巧
TextView中的getTextSize返回值是以像素(px)为单位的, 而setTextSize()是以sp为单位的. 所以如果直接用返回的值来设置会出错,解决办法是 用setTextSize() ...