PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/PerformanceTests.cs

https://github.com/sureshotday/dapper-dot-net
C# | 291 lines | 227 code | 47 blank | 17 comment | 11 complexity | cc400079b05cf9d7f9e4849578fd158d MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Linq;
  5. using System.Data.SqlClient;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using BLToolkit.Data;
  9. using Dapper;
  10. using Massive;
  11. using NHibernate.Criterion;
  12. using NHibernate.Linq;
  13. using ServiceStack.OrmLite;
  14. using ServiceStack.OrmLite.SqlServer;
  15. using SqlMapper.Linq2Sql;
  16. using SqlMapper.NHibernate;
  17. using Dapper.Contrib.Extensions;
  18. using SqlMapper.EntityFramework;
  19. namespace SqlMapper
  20. {
  21. class PerformanceTests
  22. {
  23. class Test
  24. {
  25. public static Test Create(Action<int> iteration, string name)
  26. {
  27. return new Test {Iteration = iteration, Name = name };
  28. }
  29. public Action<int> Iteration { get; set; }
  30. public string Name { get; set; }
  31. public Stopwatch Watch { get; set; }
  32. }
  33. class Tests : List<Test>
  34. {
  35. public void Add(Action<int> iteration, string name)
  36. {
  37. Add(Test.Create(iteration, name));
  38. }
  39. public void Run(int iterations)
  40. {
  41. // warmup
  42. foreach (var test in this)
  43. {
  44. test.Iteration(iterations + 1);
  45. test.Watch = new Stopwatch();
  46. test.Watch.Reset();
  47. }
  48. var rand = new Random();
  49. for (int i = 1; i <= iterations; i++)
  50. {
  51. foreach (var test in this.OrderBy(ignore => rand.Next()))
  52. {
  53. test.Watch.Start();
  54. test.Iteration(i);
  55. test.Watch.Stop();
  56. }
  57. }
  58. foreach (var test in this.OrderBy(t => t.Watch.ElapsedMilliseconds))
  59. {
  60. Console.WriteLine(test.Name + " took " + test.Watch.ElapsedMilliseconds + "ms");
  61. }
  62. }
  63. }
  64. static DataClassesDataContext GetL2SContext(SqlConnection connection)
  65. {
  66. return new DataClassesDataContext(connection);
  67. }
  68. internal class SomaConfig : Soma.Core.MsSqlConfig
  69. {
  70. public override string ConnectionString
  71. {
  72. get { return Program.ConnectionString; }
  73. }
  74. public override void Log(Soma.Core.PreparedStatement preparedStatement)
  75. {
  76. // no op
  77. }
  78. }
  79. public void Run(int iterations)
  80. {
  81. using (var connection = Program.GetOpenConnection())
  82. {
  83. var tests = new Tests();
  84. var l2scontext1 = GetL2SContext(connection);
  85. tests.Add(id => l2scontext1.Posts.First(p => p.Id == id), "Linq 2 SQL");
  86. var l2scontext2 = GetL2SContext(connection);
  87. var compiledGetPost = CompiledQuery.Compile((Linq2Sql.DataClassesDataContext ctx, int id) => ctx.Posts.First(p => p.Id == id));
  88. tests.Add(id => compiledGetPost(l2scontext2, id), "Linq 2 SQL Compiled");
  89. var l2scontext3 = GetL2SContext(connection);
  90. tests.Add(id => l2scontext3.ExecuteQuery<Post>("select * from Posts where Id = {0}", id).First(), "Linq 2 SQL ExecuteQuery");
  91. var entityContext = new EFContext(connection);
  92. tests.Add(id => entityContext.Posts.First(p => p.Id == id), "Entity framework");
  93. var entityContext2 = new EFContext(connection);
  94. tests.Add(id => entityContext2.Database.SqlQuery<Post>("select * from Posts where Id = {0}", id).First(), "Entity framework SqlQuery");
  95. //var entityContext3 = new EFContext(connection);
  96. //tests.Add(id => entityFrameworkCompiled(entityContext3, id), "Entity framework CompiledQuery");
  97. //var entityContext4 = new EFContext(connection);
  98. //tests.Add(id => entityContext4.Posts.Where("it.Id = @id", new System.Data.Objects.ObjectParameter("id", id)).First(), "Entity framework ESQL");
  99. var entityContext5 = new EFContext(connection);
  100. tests.Add(id => entityContext5.Posts.AsNoTracking().First(p => p.Id == id), "Entity framework No Tracking");
  101. var mapperConnection = Program.GetOpenConnection();
  102. tests.Add(id => mapperConnection.Query<Post>("select * from Posts where Id = @Id", new { Id = id }, buffered: true).First(), "Mapper Query (buffered)");
  103. tests.Add(id => mapperConnection.Query<Post>("select * from Posts where Id = @Id", new { Id = id }, buffered: false).First(), "Mapper Query (non-buffered)");
  104. var mapperConnection2 = Program.GetOpenConnection();
  105. tests.Add(id => mapperConnection2.Query("select * from Posts where Id = @Id", new { Id = id }, buffered: true).First(), "Dynamic Mapper Query (buffered)");
  106. tests.Add(id => mapperConnection2.Query("select * from Posts where Id = @Id", new { Id = id }, buffered: false).First(), "Dynamic Mapper Query (non-buffered)");
  107. // dapper.contrib
  108. var mapperConnection3 = Program.GetOpenConnection();
  109. tests.Add(id => mapperConnection2.Get<Post>(id), "Dapper.Cotrib");
  110. var massiveModel = new DynamicModel(Program.ConnectionString);
  111. var massiveConnection = Program.GetOpenConnection();
  112. tests.Add(id => massiveModel.Query("select * from Posts where Id = @0", massiveConnection, id).First(), "Dynamic Massive ORM Query");
  113. // PetaPoco test with all default options
  114. var petapoco = new PetaPoco.Database(Program.ConnectionString, "System.Data.SqlClient");
  115. petapoco.OpenSharedConnection();
  116. tests.Add(id => petapoco.Fetch<Post>("SELECT * from Posts where Id=@0", id), "PetaPoco (Normal)");
  117. // PetaPoco with some "smart" functionality disabled
  118. var petapocoFast = new PetaPoco.Database(Program.ConnectionString, "System.Data.SqlClient");
  119. petapocoFast.OpenSharedConnection();
  120. petapocoFast.EnableAutoSelect = false;
  121. petapocoFast.EnableNamedParams = false;
  122. petapocoFast.ForceDateTimesToUtc = false;
  123. tests.Add(id => petapocoFast.Fetch<Post>("SELECT * from Posts where Id=@0", id), "PetaPoco (Fast)");
  124. // Subsonic ActiveRecord
  125. tests.Add(id => SubSonic.Post.SingleOrDefault(x => x.Id == id), "SubSonic ActiveRecord.SingleOrDefault");
  126. // Subsonic coding horror
  127. SubSonic.tempdbDB db = new SubSonic.tempdbDB();
  128. tests.Add(id => new SubSonic.Query.CodingHorror(db.Provider, "select * from Posts where Id = @0", id).ExecuteTypedList<Post>(), "SubSonic Coding Horror");
  129. // NHibernate
  130. var nhSession1 = NHibernateHelper.OpenSession();
  131. tests.Add(id => nhSession1.CreateSQLQuery(@"select * from Posts where Id = :id")
  132. .SetInt32("id", id)
  133. .List(), "NHibernate SQL");
  134. var nhSession2 = NHibernateHelper.OpenSession();
  135. tests.Add(id => nhSession2.CreateQuery(@"from Post as p where p.Id = :id")
  136. .SetInt32("id", id)
  137. .List(), "NHibernate HQL");
  138. var nhSession3 = NHibernateHelper.OpenSession();
  139. tests.Add(id => nhSession3.CreateCriteria<Post>()
  140. .Add(Restrictions.IdEq(id))
  141. .List(), "NHibernate Criteria");
  142. var nhSession4 = NHibernateHelper.OpenSession();
  143. tests.Add(id => nhSession4
  144. .Query<Post>()
  145. .Where(p => p.Id == id).First(), "NHibernate LINQ");
  146. var nhSession5 = NHibernateHelper.OpenSession();
  147. tests.Add(id => nhSession5.Get<Post>(id), "NHibernate Session.Get");
  148. // bltoolkit
  149. var db1 = new DbManager(Program.GetOpenConnection());
  150. tests.Add(id => db1.SetCommand("select * from Posts where Id = @id", db1.Parameter("id", id)).ExecuteList<Post>(), "BLToolkit");
  151. // Simple.Data
  152. var sdb = Simple.Data.Database.OpenConnection(Program.ConnectionString);
  153. tests.Add(id => sdb.Posts.FindById(id), "Simple.Data");
  154. // Soma
  155. var somadb = new Soma.Core.Db(new SomaConfig());
  156. tests.Add(id => somadb.Find<Post>(id), "Soma");
  157. //ServiceStack's OrmLite:
  158. OrmLiteConfig.DialectProvider = SqlServerOrmLiteDialectProvider.Instance; //Using SQL Server
  159. IDbCommand ormLiteCmd = Program.GetOpenConnection().CreateCommand();
  160. tests.Add(id => ormLiteCmd.QueryById<Post>(id), "OrmLite QueryById");
  161. // HAND CODED
  162. var postCommand = new SqlCommand();
  163. postCommand.Connection = connection;
  164. postCommand.CommandText = @"select Id, [Text], [CreationDate], LastChangeDate,
  165. Counter1,Counter2,Counter3,Counter4,Counter5,Counter6,Counter7,Counter8,Counter9 from Posts where Id = @Id";
  166. var idParam = postCommand.Parameters.Add("@Id", System.Data.SqlDbType.Int);
  167. tests.Add(id =>
  168. {
  169. idParam.Value = id;
  170. using (var reader = postCommand.ExecuteReader())
  171. {
  172. reader.Read();
  173. var post = new Post();
  174. post.Id = reader.GetInt32(0);
  175. post.Text = reader.GetNullableString(1);
  176. post.CreationDate = reader.GetDateTime(2);
  177. post.LastChangeDate = reader.GetDateTime(3);
  178. post.Counter1 = reader.GetNullableValue<int>(4);
  179. post.Counter2 = reader.GetNullableValue<int>(5);
  180. post.Counter3 = reader.GetNullableValue<int>(6);
  181. post.Counter4 = reader.GetNullableValue<int>(7);
  182. post.Counter5 = reader.GetNullableValue<int>(8);
  183. post.Counter6 = reader.GetNullableValue<int>(9);
  184. post.Counter7 = reader.GetNullableValue<int>(10);
  185. post.Counter8 = reader.GetNullableValue<int>(11);
  186. post.Counter9 = reader.GetNullableValue<int>(12);
  187. }
  188. }, "hand coded");
  189. DataTable table = new DataTable
  190. {
  191. Columns =
  192. {
  193. {"Id", typeof (int)},
  194. {"Text", typeof (string)},
  195. {"CreationDate", typeof (DateTime)},
  196. {"LastChangeDate", typeof (DateTime)},
  197. {"Counter1", typeof (int)},
  198. {"Counter2", typeof (int)},
  199. {"Counter3", typeof (int)},
  200. {"Counter4", typeof (int)},
  201. {"Counter5", typeof (int)},
  202. {"Counter6", typeof (int)},
  203. {"Counter7", typeof (int)},
  204. {"Counter8", typeof (int)},
  205. {"Counter9", typeof (int)},
  206. }
  207. };
  208. tests.Add(id =>
  209. {
  210. idParam.Value = id;
  211. object[] values = new object[13];
  212. using (var reader = postCommand.ExecuteReader())
  213. {
  214. reader.Read();
  215. reader.GetValues(values);
  216. table.Rows.Add(values);
  217. }
  218. }, "DataTable via IDataReader.GetValues");
  219. tests.Run(iterations);
  220. }
  221. }
  222. }
  223. static class SqlDataReaderHelper
  224. {
  225. public static string GetNullableString(this SqlDataReader reader, int index)
  226. {
  227. object tmp = reader.GetValue(index);
  228. if (tmp != DBNull.Value)
  229. {
  230. return (string)tmp;
  231. }
  232. return null;
  233. }
  234. public static Nullable<T> GetNullableValue<T>(this SqlDataReader reader, int index) where T : struct
  235. {
  236. object tmp = reader.GetValue(index);
  237. if (tmp != DBNull.Value)
  238. {
  239. return (T)tmp;
  240. }
  241. return null;
  242. }
  243. }
  244. }