Beispiel #1
0
        private static void AddTests_Dapper(Tests tests)
        {
            tests.Add(id =>
            {
                using (var connection = Program.GetClosedConnection())
                {
                    global::Dapper.SqlMapper.Query <Post>(connection, "select * from Posts where Id = @Id", new { Id = id }).First();
                }
            },
                      "Dapper (Buffered, Sync)");

            tests.Add(id =>
            {
                using (var connection = Program.GetOpenConnection())
                {
                    global::Dapper.SqlMapper.Query <Post>(connection, "select * from Posts where Id = @Id", new { Id = id }).First();
                }
            },
                      "Dapper (Non-buffered, Sync)");

            tests.Add(id =>
            {
                using (var connection = Program.GetClosedConnection())
                {
                    global::Dapper.SqlMapper.Query <dynamic>(connection, "select * from Posts where Id = @Id", new { Id = id }).First();
                }
            },
                      "Dapper (Dynamic, Buffered, Sync)");

            tests.Add((Action <int>)(id =>
            {
                using (var connection = Program.GetOpenConnection())
                {
                    global::Dapper.SqlMapper.Query <dynamic>(connection, "select * from Posts where Id = @Id", new { Id = id }).First();
                }
            }),
                      "Dapper (Dynamic, Non-buffered, Sync)");

            //tests.Add(async id => (await global::Dapper.SqlMapper.QueryAsync<Post>(mapperConnectionClosed3, "select * from Posts where Id = @Id", new { Id = id })).First(),
            //	() => mapperConnectionClosed3.Dispose(),
            //	"Dapper (Buffered, Async)");

            tests.Add(async(int id) =>
            {
                using (var connection = Program.GetOpenConnection())
                {
                    (await global::Dapper.SqlMapper.QueryAsync <Post>(connection, "select * from Posts where Id = @Id", new { Id = id })).First();
                }
            },
                      "Dapper (Non-buffered, Async)");

            //tests.Add((Func<int,Task>)(async id => (await global::Dapper.SqlMapper.QueryAsync<dynamic>(mapperConnectionClosed4, "select * from Posts where Id = @Id", new { Id = id })).First()),
            //	() => mapperConnectionClosed4.Dispose(),
            //	"Dapper (Dynamic, Buffered, Async)");

            tests.Add(async id =>
            {
                using (var connection = Program.GetOpenConnection())
                {
                    (await global::Dapper.SqlMapper.QueryAsync <dynamic>(connection, "select * from Posts where Id = @Id", new { Id = id })).First();
                }
            },
                      "Dapper (Dynamic, Non-buffered, Async)");
        }
