Skip to content

Commit 9ceb5cd

Browse files
Feat/62 (#9)
* feat: + Remove dbTypes attribute parametrs. DbType added with DbTypeMapAttribute. * feat: + Update benchmark
1 parent 43ab55b commit 9ceb5cd

12 files changed

Lines changed: 427 additions & 192 deletions

Gedaq.Npgsql.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 18
4-
VisualStudioVersion = 18.1.11312.151 d18.0
4+
VisualStudioVersion = 18.1.11312.151
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gedaq.Npgsql", "Src\Gedaq.Npgsql\Gedaq.Npgsql.csproj", "{9C9B875A-9C0A-49CB-A7C8-F1D5BD8CA615}"
77
EndProject

Src/Gedaq.Npgsql/Attributes/BinaryExportAttribute.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Gedaq.Common.Enums;
22
using Gedaq.Npgsql.Enums;
3-
using NpgsqlTypes;
43
using System;
54

65
namespace Gedaq.Npgsql.Attributes
@@ -19,7 +18,6 @@ public sealed class BinaryExportAttribute : Attribute
1918
/// <param name="methodName">Name of the generated method</param>
2019
/// <param name="queryMapTypes">The types into which the Row received from the database will be converted</param>
2120
/// <param name="query">Sql query</param>
22-
/// <param name="dbTypes">The database types, in the order they appear in the query, that will be read in that order from the Row.</param>
2321
/// <param name="methodType">Type of generated method see <see cref="MethodType"/></param>
2422
/// <param name="sourceType">The type of database connection source for which the method will be generated see <see cref="SourceType"/></param>
2523
/// <param name="accessModifier">Access Modifier of Generated Methods see <see cref="AccessModifier"/></param>
@@ -29,7 +27,6 @@ public BinaryExportAttribute(
2927
string methodName,
3028
Type[] queryMapTypes,
3129
string? query = null,
32-
NpgsqlDbType[]? dbTypes = null,
3330
MethodType methodType = MethodType.Sync,
3431
SourceType sourceType = SourceType.Connection,
3532
AccessModifier accessModifier = AccessModifier.AsContainingClass,

Src/Gedaq.Npgsql/Gedaq.Npgsql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</ItemGroup>
1111
<PropertyGroup>
1212
<PackageId>Gedaq.Npgsql</PackageId>
13-
<PackageVersion>10.0.0.2</PackageVersion>
13+
<PackageVersion>10.0.0.3</PackageVersion>
1414
<Authors>Brevnov Vyacheslav Sergeevich</Authors>
1515
<RepositoryUrl>https://github.com/SoftStoneDevelop/Gedaq.Npgsql</RepositoryUrl>
1616
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>

Src/NpgsqlBenchmark/Benchmarks/BinaryExportMap.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void IterationCleanup()
5353
}
5454

5555
[Gedaq.Npgsql.Attributes.Query(
56-
@"
56+
query: @"
5757
SELECT
5858
p.id,
5959
p.firstname,
@@ -66,19 +66,19 @@ public void IterationCleanup()
6666
FROM person p
6767
LEFT JOIN identification i ON i.id = p.identification_id
6868
",
69-
"NpgsqlQuery",
70-
typeof(Person))]
69+
methodName: "NpgsqlQuery",
70+
queryMapTypes: [typeof(Person)])]
7171
[Benchmark(Description = $"NpgsqlQuery")]
7272
public async Task NpgsqlQuery()
7373
{
7474
for (int i = 0; i < Size; i++)
7575
{
76-
var persons = _connection.NpgsqlQuery().ToList();
76+
var persons = _connection.NpgsqlQuery();
7777
}
7878
}
7979

