国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

微型ORM的第二篇DapperLambda性能測試[Dapper比較篇]

2019-11-14 13:34:59
字體:
來源:轉載
供稿:網友

由于這周比較忙,所以本來想做的性能測試,一直沒時間,想想還是今天給補上吧

由于很多人都擔心性能問題,封裝之后跟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次,取平均時長。測試結果如下如。

跟預期差不多是一致。

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次執行,取平均耗時。

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;        }

 

總體來說,測試的結果還在預期范圍之類,在使用方便和些許的性能中各位抉擇。如果有時間的話,考慮換掉Dapper,再重新比較一下。堅持。。。。

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 治多县| 洛浦县| 新安县| 淮滨县| 汉川市| 老河口市| 昂仁县| 禄丰县| 澳门| 湖北省| 浦县| 桐梓县| 齐齐哈尔市| 司法| 拜城县| 比如县| 维西| 永登县| 河西区| 弥渡县| 盐源县| 汨罗市| 清河县| 获嘉县| 海原县| 德安县| 通州区| 集贤县| 远安县| 茂名市| 丰原市| 武功县| 大港区| 富源县| 清远市| 太保市| 巴林右旗| 阿克苏市| 武威市| 乌拉特后旗| 玉林市|