三 EF 和ado.net 的性能对比.

     网上也有很多人对这些做了性能对比.但不想只参照网上的,我还是要自己做一下性能对比.毕竟每个人的环境都不太一样,而且你要把EF作为自己项目中使用,你首先要对这性能要负责.怎么能随便摘抄一下网上的性能分析呢.

    好了,我首先介绍一下我的测试环境. 数据库 sql server 2008 R2,系统 win 7 . cpu I5. 内存4 g. 数据库装在本地. 我的用的是EF5.0. 数据库的数据,已经有12万多条了.

下面我测试的代码

 首先EF 的测试代码

 

View Code
 1  public class EntityDB
 2     {
 3 
 4         //根据id 查询
 5         public static void getByID()
 6         {
 7             using (Entities ec = new Entities())
 8             {
 9          
10                 //id是数据库中的任意一个id
11                 var id = Parameter.IDs[Parameter.index];
12                 ec.TestEnt.Where(ent => ent.ID ==id).ToList();
13                
14               
15             }
16         }
17         //求平均数
18         public static void avg()
19         {
20             using (Entities ec = new Entities())
21             {
22 
23                 var id = Parameter.IDs[Parameter.index];
24                 ec.TestEnt.Average(ent => ent.Amount);
25                
26             }
27         }
28         //添加
29         public static void add()
30         {
31             using (Entities ec = new Entities())
32             {
33                 TestEnt detail = new TestEnt();
34                 detail.ID = Guid.NewGuid();
35                 detail.IsPercent = true;
36                 detail.Memo = "hsdgsg";
37                 detail.ModificaitonUser = "jake";
38                 detail.ModificationDate = DateTime.Now;
39                 detail.Month = 11;
40                 detail.MonthlyDataID = Guid.NewGuid();
41                 detail.Year = 2012;
42                 detail.Status = 1;
43                 ec.TestEnt.AddObject(detail);
44                 ec.SaveChanges();               
45             }
46         }
47         //修改
48         public static void update()
49         {
50             using (Entities ec = new Entities())
51             {
52 
53                 var id = Parameter.IDs[Parameter.index];
54                var detail=ec.TestEnt.Where(ent => ent.ID == id).ToList().First();
55                detail.IsPercent = true;
56                detail.Memo = "hsdgsg";
57                detail.ModificaitonUser = "jake";
58                detail.ModificationDate = DateTime.Now;
59                detail.Month = 11;
60                detail.Year = 2012;
61                detail.Status = 1;
62                ec.SaveChanges();
63             }
64         }
65         //求最大值
66         public static void max()
67         {
68             using (Entities ec = new Entities())
69             {
70 
71                 var id = Parameter.IDs[Parameter.index];
72                 ec.TestEnt.Max(ent => ent.Amount);
73 
74             }
75         }
76     }

然后是ado.net 的测试代码

