由于这周比较忙,所以本来想做的性能测试,一直没时间,想想还是今天给补上吧

由于很多人都担心性能问题,封装之后跟Dapper的性能差距是多少,今天我给出我的测试方法,仅供参考.

  1. 创建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比较篇]的更多相关文章

  1. 微型 ORM 的第一篇 DapperLambda发布

    引言:因为接触过多个ORM,但使用的时候都遇到了各自的一些不够理想的地方,从最早开始开始公司自己分装的,到后面用EF,以及Dapper和DapperExtensions  到现在用的FluentDat ...

  2. FluentData微型ORM

    最近在帮朋友做一个简单管理系统,因为笔者够懒,但是使用过的NHibernate用来做这中项目又太不实际了,索性百度了微型ORM,FluentData是第一个跳入我眼睛的词.简单的了解下FluentDa ...

  3. ORM增删改查并发性能测试2

    前言 上一篇<ORM增删改查并发性能测试>出现了点小失误,有的输出SQL日志的代码没有禁用,数据库连接字符串可能有问题.统一环境,统一代码后,重新写一篇. 这次重点是并发性能测试,真不是为 ...

  4. ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

    前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...

  5. 视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

    说好的给园子里的朋友们录制与<ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库> 这篇博客相对应的视频,由于一个月一来没有时 ...

  6. 【译】微型ORM:PetaPoco【不完整的翻译】

    PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...

  7. 【译】微型ORM:PetaPoco

    PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...

  8. 【译】微型ORM:PetaPoco【不完整的翻译】(转)

    出处:http://www.cnblogs.com/youring2/archive/2012/06/04/2532130.html PetaPoco是一款适用于.Net 和Mono的微小.快速.单文 ...

  9. 微型ORM:PetaPoco 学习资料整理

    github地址:https://github.com/CollaboratingPlatypus/PetaPoco petapoco 实体中字段去掉关联(类似于EF中的NotMap) 微型ORM:P ...

随机推荐

  1. Android UI ActionBar功能-启动ActionBar

    官方帮助文档:http://wear.techbrood.com/training/basics/actionbar/index.html ------------------------------ ...

  2. 获取枚举Name,Value,Description两种方法

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  3. 第四章SignalR自托管主机

    第四章SignalR自托管主机 SignalR服务器通常在IIS的Asp.Net应用程序上承载,但它也可以使用自托管库来作为自托管的主机来运行(就像控制台应用程序或Windows服务那样)与Signa ...

  4. 配置Nutch模拟浏览器以绕过反爬虫限制

    原文链接:http://yangshangchuan.iteye.com/blog/2030741 当我们配置Nutch抓取 http://yangshangchuan.iteye.com 的时候,抓 ...

  5. mac nodejs&npm 安装

    https://www.baidu.com/link?url=Ekv7EzWuMOXjIqFL_ewddWzdahU7jMAsWY4gOGOjMtC&ie=UTF-8&wd=nodej ...

  6. RabbitMQ安装后启动出错:- unable to connect to epmd on blockstorage: timeout (timed out)

    具体出错信息如下: [root@blockstorage ~]# rabbitmqctl change_password guest RABBIT_PASS Changing password for ...

  7. IOS app启动过程

    1.main函数   2.UIApplicationMain * 创建UIApplication对象 * 创建UIApplication的delegate对象   3.delegate对象开始处理(监 ...

  8. 学习python 一些错误记录

    1. TypeError: 'unicode' object is not callable当遇到这样的错误时候, 一般是属性当做方法调用了,比如,selenium 脚本, driver.title ...

  9. Mac Please try running this command again as root/Administrator.

    mac 终端安装程序,需要权限,出现以下提示语句: Please try running this command again as root/Administrator. 需要执行以下命令即可: s ...

  10. mysql函数操作(2)

    <?php $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); $dbh->s ...