微型 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

相关文章
相关标签/搜索