星空网 > 软件开发 > ASP.net

微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]

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

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

  1. 创建IDbConnection;(DapperLambda 已经把IDbConnection封装在DbContext,所以创建的是DbContext)
 1  public class DBHelper 2   { 3     private static string localStr = "server=(local);User ID=sa;Password=password01!;Database=LocalDB;Persist Security Info=True;Pooling=true;Max Pool Size=700"; 4     public static DbContext GetContext() 5     { 6       return new DbContext().ConnectionString(localStr); 7     } 8     public static System.Data.IDbConnection GetDapperConnection() 9     {10       return new System.Data.SqlClient.SqlConnection(localStr);11     }12   }

 

   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(1, 3014) });      }      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(1, 3014) }).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(1, 3014));      }      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(1, 3014)).QueryMany();      }      timer.Stop();      return timer.ElapsedMilliseconds;    }

 

原本计划是起四个线程,在各自线程里调用相应的方法500次,取总耗时时间,发现线程启动的顺序,对测试的结果有明显的影响。因此改成同步的调用每个方法500次,取平均时长。测试结果如下如。

微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]

跟预期差不多是一致。

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 = "18922223333", Status = 0 });      }      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", "18912345678")                        .Parameter("Status", 0).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 = "18911112222", Status = 0 }).Execute();      }      timer.Stop();      return timer.ElapsedMilliseconds;    }

 

循环500次执行,取平均耗时。

微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]

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(1, 10000) });      }      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(1, 10000) }).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(1, 10000)).Execute();      }      timer.Stop();      return timer.ElapsedMilliseconds;    }

 

微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]

总体来说,测试的结果还在预期范围之类,在使用方便和些许的性能中各位抉择。如果有时间的话,考虑换掉Dapper,再重新比较一下。坚持。。。。

 




原标题:微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

世界杯侵权:https://www.goluckyvip.com/tag/12662.html
世界杯选品:https://www.goluckyvip.com/tag/12663.html
关税预付:https://www.goluckyvip.com/tag/12665.html
世界杯球衣:https://www.goluckyvip.com/tag/12666.html
华为搬离深圳:https://www.goluckyvip.com/tag/12667.html
世界杯赛程表:https://www.goluckyvip.com/tag/12668.html
小米SU7的不甘心全写进了 “周边”的商品链接里 :https://www.kjdsnews.com/a/1836549.html
10天涨粉300+万 “王婆说媒”凭什么一夜爆红? :https://www.kjdsnews.com/a/1836550.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流