微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇],小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4604字,纯文字阅读大概需要7分钟。
内容图文
![微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]](/upload/InfoBanner/zyjiaocheng/1184/ce2c763fb07c439baf4882cdb29419a5.jpg)
由于这周比较忙,所以本来想做的性能测试,一直没时间,想想还是今天给补上吧
由于很多人都担心性能问题,封装之后跟Dapper的性能差距是多少,今天我给出我的测试方法,仅供参考.
- 创建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"; 4publicstatic DbContext GetContext() 5 { 6returnnew DbContext().ConnectionString(localStr); 7 } 8publicstatic System.Data.IDbConnection GetDapperConnection() 9 { 10returnnew 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; } publicstaticlong 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; } publicstaticlong 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; } publicstaticlong 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次,取平均时长。测试结果如下如。
跟预期差不多是一致。
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; } publicstaticlong 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; } publicstaticlong 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次执行,取平均耗时。
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; } publicstaticlong 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; } publicstaticlong 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; }
总体来说,测试的结果还在预期范围之类,在使用方便和些许的性能中各位抉择。如果有时间的话,考虑换掉Dapper,再重新比较一下。坚持。。。。
原文:http://www.cnblogs.com/Contoso/p/5117383.html
内容总结
以上是互联网集市为您收集整理的微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]全部内容,希望文章能够帮你解决微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。