|
| 1 | +using System.ComponentModel; |
| 2 | +using BenchmarkDotNet.Attributes; |
| 3 | + |
| 4 | +namespace Dapper.Tests.Performance |
| 5 | +{ |
| 6 | + [Description("Dapper cache impact")] |
| 7 | + [MemoryDiagnoser] |
| 8 | + public class DapperCacheImpact : BenchmarkBase |
| 9 | + { |
| 10 | + [GlobalSetup] |
| 11 | + public void Setup() => BaseSetup(); |
| 12 | + |
| 13 | + private object args = new { Id = 42, Name = "abc" }; |
| 14 | + |
| 15 | + public class Foo |
| 16 | + { |
| 17 | + public int Id { get; set; } |
| 18 | + public string Name { get; set; } |
| 19 | + } |
| 20 | + |
| 21 | + // note: custom BDN setup means [Params] is awkward; unroll manually instead |
| 22 | + [Benchmark] |
| 23 | + public void ExecuteNoParameters_Cache() => _connection.Execute(new CommandDefinition("select '42' as Id, 'abc' as Name", flags: CommandFlags.None)); |
| 24 | + [Benchmark] |
| 25 | + public void ExecuteParameters_Cache() => _connection.Execute(new CommandDefinition("select @id as Id, @name as Name", args, flags: CommandFlags.None)); |
| 26 | + [Benchmark] |
| 27 | + public void QueryFirstNoParameters_Cache() => _connection.QueryFirst<Foo>(new CommandDefinition("select '42' as Id, 'abc' as Name", flags: CommandFlags.None)); |
| 28 | + [Benchmark] |
| 29 | + public void QueryFirstParameters_Cache() => _connection.QueryFirst<Foo>(new CommandDefinition("select @id as Id, @name as Name", args, flags: CommandFlags.None)); |
| 30 | + [Benchmark] |
| 31 | + public void ExecuteNoParameters_NoCache() => _connection.Execute(new CommandDefinition("select '42' as Id, 'abc' as Name", flags: CommandFlags.NoCache)); |
| 32 | + [Benchmark] |
| 33 | + public void ExecuteParameters_NoCache() => _connection.Execute(new CommandDefinition("select @id as Id, @name as Name", args, flags: CommandFlags.NoCache)); |
| 34 | + [Benchmark] |
| 35 | + public void QueryFirstNoParameters_NoCache() => _connection.QueryFirst<Foo>(new CommandDefinition("select '42' as Id, 'abc' as Name", flags: CommandFlags.NoCache)); |
| 36 | + [Benchmark] |
| 37 | + public void QueryFirstParameters_NoCache() => _connection.QueryFirst<Foo>(new CommandDefinition("select @id as Id, @name as Name", args, flags: CommandFlags.NoCache)); |
| 38 | + } |
| 39 | +} |
0 commit comments