Beispiel #2
0
        private static void AddTests_HandCoded(Tests tests, int concurrency)
        {
            Func <SqlConnection, int, SqlCommand> createCommand = (connection, id) =>
            {
                var command = new SqlCommand();
                command.Connection  = connection;
                command.CommandText = @"select Id, [Text], [CreationDate], LastChangeDate, 
                Counter1,Counter2,Counter3,Counter4,Counter5,Counter6,Counter7,Counter8,Counter9 from Posts where Id = @Id";
                var idParam = command.Parameters.Add("@Id", System.Data.SqlDbType.Int);
                idParam.Value = id;
                return(command);
            };

            //var connections1 = Enumerable.Range(0, concurrency).Select(x => Program.GetOpenConnection()).ToArray();
            tests.Add(id =>
            {
                using (var connection = Program.GetOpenConnection())
                    using (var postCommand = createCommand(connection, id))
                        using (var reader = postCommand.ExecuteReader())
                        {
                            reader.Read();
                            var post            = new Post();
                            post.Id             = reader.GetInt32(0);
                            post.Text           = reader.GetNullableString(1);
                            post.CreationDate   = reader.GetDateTime(2);
                            post.LastChangeDate = reader.GetDateTime(3);

                            post.Counter1 = reader.GetNullableValue <int>(4);
                            post.Counter2 = reader.GetNullableValue <int>(5);
                            post.Counter3 = reader.GetNullableValue <int>(6);
                            post.Counter4 = reader.GetNullableValue <int>(7);
                            post.Counter5 = reader.GetNullableValue <int>(8);
                            post.Counter6 = reader.GetNullableValue <int>(9);
                            post.Counter7 = reader.GetNullableValue <int>(10);
                            post.Counter8 = reader.GetNullableValue <int>(11);
                            post.Counter9 = reader.GetNullableValue <int>(12);
                        }
            },
                      //()=>connections1.Cast<IDisposable>().ToList().ForEach(x=>x.Dispose()),
                      "Hand Coded (DataReader, Sync)");

            //var connections2 = Enumerable.Range(0, concurrency).Select(x => Program.GetOpenConnection()).ToArray();
            tests.Add(async id =>
            {
                using (var connection = Program.GetOpenConnection())
                    using (var postCommand = createCommand(connection, id))
                        using (var reader = await postCommand.ExecuteReaderAsync())
                        {
                            await reader.ReadAsync();
                            var post            = new Post();
                            post.Id             = await reader.GetFieldValueAsync <int>(0);
                            post.Text           = reader.GetNullableString(1);
                            post.CreationDate   = await reader.GetFieldValueAsync <DateTime>(2);
                            post.LastChangeDate = await reader.GetFieldValueAsync <DateTime>(3);

                            post.Counter1 = await reader.GetNullableValueAsync <int>(4);
                            post.Counter2 = await reader.GetNullableValueAsync <int>(5);
                            post.Counter3 = await reader.GetNullableValueAsync <int>(6);
                            post.Counter4 = await reader.GetNullableValueAsync <int>(7);
                            post.Counter5 = await reader.GetNullableValueAsync <int>(8);
                            post.Counter6 = await reader.GetNullableValueAsync <int>(9);
                            post.Counter7 = await reader.GetNullableValueAsync <int>(10);
                            post.Counter8 = await reader.GetNullableValueAsync <int>(11);
                            post.Counter9 = await reader.GetNullableValueAsync <int>(12);
                        }
            },
                      //()=>connections2.Cast<IDisposable>().ToList().ForEach(x=>x.Dispose()),
                      "Hand Coded (DataReader, Async)");

            DataTable table = new DataTable
            {
                Columns =
                {
                    { "Id",             typeof(int)      },
                    { "Text",           typeof(string)   },
                    { "CreationDate",   typeof(DateTime) },
                    { "LastChangeDate", typeof(DateTime) },
                    { "Counter1",       typeof(int)      },
                    { "Counter2",       typeof(int)      },
                    { "Counter3",       typeof(int)      },
                    { "Counter4",       typeof(int)      },
                    { "Counter5",       typeof(int)      },
                    { "Counter6",       typeof(int)      },
                    { "Counter7",       typeof(int)      },
                    { "Counter8",       typeof(int)      },
                    { "Counter9",       typeof(int)      },
                }
            };

            ////var connections3 = Enumerable.Range(0, concurrency).Select(x => Program.GetOpenConnection()).ToArray();
            //tests.Add(id =>
            //	{
            //		using (var connection = Program.GetOpenConnection())
            //		using (var postCommand = createCommand(connection, id))
            //		{
            //			object[] values = new object[13];
            //			using (var reader = postCommand.ExecuteReader())
            //			{
            //				reader.Read();
            //				reader.GetValues(values);
            //				table.Rows.Add(values);
            //			}
            //		}
            //	},
            //	//()=>connections3.Cast<IDisposable>().ToList().ForEach(x=>x.Dispose()),
            //	"Hand Coded (DataTable via IDataReader.GetValues, Sync)");

            ////var connections4 = Enumerable.Range(0, concurrency).Select(x => Program.GetOpenConnection()).ToArray();
            //tests.Add(async id =>
            //{
            //	using (var connection = Program.GetOpenConnection())
            //	using (var postCommand = createCommand(connection, id))
            //	{
            //		object[] values = new object[13];
            //		using (var reader = await postCommand.ExecuteReaderAsync())
            //		{
            //			await reader.ReadAsync();
            //			reader.GetValues(values);
            //			table.Rows.Add(values);
            //		}
            //	}
            //},
            //	//()=>connections4.Cast<IDisposable>().ToList().ForEach(x=>x.Dispose()),
            //"Hand Coded (DataTable via IDataReader.GetValues, Async)");
        }