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

因爲這周比較忙,因此原本想作的性能測試,一直沒時間,想一想仍是今天給補上吧app

因爲不少人都擔憂性能問題,封裝以後跟Dapper的性能差距是多少,今天我給出個人測試方法,僅供參考.dom

  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次,取平均時長。測試結果以下如。spa

跟預期差很少是一致。pwa

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

3.更新方法測試server

        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,再從新比較一下。堅持。。。。blog

相關文章
相關標籤/搜索