微型 ORM 的第二篇 DapperLambda性能测试[Dapper比较篇]

由于这周比较忙,所以本来想做的性能测试,一直没时间,想想还是今天给补上吧

由于很多人都担心性能问题,封装之后跟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,再重新比较一下。坚持。。。。

 

posted @ 2016-01-09 21:56  林锋  阅读(3414)  评论(9编辑  收藏  举报