PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/PerformanceTests.cs

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