|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Jobs; |
| 3 | +using Dapper; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using Npgsql; |
| 6 | +using NpgsqlBenchmark.Model; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | + |
| 10 | +namespace NpgsqlBenchmark.Benchmarks |
| 11 | +{ |
| 12 | + [MemoryDiagnoser] |
| 13 | + [SimpleJob(RuntimeMoniker.Net70)] |
| 14 | + [HideColumns("Error", "StdDev", "Median", "RatioSD", "Gen0", "Gen1", "Gen2")] |
| 15 | + public class CompareDapper |
| 16 | + { |
| 17 | + [Params(10, 20, 30)] |
| 18 | + public int Size; |
| 19 | + |
| 20 | + private NpgsqlConnection _connection; |
| 21 | + |
| 22 | + [GlobalSetup] |
| 23 | + public void Setup() |
| 24 | + { |
| 25 | + var root = new ConfigurationBuilder() |
| 26 | + .SetBasePath(Directory.GetCurrentDirectory()) |
| 27 | + .AddJsonFile("settings.json", optional: false) |
| 28 | + .Build() |
| 29 | + ; |
| 30 | + |
| 31 | + _connection = new NpgsqlConnection(root.GetConnectionString("SqlConnection")); |
| 32 | + _connection.Open(); |
| 33 | + } |
| 34 | + |
| 35 | + [GlobalCleanup] |
| 36 | + public void Cleanup() |
| 37 | + { |
| 38 | + _connection?.Dispose(); |
| 39 | + } |
| 40 | + |
| 41 | + [Gedaq.Npgsql.Attributes.Query( |
| 42 | + @" |
| 43 | +SELECT |
| 44 | + p.id, |
| 45 | + p.firstname, |
| 46 | +~StartInner::Identification:id~ |
| 47 | + i.id, |
| 48 | + i.typename, |
| 49 | +~EndInner::Identification~ |
| 50 | + p.middlename, |
| 51 | + p.lastname |
| 52 | +FROM person p |
| 53 | +LEFT JOIN identification i ON i.id = p.identification_id |
| 54 | +WHERE p.id > $1 |
| 55 | +", |
| 56 | + "CompareRead", |
| 57 | + typeof(Person) |
| 58 | + ), |
| 59 | + Gedaq.Npgsql.Attributes.Parametr(parametrType: typeof(int), position: 1) |
| 60 | + ] |
| 61 | + [Benchmark(Description = $"Gedaq.Npgsql")] |
| 62 | + public void Npgsql() |
| 63 | + { |
| 64 | + for (int i = 0; i < Size; i++) |
| 65 | + { |
| 66 | + var persons = ((NpgsqlConnection)_connection).CompareRead(49999).ToList(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + [Benchmark(Baseline = true, Description = "Dapper")] |
| 71 | + public void Dapper() |
| 72 | + { |
| 73 | + for (int i = 0; i < Size; i++) |
| 74 | + { |
| 75 | + var persons = _connection.Query<Person, Identification, Person>(@" |
| 76 | +SELECT |
| 77 | + p.id, |
| 78 | + p.firstname, |
| 79 | + p.middlename, |
| 80 | + p.lastname, |
| 81 | + p.identification_id, |
| 82 | + i.typename |
| 83 | +FROM person p |
| 84 | +LEFT JOIN identification i ON i.id = p.identification_id |
| 85 | +WHERE p.id > @id |
| 86 | +", |
| 87 | +(person, ident) => |
| 88 | +{ |
| 89 | + person.Identification = ident; |
| 90 | + return person; |
| 91 | +}, |
| 92 | +new { id = 49999 }, |
| 93 | +splitOn: "identification_id" |
| 94 | +) |
| 95 | + .ToList(); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments