Skip to content

Commit 5739931

Browse files
refact: * open connection in iteration setup, not in the bench
1 parent 4573844 commit 5739931

4 files changed

Lines changed: 137 additions & 26 deletions

File tree

Src/NpgsqlBenchmark/Benchmarks/BinaryImportMap.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BenchmarkDotNet.Attributes;
22
using BenchmarkDotNet.Jobs;
3+
using Npgsql;
34
using NpgsqlBenchmark.Model;
45
using System.Linq;
56
using System.Threading.Tasks;
@@ -11,21 +12,50 @@ namespace NpgsqlBenchmark.Benchmarks
1112
[HideColumns("Error", "StdDev", "Median", "RatioSD", "Gen0", "Gen1", "Gen2")]
1213
public class BinaryImportMap : PostgresBenchmark
1314
{
15+
private NpgsqlConnection _connection;
16+
1417
[Params(10, 20, 30, 40)]
1518
public int Size;
1619

1720
[GlobalSetup]
18-
public async Task Setup()
21+
public async Task GlobalSetup()
1922
{
2023
await OneTimeSetUp();
2124
}
2225

2326
[GlobalCleanup]
24-
public async Task Cleanup()
27+
public async Task GlobalCleanup()
2528
{
2629
await OneTimeTearDown();
2730
}
2831

32+
[IterationSetup]
33+
public async Task IterationSetup()
34+
{
35+
_connection = await _npgsqlDataSource.OpenConnectionAsync();
36+
}
37+
38+
[IterationCleanup]
39+
public async Task IterationCleanup()
40+
{
41+
try
42+
{
43+
var connection = _connection;
44+
if (connection != null)
45+
{
46+
await connection.DisposeAsync();
47+
}
48+
}
49+
catch
50+
{
51+
// ignore
52+
}
53+
finally
54+
{
55+
_connection = null;
56+
}
57+
}
58+
2959
[Gedaq.Npgsql.Attributes.Query(
3060
@"
3161
SELECT
@@ -46,10 +76,9 @@ FROM person p
4676
[Benchmark(Description = $"NpgsqlQuery")]
4777
public async Task NpgsqlQuery()
4878
{
49-
await using var connection = await _npgsqlDataSource.OpenConnectionAsync();
5079
for (int i = 0; i < Size; i++)
5180
{
52-
var persons = connection.NpgsqlQuery().ToList();
81+
var persons = _connection.NpgsqlQuery().ToList();
5382
}
5483
}
5584

@@ -76,10 +105,9 @@ FROM person p
76105
[Benchmark(Baseline = true, Description = "NpgsqlBinaryImport")]
77106
public async Task NpgsqlBinaryImport()
78107
{
79-
await using var connection = await _npgsqlDataSource.OpenConnectionAsync();
80108
for (int i = 0; i < Size; i++)
81109
{
82-
var persons = connection.NpgsqlBinaryImport().ToList();
110+
var persons = _connection.NpgsqlBinaryImport().ToList();
83111
}
84112
}
85113
}

Src/NpgsqlBenchmark/Benchmarks/CompareDapper.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BenchmarkDotNet.Attributes;
22
using BenchmarkDotNet.Jobs;
33
using Dapper;
4+
using Npgsql;
45
using NpgsqlBenchmark.Model;
56
using System.Collections.Generic;
67
using System.Data.Common;
@@ -14,21 +15,50 @@ namespace NpgsqlBenchmark.Benchmarks
1415
[HideColumns("Error", "StdDev", "Median", "RatioSD", "Gen0", "Gen1", "Gen2")]
1516
public partial class CompareDapper : PostgresBenchmark
1617
{
18+
private NpgsqlConnection _connection;
19+
1720
[Params(10, 20, 30)]
1821
public int Size;
1922

2023
[GlobalSetup]
21-
public async Task Setup()
24+
public async Task GlobalSetup()
2225
{
2326
await OneTimeSetUp();
2427
}
2528

2629
[GlobalCleanup]
27-
public async Task Cleanup()
30+
public async Task GlobalCleanup()
2831
{
2932
await OneTimeTearDown();
3033
}
3134

35+
[IterationSetup]
36+
public async Task IterationSetup()
37+
{
38+
_connection = await _npgsqlDataSource.OpenConnectionAsync();
39+
}
40+
41+
[IterationCleanup]
42+
public async Task IterationCleanup()
43+
{
44+
try
45+
{
46+
var connection = _connection;
47+
if (connection != null)
48+
{
49+
await connection.DisposeAsync();
50+
}
51+
}
52+
catch
53+
{
54+
// ignore
55+
}
56+
finally
57+
{
58+
_connection = null;
59+
}
60+
}
61+
3262
[Gedaq.Npgsql.Attributes.Query(
3363
@"
3464
SELECT
@@ -52,20 +82,18 @@ WHERE p.id > $1
5282
[Benchmark(Baseline = true, Description = $"Gedaq.Npgsql")]
5383
public async Task Npgsql()
5484
{
55-
await using var connection = await _npgsqlDataSource.OpenConnectionAsync();
5685
for (int i = 0; i < Size; i++)
5786
{
58-
var persons = GetAllPerson(connection, 49999).ToList();
87+
var persons = GetAllPerson(_connection, 49999).ToList();
5988
}
6089
}
6190

6291
[Benchmark(Description = "Dapper")]
6392
public async Task Dapper()
6493
{
65-
await using var connection = await _npgsqlDataSource.OpenConnectionAsync();
6694
for (int i = 0; i < Size; i++)
6795
{
68-
var persons = connection.Query<Person, Identification, Person>(@"
96+
var persons = _connection.Query<Person, Identification, Person>(@"
6997
SELECT
7098
p.id,
7199
p.firstname,
@@ -114,10 +142,9 @@ WHERE p.id > @id
114142
[Benchmark(Description = "DapperAOT")]
115143
public async Task DapperAOT()
116144
{
117-
await using var connection = await _npgsqlDataSource.OpenConnectionAsync();
118145
for (int i = 0; i < Size; i++)
119146
{
120-
var persons = DapperAOTGetAllPerson(connection, 49999).ToList();
147+
var persons = DapperAOTGetAllPerson(_connection, 49999).ToList();
121148
}
122149
}
123150
}

Src/NpgsqlBenchmark/Benchmarks/ComparePrepareDapper.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BenchmarkDotNet.Attributes;
22
using BenchmarkDotNet.Jobs;
33
using Dapper;
4+
using Npgsql;
45
using NpgsqlBenchmark.Model;
56
using System.Linq;
67
using System.Threading.Tasks;
@@ -12,21 +13,50 @@ namespace NpgsqlBenchmark.Benchmarks
1213
[HideColumns("Error", "StdDev", "Median", "RatioSD", "Gen0", "Gen1", "Gen2")]
1314
public partial class ComparePrepareDapper : PostgresBenchmark
1415
{
16+
private NpgsqlConnection _connection;
17+
1518
[Params(100, 200, 300)]
1619
public int Size;
1720

1821
[GlobalSetup]
19-
public async Task Setup()
22+
public async Task GlobalSetup()
2023
{
2124
await OneTimeSetUp();
2225
}
2326

2427
[GlobalCleanup]
25-
public async Task Cleanup()
28+
public async Task GlobalCleanup()
2629
{
2730
await OneTimeTearDown();
2831
}
2932

33+
[IterationSetup]
34+
public async Task IterationSetup()
35+
{
36+
_connection = await _npgsqlDataSource.OpenConnectionAsync();
37+
}
38+
39+
[IterationCleanup]
40+
public async Task IterationCleanup()
41+
{
42+
try
43+
{
44+
var connection = _connection;
45+
if (connection != null)
46+
{
47+
await connection.DisposeAsync();
48+
}
49+
}
50+
catch
51+
{
52+
// ignore
53+
}
54+
finally
55+
{
56+
_connection = null;
57+
}
58+
}
59+
3060
[Gedaq.Npgsql.Attributes.Query(
3161
@"
3262
SELECT
@@ -50,8 +80,7 @@ WHERE p.id < $1
5080
[Benchmark(Description = $"Gedaq.Npgsql")]
5181
public async Task Npgsql()
5282
{
53-
await using var connection = await _npgsqlDataSource.OpenConnectionAsync();
54-
using var getAllCmd = CreateGetAllPersonCommand(connection, prepare: true);
83+
using var getAllCmd = CreateGetAllPersonCommand(_connection, prepare: true);
5584
for (int i = 0; i < Size; i++)
5685
{
5786
SetGetAllPersonParametrs(getAllCmd, i);
@@ -67,12 +96,11 @@ public class Parametr
6796
[Benchmark(Baseline = true, Description = "Dapper")]
6897
public async Task Dapper()
6998
{
70-
await using var connection = await _npgsqlDataSource.OpenConnectionAsync();
7199
var param = new Parametr();
72100
for (int i = 0; i < Size; i++)
73101
{
74102
param.id = i;
75-
var persons = connection.Query<Person, Identification, Person>(@"
103+
var persons = _connection.Query<Person, Identification, Person>(@"
76104
SELECT
77105
p.id,
78106
p.firstname,

Src/NpgsqlBenchmark/Benchmarks/QueryMap.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BenchmarkDotNet.Attributes;
22
using BenchmarkDotNet.Jobs;
3+
using Npgsql;
34
using NpgsqlBenchmark.Model;
45
using System.Data.Common;
56
using System.Linq;
@@ -12,21 +13,50 @@ namespace NpgsqlBenchmark.Benchmarks
1213
[HideColumns("Error", "StdDev", "Median", "RatioSD", "Gen0", "Gen1", "Gen2")]
1314
public class QueryMap : PostgresBenchmark
1415
{
16+
private NpgsqlConnection _connection;
17+
1518
[Params(50, 100, 200)]
1619
public int Size;
1720

1821
[GlobalSetup]
19-
public async Task Setup()
22+
public async Task GlobalSetup()
2023
{
2124
await OneTimeSetUp();
2225
}
2326

2427
[GlobalCleanup]
25-
public async Task Cleanup()
28+
public async Task GlobalCleanup()
2629
{
2730
await OneTimeTearDown();
2831
}
2932

33+
[IterationSetup]
34+
public async Task IterationSetup()
35+
{
36+
_connection = await _npgsqlDataSource.OpenConnectionAsync();
37+
}
38+
39+
[IterationCleanup]
40+
public async Task IterationCleanup()
41+
{
42+
try
43+
{
44+
var connection = _connection;
45+
if (connection != null)
46+
{
47+
await connection.DisposeAsync();
48+
}
49+
}
50+
catch
51+
{
52+
// ignore
53+
}
54+
finally
55+
{
56+
_connection = null;
57+
}
58+
}
59+
3060
[Gedaq.Npgsql.Attributes.Query(
3161
@"
3262
SELECT
@@ -49,10 +79,9 @@ FROM person p
4979
[Benchmark(Description = $"Gedaq.Npgsql")]
5080
public async Task Npgsql()
5181
{
52-
await using var connection = await _npgsqlDataSource.OpenConnectionAsync();
5382
for (int i = 0; i < Size; i++)
5483
{
55-
var persons = connection.ReadInnerMap(50000).ToList();
84+
var persons = _connection.ReadInnerMap(50000).ToList();
5685
}
5786
}
5887

@@ -79,10 +108,9 @@ FROM person p
79108
[Benchmark(Baseline = true, Description = "Gedaq.DbConnection")]
80109
public async Task DbConnection()
81110
{
82-
await using var connection = await _npgsqlDataSource.OpenConnectionAsync();
83111
for (int i = 0; i < Size; i++)
84112
{
85-
var persons = ((DbConnection)connection).ReadInnerMap(50000).ToList();
113+
var persons = ((DbConnection)_connection).ReadInnerMap(50000).ToList();
86114
}
87115
}
88116
}

0 commit comments

Comments
 (0)