麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

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

2019-11-14 13:34:59
字體:
供稿:網(wǎng)友

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

由于很多人都擔(dān)心性能問題,封裝之后跟Dapper的性能差距是多少,今天我給出我的測試方法,僅供參考.

  1. 創(chuàng)建IDbConnection;(DapperLambda 已經(jīng)把IDbConnection封裝在DbContext,所以創(chuàng)建的是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比較簡單,都是執(zhí)行SQL+參數(shù)。。

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

 

原本計劃是起四個線程,在各自線程里調(diào)用相應(yīng)的方法500次,取總耗時時間,發(fā)現(xiàn)線程啟動的順序,對測試的結(jié)果有明顯的影響。因此改成同步的調(diào)用每個方法500次,取平均時長。測試結(jié)果如下如。

跟預(yù)期差不多是一致。

2.單條數(shù)據(jù)插入的

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

 

循環(huán)500次執(zhí)行,取平均耗時。

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

 

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

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 日韩精品一区二区三区中文 | 日韩视频在线观看免费视频 | 成年人性视频 | 视频在线中文字幕 | 国产精品久久久久久久av | 久久久成人免费视频 | 欧美日韩a∨毛片一区 | 久久草在线看 | 国产成年人小视频 | 免费嗨片首页中文字幕 | 亚洲一级片在线观看 | 欧美激情区 | 国产欧美在线一区二区三区 | 91精品国产九九九久久久亚洲 | 国产日韩在线观看一区 | 欧美亚洲一区二区三区四区 | 高清一区二区在线观看 | a级在线| 午夜视频播放 | 色播视频在线播放 | 亚洲xxx视频 | 免费在线观看国产 | 日韩视频在线观看免费 | 成年人高清视频在线观看 | 亚洲一区二区不卡视频 | 欧美a视频在线观看 | 一区二区国产在线 | 电影一级毛片 | 99精品视频在线导航 | 99热99精品| 狠狠干91| a视频在线播放 | 视频www| 亚洲天堂一级片 | 欧美扩阴视频 | 黄色特级一级片 | 精品一区二区久久久久久久网精 | 97久久精品一区二区三区观看 | videos韩国 | www.777含羞草| xfplay噜噜av |