View Code
  1  public class SQLNet
  2     {
  3         private static string connStr = "data source=.;initial catalog=testDb;integrated security=True;MultipleActiveResultSets=True;";
  4         public static void avg()
  5         {
  6            
  7             using (SqlConnection conn = new SqlConnection(connStr))
  8             {
  9             
 10                 string sql = @"SELECT 
 11     AVG([Extent1].[Amount]) AS [A1]
 12     FROM [dbo].[testEnt] AS [Extent1]";
 13                 SqlCommand command = new SqlCommand(sql, conn);
 14                 SqlDataAdapter da = new SqlDataAdapter(command);
 15                 DataSet ds = new DataSet();
 16                 if (conn.State != ConnectionState.Open)
 17                 {
 18                     conn.Open();
 19                 }             
 20                 da.Fill(ds);                      
 21                 command.Parameters.Clear();
 22               
 23             }
 24 
 25 
 26         }
 27 
 28         public static Guid getByID()
 29         {
 30                 string sql=string.Format( @"SELECT [ID]
 31       ,[MonthlyDataID]
 32       ,[AccountCode]
 33       ,[AccountName]
 34       ,[Amount]
 35       ,[Year]
 36       ,[Month]
 37       ,[Class]
 38       ,[Appendix]
 39       ,[DealerCode]
 40       ,[DataStatus]
 41       ,[Memo]
 42       ,[IsPercent]
 43       ,[UploadStatus]
 44       ,[Status]
 45       ,[CreationUser]
 46       ,[CreationDate]
 47       ,[ModificaitonUser]
 48       ,[ModificationDate]
 49   FROM [BMW.Interface].[dbo].[testEnt]
 50   where [ID]=@id");
 51                 Guid id = Parameter.IDs[Parameter.index];
 52              using (SqlConnection conn = new SqlConnection(connStr))
 53              {
 54                       SqlCommand command = new SqlCommand(sql, conn);
 55                     
 56                 SqlParameter param = new SqlParameter("id", id);
 57                 command.Parameters.Add(param);
 58                 SqlDataAdapter da = new SqlDataAdapter(command);
 59                 DataSet ds = new DataSet();
 60                 if (conn.State != ConnectionState.Open)
 61                 {
 62                     conn.Open();
 63                 }             
 64                 da.Fill(ds);                      
 65                 command.Parameters.Clear();
 66              }
 67              return id;
 68         }
 69 
 70         public static void update()
 71         {
 72             var id=getByID();
 73             string sql=@"update [dbo].[testEnt]
 74 set [Year] = @0, [Month] = @1, [Memo] = @2, [IsPercent] = @3, [Status] = @4, [ModificaitonUser] = @5, [ModificationDate] = @6
 75 where ([ID] = @7)";
 76             using (SqlConnection conn = new SqlConnection(connStr))
 77             {
 78                 SqlCommand command = new SqlCommand(sql, conn);
 79                 SqlParameter param0 = new SqlParameter("0", 2012);
 80                 SqlParameter param1 = new SqlParameter("1", 11);
 81                 SqlParameter param2 = new SqlParameter("2", "hsdgsg");
 82                 SqlParameter param3 = new SqlParameter("3", 1);
 83                 SqlParameter param4 = new SqlParameter("4", 1);
 84                 SqlParameter param5 = new SqlParameter("5", "jake");
 85                 SqlParameter param6 = new SqlParameter("6", DateTime.Now);
 86                 SqlParameter param7 = new SqlParameter("7", id);
 87                 command.Parameters.Add(param0);
 88                 command.Parameters.Add(param1);
 89                 command.Parameters.Add(param2);
 90                 command.Parameters.Add(param3);
 91                 command.Parameters.Add(param4);
 92                 command.Parameters.Add(param5);
 93                 command.Parameters.Add(param6);
 94                 command.Parameters.Add(param7);
 95             
 96                 if (conn.State != ConnectionState.Open)
 97                 {
 98                     conn.Open();
 99                 }
100                 command.ExecuteNonQuery();
101                 command.Parameters.Clear();
102             }
103 
104         }
105 
106         public static void max()
107         {
108             string sql = @" SELECT  MAX([Amount]) AS [A1]  FROM [dbo].[testEnt]";
109             using (SqlConnection conn = new SqlConnection(connStr))
110             {
111                 SqlCommand command = new SqlCommand(sql, conn);
112 
113                 if (conn.State != ConnectionState.Open)
114                 {
115                     conn.Open();
116                 }
117                 command.ExecuteNonQuery();
118                 command.Parameters.Clear();
119             }
120         }
121 
122         public static void add()
123         {
124             string sql = @"insert [dbo].[testEnt]([ID], [MonthlyDataID], [AccountCode], [AccountName], [Amount], [Year], [Month], [Class], [Appendix], [DealerCode], [DataStatus], [Memo], [IsPercent], [UploadStatus], [Status], [CreationUser], [CreationDate], [ModificaitonUser], [ModificationDate])
125 values (@0, @1, null, null, null, @2, @3, null, null, null, null, @4, @5, null, @6, null, null, @7, @8)";
126             using (SqlConnection conn = new SqlConnection(connStr))
127             {
128                 SqlCommand command = new SqlCommand(sql, conn);
129                 SqlParameter param0 = new SqlParameter("2", 2012);
130                 SqlParameter param1 = new SqlParameter("3", 11);
131                 SqlParameter param2 = new SqlParameter("4", "hsdgsg");
132                 SqlParameter param3 = new SqlParameter("5", 1);
133                 SqlParameter param4 = new SqlParameter("6", 1);
134                 SqlParameter param5 = new SqlParameter("7", "jake");
135                 SqlParameter param6 = new SqlParameter("8", DateTime.Now);
136                 SqlParameter param7 = new SqlParameter("0", Guid.NewGuid());
137                 SqlParameter param8 = new SqlParameter("1", Guid.NewGuid());
138                 command.Parameters.Add(param0);
139                 command.Parameters.Add(param1);
140                 command.Parameters.Add(param2);
141                 command.Parameters.Add(param3);
142                 command.Parameters.Add(param4);
143                 command.Parameters.Add(param5);
144                 command.Parameters.Add(param6);
145                 command.Parameters.Add(param7);
146                 command.Parameters.Add(param8);
147 
148                 if (conn.State != ConnectionState.Open)
149                 {
150                     conn.Open();
151                 }
152                 command.ExecuteNonQuery();
153                 command.Parameters.Clear();
154             }
155 
156         }
157     }

 

 后面我是用我自己做的一个压力测试工具测试出来 的性能数据.

 

请大家参考.

方法[entityFramework_getbyID]: 成功数1000   ,失败数0      ,完成数1000   ,平均时间2.23   毫秒,
方法[entityFramework_getbyID]: 成功数10000  ,失败数0      ,完成数10000  ,平均时间1.89   毫秒,
方法[sqlDB_getByID       ]: 成功数1000   ,失败数0      ,完成数1000   ,平均时间0.32   毫秒,
方法[sqlDB_getByID       ]: 成功数10000  ,失败数0      ,完成数10000  ,平均时间0.35   毫秒,


方法[entityFramework_add ]: 成功数1000   ,失败数0      ,完成数1000   ,平均时间11.93  毫秒,
方法[entityFramework_add ]: 成功数10000  ,失败数0      ,完成数10000  ,平均时间20.90  毫秒,
方法[sqlDB_add           ]: 成功数1000   ,失败数0      ,完成数1000   ,平均时间16.28  毫秒,
方法[sqlDB_add           ]: 成功数10000  ,失败数0      ,完成数10000  ,平均时间18.62  毫秒,

 


方法[sqlDB_upd           ]: 成功数1000   ,失败数0      ,完成数1000   ,平均时间4.12   毫秒,
方法[sqlDB_upd           ]: 成功数10000  ,失败数0      ,完成数10000  ,平均时间135.65 毫秒,
方法[entityFramework_update]: 成功数1000   ,失败数0      ,完成数1000   ,平均时间13.17  毫秒,
方法[entityFramework_update]: 成功数10000  ,失败数0      ,完成数10000  ,平均时间11.46  毫秒,


方法[sqlDB_avg           ]: 成功数851    ,失败数149    ,完成数1000   ,平均时间10663.90毫秒,
方法[sqlDB_avg           ]: 成功数100    ,失败数0      ,完成数100    ,平均时间1792.23毫秒,
方法[entityFramework_avg ]: 成功数823    ,失败数177    ,完成数1000   ,平均时间10316.37毫秒,
方法[entityFramework_avg ]: 成功数100    ,失败数0      ,完成数100    ,平均时间1574.56毫秒,


方法[sqlDB_max           ]: 成功数1000   ,失败数0      ,完成数1000   ,平均时间7713.51毫秒,
方法[sqlDB_max           ]: 成功数100    ,失败数0      ,完成数100    ,平均时间1365.35毫秒,
方法[entityFramework_max ]: 成功数100    ,失败数0      ,完成数100    ,平均时间680.14 毫秒,
方法[entityFramework_max ]: 成功数1000   ,失败数0      ,完成数1000   ,平均时间8180.12毫秒,

从上面的数据,我们可以看出,其实大部分他们之间的时间都差的不是很远.除了根据用户id查询这一项.

因此,我觉的用EF做大项目,是可以考虑的.不知道各位博友,有何高见.

 

 

 

posted @ 2013-04-23 22:24  jake强  阅读(7964)  评论(47编辑  收藏  举报