8080
[Gedaq.Npgsql.Attributes.BinaryExport(
81-
@"
81+
query: @"
8282
COPY
8383
(
8484
SELECT
@@ -94,8 +94,8 @@ FROM person p
9494
LEFT JOIN identification i ON i.id = p.identification_id
9595
) TO STDOUT (FORMAT BINARY)
9696
",
97-
"NpgsqlBinaryExport",
98-
typeof(Person))]
97+
methodName: "NpgsqlBinaryExport",
98+
queryMapTypes: [typeof(Person)])]
9999
[Benchmark(Baseline = true, Description = "NpgsqlBinaryExport")]
100100
public async Task NpgsqlBinaryExport()
101101
{
Lines changed: 162 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
using BenchmarkDotNet.Attributes;
22
using BenchmarkDotNet.Jobs;
33
using Dapper;
4+
using Gedaq.Common.Enums;
45
using Npgsql;
56
using NpgsqlBenchmark.Model;
67
using System.Collections.Generic;
78
using System.Data.Common;
8-
using System.Linq;
99
using System.Threading.Tasks;
1010

1111
namespace NpgsqlBenchmark.Benchmarks
1212
{
13+
public class PersonFlat
14+
{
15+
public int Id { get; set; }
16+
17+
public string FirstName { get; set; }
18+
19+
public string MiddleName { get; set; }
20+
21+
public string LastName { get; set; }
22+
23+
[Gedaq.Common.Attributes.IgnoreProperty()]
24+
public Identification Identification { get; set; }
25+
}
26+
1327
[MemoryDiagnoser]
1428
[SimpleJob(RuntimeMoniker.Net10_0)]
15-
[HideColumns("Error", "StdDev", "Median", "RatioSD", "Gen0", "Gen1", "Gen2")]
1629
public partial class CompareDapper : PostgresBenchmark
1730
{
1831
private NpgsqlConnection _connection;
1932

20-
[Params(0)]
21-
public int Size;
33+
[Params(10, 20, 30)]
34+
public int Iterations;
2235

2336
[GlobalSetup]
2437
public async Task GlobalSetup()
@@ -55,35 +68,37 @@ public void IterationCleanup()
5568
}
5669
}
5770

58-
[Gedaq.Npgsql.Attributes.Query(
59-
@"
71+
[Benchmark(Baseline = true, Description = "Dapper")]
72+
public void Dapper()
73+
{
74+
for (int i = 0; i < Iterations; i++)
75+
{
76+
var persons = _connection.Query<Person, Identification, Person>(@"
6077
SELECT
6178
p.id,
6279
p.firstname,
63-
~StartInner::Identification:id~
64-
i.id,
65-
i.typename,
66-
~EndInner::Identification~
6780
p.middlename,
68-
p.lastname
81+
p.lastname,
82+
p.identification_id,
83+
i.typename
6984
FROM person p
7085
LEFT JOIN identification i ON i.id = p.identification_id
71-
WHERE p.id >= $1
7286
",
73-
"GetAllPerson",
74-
typeof(Person)),
75-
Gedaq.Npgsql.Attributes.Parametr(parametrType: typeof(int), position: 1)
76-
]
77-
[Benchmark(Baseline = true, Description = $"Gedaq.Npgsql", OperationsPerInvoke = 1_000)]
78-
public void Npgsql()
79-
{
80-
var persons = GetAllPerson(_connection, 50_000).ToList();
87+
(person, ident) =>
88+
{
89+
person.Identification = ident;
90+
return person;
91+
},
92+
splitOn: "identification_id").AsList();
93+
}
8194
}
8295

83-
[Benchmark(Description = "Dapper", OperationsPerInvoke = 1_000)]
84-
public void Dapper()
96+
[Benchmark(Description = "Dapper Async")]
97+
public async Task DapperAsync()
8598
{
86-
var persons = _connection.Query<Person, Identification, Person>(@"
99+
for (int i = 0; i < Iterations; i++)
100+
{
101+
var persons = (await _connection.QueryAsync<Person, Identification, Person>(@"
87102
SELECT
88103
p.id,
89104
p.firstname,
@@ -93,21 +108,18 @@ public void Dapper()
93108
i.typename
94109
FROM person p
95110
LEFT JOIN identification i ON i.id = p.identification_id
96-
WHERE p.id >= @id
97111
",
98112
(person, ident) =>
99113
{
100114
person.Identification = ident;
101115
return person;
102116
},
103-
new { id = 50_000 },
104-
splitOn: "identification_id"
105-
)
106-
.ToList();
117+
splitOn: "identification_id")).AsList();
118+
}
107119
}
108120

109121
[DapperAot]
110-
public static IEnumerable<Person> DapperAOTGetAllPerson(DbConnection connection, int id) => connection.Query<Person, Identification, Person>(
122+
public static IEnumerable<Person> DapperAOTGetAllPerson(DbConnection connection) => connection.Query<Person, Identification, Person>(
111123
@"SELECT
112124
p.id,
113125
p.firstname,
@@ -117,21 +129,136 @@ public static IEnumerable<Person> DapperAOTGetAllPerson(DbConnection connection,
117129
i.typename
118130
FROM person p
119131
LEFT JOIN identification i ON i.id = p.identification_id
120-
WHERE p.id >= @id
121132
",
122133
(person, ident) =>
123134
{
124135
person.Identification = ident;
125136
return person;
126137
},
127-
new { id },
128-
splitOn: "identification_id"
129-
);
138+
splitOn: "identification_id");
130139

131-
[Benchmark(Description = "DapperAOT", OperationsPerInvoke = 1_000)]
140+
[Benchmark(Description = "DapperAOT")]
132141
public void DapperAOT()
133142
{
134-
var persons = DapperAOTGetAllPerson(_connection, 50_000).ToList();
143+
for (int i = 0; i < Iterations; i++)
144+
{
145+
var persons = DapperAOTGetAllPerson(_connection).AsList();
146+
}
147+
}
148+
149+
[DapperAot]
150+
public static Task<IEnumerable<Person>> DapperAOTGetAllPersonAsync(DbConnection connection) => connection.QueryAsync<Person, Identification, Person>(
151+
@"SELECT
152+
p.id,
153+
p.firstname,
154+
p.middlename,
155+
p.lastname,
156+
p.identification_id,
157+
i.typename
158+
FROM person p
159+
LEFT JOIN identification i ON i.id = p.identification_id
160+
",
161+
(person, ident) =>
162+
{
163+
person.Identification = ident;
164+
return person;
165+
},
166+
splitOn: "identification_id");
167+
168+
[Benchmark(Description = "DapperAOT Async")]
169+
public async Task DapperAOTAsync()
170+
{
171+
for (int i = 0; i < Iterations; i++)
172+
{
173+
var persons = (await DapperAOTGetAllPersonAsync(_connection)).AsList();
174+
}
175+
}
176+
177+
[Gedaq.Npgsql.Attributes.Query(
178+
query: @"
179+
SELECT
180+
p.id,
181+
p.firstname,
182+
~StartInner::Identification:id~
183+
i.id,
184+
i.typename,
185+
~EndInner::Identification~
186+
p.middlename,
187+
p.lastname
188+
FROM person p
189+
LEFT JOIN identification i ON i.id = p.identification_id
190+
",
191+
methodName: "GetAllPerson",
192+
queryMapTypes: [typeof(Person)],
193+
methodType: MethodType.Sync | MethodType.Async,
194+
asyncResultType: AsyncResult.ValueTask)]
195+
[Benchmark(Description = $"Gedaq Static Sync")]
196+
public void GedaqStatic()
197+
{
198+
for (int i = 0; i < Iterations; i++)
199+
{
200+
var persons = GetAllPerson(_connection);
201+
}
202+
}
203+
204+
[Benchmark(Description = $"Gedaq Static Async")]
205+
public async Task GedaqStaticAsync()
206+
{
207+
for (int i = 0; i < Iterations; i++)
208+
{
209+
var persons = await GetAllPersonAsync(_connection);
210+
}
211+
}
212+
213+
[Gedaq.Npgsql.Attributes.Query(
214+
query: null,
215+
methodName: "GetAllPersonDyn",
216+
queryMapTypes: [typeof(PersonFlat), typeof(Identification)],
217+
overrideAliasPrefixs: ["person_", "identity_"],
218+
methodType: MethodType.Sync | MethodType.Async,
219+
asyncResultType: AsyncResult.ValueTask)]
220+
[Benchmark(Description = $"Gedaq Dynamic Sync")]
221+
public void GedaqDynamic()
222+
{
223+
for (int i = 0; i < Iterations; i++)
224+
{
225+
var persons = new List<PersonFlat>();
226+
227+
GetAllPersonDyn(_connection, @"
228+
SELECT
229+
p.id as person_id,
230+
p.firstname as person_firstname,
231+
p.middlename as person_middlename,
232+
p.lastname as person_lastname,
233+
i.id as identity_id,
234+
i.typename as identity_typename
235+
FROM person p
236+
LEFT JOIN identification i ON i.id = p.identification_id
237+
",
238+
(person, indetity) => { person.Identification = indetity; persons.Add(person); });
239+
}
240+
}
241+
242+
[Benchmark(Description = $"Gedaq Dynamic Async")]
243+
public async Task GedaqDynamicAsync()
244+
{
245+
for (int i = 0; i < Iterations; i++)
246+
{
247+
var persons = new List<PersonFlat>();
248+
249+
await GetAllPersonDynAsync(_connection, @"
250+
SELECT
251+
p.id as person_id,
252+
p.firstname as person_firstname,
253+
p.middlename as person_middlename,
254+
p.lastname as person_lastname,
255+
i.id as identity_id,
256+
i.typename as identity_typename
257+
FROM person p
258+
LEFT JOIN identification i ON i.id = p.identification_id
259+
",
260+
(person, indetity) => { person.Identification = indetity; persons.Add(person); });
261+
}
135262
}
136263
}
137264
}

0 commit comments

Comments
 (0)