ORM框架示例及查询测试,上首页修改版(11种框架)

继上次ORM之殇,我们需要什么样的ORM框架? 

整理了11个ORM框架测试示例,通过示例代码和结果,能很容易了解各种框架的特性,优缺点,排名不分先后

  • EF
  • PDF
  • XCODE
  • CRL
  • NHibernate
  • MySoft
  • Moon
  • Cyq
  • Dapper
  • IBatisNet
  • LoognOrm

框架风格

  • 完全对象化,以Linq&Lambda语法为依托 EF,CRL,NHibernate
  • 只有结果对象化,Cyq,Dapper,IBatisNet,LoognOrm,PDF(以方法参数形式传递条件)
  • 使用代理类实现自定义语法 Moon,MySoft,XCODE(需要生成代理类,构造自已的查询语法)
    语法实现方式也有很多种,详细可看测试类调用,便捷性也会有很大区别

上面的框架里,以参数形式传值,效率可能高点,但编程性和便捷性就会差点

 

部署配置

  • 需供助工具生成MODEL Moon,MySoft,PDF,XCODE
  • 需要定义对象映射 NHibernate,IBatisNet,IBatisNet配置起来很麻烦
  • 只需写MODEL EF,CRL,Cyq,Dapper,LoognOrm

开放数据连接

  有些ORM只提供了从config文件读取数据连接,这样做很是不方便(最新版有没有不知)

  不能以编程方式实现的有 IBatisNet,Moon,PDF,XCODE

 

测试目的

  1. 测试ORM数据访问性能,比较简单粗暴,统计对象映射转换和返回时间,(纯对象化的ORM用时会比用参数形式的ORM长)
  2. ORM开发调用实现方式,不同ORM开发风格和便捷性一目了然

测试结果

  • EF和NHibernate平均下来稍慢点,其它区别不大
  • 实现方式纯对象化操作的ORM会更方便点
  • 需要手动映射对象的开发起来会更麻烦

测试实现

为了体现ORM过程,尽量使用对象来表示

各种ORM属性实现方式可能不一样,所以定义成了接口

实体接口

public interface IProduct

    {

        int Id { get; set; }

        string ProductId { get; set; }

        string ProductName { get; set; }

        string BarCode { get; set; }

        DateTime AddTime { get; set; }

    }

测试接口

这里只定义了参数index,在方法里按index创建对应的对象

public interface ITester

    {

        IProduct GetProduct(int index);

        string Remark { get; }

        bool Insert(int index);

        int Select(int index);

        int Update(int index);

        int Delete(int index);

    }

接口实现示例

为了测试ORM性能,尽量不拼接SQL,这将直接影响结果

public class CRLTester : ITester

    {

        public string Remark

        {

            get

            {

                return "";

            }

        }

        public IProduct GetProduct(int index)

        {

            return new Product() { Id = index, ProductName = "ProductName" + index, BarCode = "BarCode" + index, AddTime = DateTime.Now };

        }

        public bool Insert(int index)

        {

            var data = GetProduct(index) as Product;

            ProductManage.Instance.Add(data);

            return true;

        }



        public int Select(int index)

        {

            var data = GetProduct(index) as Product;

            var list = ProductManage.Instance.QueryList(b => b.ProductName == data.ProductName);

            return list.Count;

        }



        public int Delete(int index)

        {

            var data = GetProduct(index) as Product;

            var n = ProductManage.Instance.Delete(b => b.Id == data.Id);

            return n;

        }





        public int Update(int index)

        {

            var data = GetProduct(index) as Product;

            //因为不是查询出来的,手动设置哪些属性被更改了

            data.Change(b => b.ProductName);

            data.Change(b => b.BarCode);

            return ProductManage.Instance.Update(data);

        }

    }

测试过程,调用接口实现,对增删改查循环执行指定次数

static string DoTest(ITester tester, TestType type,int n)

        {

            Stopwatch sw = new Stopwatch();

            //插入

            sw.Start();

            for (int i = 1; i <= n; i++)

            {

                switch (type)

                {

                    case TestType.DELETE:

                        tester.Delete(i);

                        break;

                    case TestType.INSERT:

                        tester.Insert(i);

                        break;

                    case TestType.SELECT:

                        tester.Select(i);

                        break;

                    case TestType.UPDATE:

                        tester.Update(i);

                        break;

                }

            }

            sw.Stop();

            var times = sw.ElapsedMilliseconds;

            var avg = times / Convert.ToDouble(n);

            return string.Format("{0},用时 {1} 毫秒 平均 {2}", type, sw.ElapsedMilliseconds, avg);

        }

