Skip to content

Commit 8266d44

Browse files
feat: add compare dapper bench
1 parent 026bc99 commit 8266d44

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}

Src/NpgsqlBenchmark/NpgsqlBenchmark.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
8+
<PackageReference Include="Dapper" Version="2.0.123" />
89
<PackageReference Include="Gedaq" Version="1.4.6.7" />
910
<PackageReference Include="Gedaq.DbConnection" Version="1.2.3" />
1011
<PackageReference Include="Gedaq.Npgsql" Version="1.2.3" />

Src/NpgsqlBenchmark/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ internal class Program
1212
static void Main(string[] args)
1313
{
1414
//FillTestDatabase();
15-
BenchmarkRunner.Run<QueryMap>();
16-
BenchmarkRunner.Run<BinaryImportMap>();
15+
BenchmarkRunner.Run<CompareDapper>();
16+
//BenchmarkRunner.Run<QueryMap>();
17+
//BenchmarkRunner.Run<BinaryImportMap>();
1718
}
1819

1920
private static void FillTestDatabase()
@@ -141,7 +142,7 @@ FROM STDIN (FORMAT BINARY)
141142
var millions = 0;
142143
var millionsCounter = 0;
143144

144-
for (int i = 0; i < 1_000_000; i++)
145+
for (int i = 0; i < 100_000; i++)
145146
{
146147
writer.StartRow();
147148
writer.Write(i, NpgsqlTypes.NpgsqlDbType.Integer);

0 commit comments

Comments
 (0)