使用本地数据库local测试,结果如下(仅供参考)

EF 100次 开始测试 2015/4/13 16:28:48

INSERT,用时 1459 毫秒 平均 14.59

SELECT,用时 555 毫秒 平均 5.55

UPDATE,用时 483 毫秒 平均 4.83

DELETE,用时 178 毫秒 平均 1.78

===================================

CRL 100次 开始测试 2015/4/13 16:28:54

INSERT,用时 144 毫秒 平均 1.44

SELECT,用时 190 毫秒 平均 1.9

UPDATE,用时 81 毫秒 平均 0.81

DELETE,用时 172 毫秒 平均 1.72

===================================

PDF 100次 开始测试 2015/4/13 16:28:58

INSERT,用时 173 毫秒 平均 1.73

SELECT,用时 129 毫秒 平均 1.29

UPDATE,用时 153 毫秒 平均 1.53

DELETE,用时 51 毫秒 平均 0.51

===================================

XCODE 100次 开始测试 2015/4/13 16:29:01

INSERT,用时 432 毫秒 平均 4.32

SELECT,用时 146 毫秒 平均 1.46

UPDATE,用时 172 毫秒 平均 1.72

DELETE,用时 123 毫秒 平均 1.23

===================================

NHibernate 100次 开始测试 2015/4/13 16:29:11

INSERT,用时 317 毫秒 平均 3.17

SELECT,用时 482 毫秒 平均 4.82

UPDATE,用时 155 毫秒 平均 1.55

DELETE,用时 178 毫秒 平均 1.78

===================================

MySoft 100次 开始测试 2015/4/13 16:29:15

INSERT,用时 374 毫秒 平均 3.74

SELECT,用时 309 毫秒 平均 3.09

UPDATE,用时 318 毫秒 平均 3.18

DELETE,用时 247 毫秒 平均 2.47

===================================

Moon 100次 开始测试 2015/4/13 16:29:19

INSERT,用时 185 毫秒 平均 1.85

SELECT,用时 105 毫秒 平均 1.05

UPDATE,用时 81 毫秒 平均 0.81

DELETE,用时 127 毫秒 平均 1.27

===================================

Cyq 100次 开始测试 2015/4/13 16:29:24

INSERT,用时 225 毫秒 平均 2.25

SELECT,用时 131 毫秒 平均 1.31

UPDATE,用时 63 毫秒 平均 0.63

DELETE,用时 304 毫秒 平均 3.04

===================================

Dapper 100次 开始测试 2015/4/13 16:29:28

INSERT,用时 141 毫秒 平均 1.41

SELECT,用时 125 毫秒 平均 1.25

UPDATE,用时 57 毫秒 平均 0.57

DELETE,用时 80 毫秒 平均 0.8

===================================

IBatisNet 100次 开始测试 2015/4/13 16:29:32

INSERT,用时 162 毫秒 平均 1.62

SELECT,用时 111 毫秒 平均 1.11

UPDATE,用时 60 毫秒 平均 0.6

DELETE,用时 94 毫秒 平均 0.94

===================================

LoognOrm 100次 开始测试 2015/4/13 16:29:37

INSERT,用时 109 毫秒 平均 1.09

SELECT,用时 94 毫秒 平均 0.94

UPDATE,用时 96 毫秒 平均 0.96

DELETE,用时 57 毫秒 平均 0.57

===================================

EF插入表现得非常慢,可能是由于它的机制引起的

在上面说的,没有使用语法解析的ORM,用时相对比较短,其它差不多

影响效率的原因

  • 框架内部机制,比如EF,Insert奇慢
  • 对象映射和语法解析时间 ,对象和表映射转换,查询语法解析
  • 生成的语句复杂度和参数化

运行截图

ORM框架示例及查询测试,上首页修改版(11种框架)

以上结果仅作参考,以实际测试结果为准,欢迎留言讨论

测试项目下载地址:http://files.cnblogs.com/files/hubro/ORMTest.rar?a=2

请更改config文件中的数据连接,IBatisNet需单独修改

你可能感兴趣的:(orm)