1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Data . Common ;
3
4
using System . Threading . Tasks ;
4
5
using BenchmarkDotNet . Attributes ;
9
10
using BenchmarkDotNet . Exporters ;
10
11
using BenchmarkDotNet . Jobs ;
11
12
using BenchmarkDotNet . Running ;
12
- using BenchmarkDotNet . Toolchains . CsProj ;
13
13
using BenchmarkDotNet . Validators ;
14
- using MySql . Data . MySqlClient ;
15
14
16
15
namespace Benchmark
17
16
{
@@ -21,14 +20,12 @@ static void Main()
21
20
{
22
21
var customConfig = ManualConfig
23
22
. Create ( DefaultConfig . Instance )
24
- . With ( JitOptimizationsValidator . FailOnError )
25
- . With ( MemoryDiagnoser . Default )
26
- . With ( StatisticColumn . AllStatistics )
27
- . With ( Job . Default . With ( Runtime . Clr ) . With ( Jit . RyuJit ) . With ( Platform . X64 ) . With ( CsProjClassicNetToolchain . Net472 ) . WithNuGet ( "MySqlConnector" , "0.56.0" ) . WithId ( "net472 0.56.0" ) )
28
- . With ( Job . Default . With ( Runtime . Clr ) . With ( Jit . RyuJit ) . With ( Platform . X64 ) . With ( CsProjClassicNetToolchain . Net472 ) . WithNuGet ( "MySqlConnector" , "0.57.0-beta2" ) . WithId ( "net472 0.57.0" ) )
29
- . With ( Job . Default . With ( Runtime . Core ) . With ( CsProjCoreToolchain . NetCoreApp21 ) . WithNuGet ( "MySqlConnector" , "0.56.0" ) . WithId ( "netcore21 0.56.0" ) )
30
- . With ( Job . Default . With ( Runtime . Core ) . With ( CsProjCoreToolchain . NetCoreApp21 ) . WithNuGet ( "MySqlConnector" , "0.57.0-beta2" ) . WithId ( "netcore21 0.57.0" ) )
31
- . With ( DefaultExporters . Csv ) ;
23
+ . AddValidator ( JitOptimizationsValidator . FailOnError )
24
+ . AddDiagnoser ( MemoryDiagnoser . Default )
25
+ . AddColumn ( StatisticColumn . AllStatistics )
26
+ . AddJob ( Job . Default . WithRuntime ( ClrRuntime . Net48 ) )
27
+ . AddJob ( Job . Default . WithRuntime ( CoreRuntime . Core31 ) )
28
+ . AddExporter ( DefaultExporters . Csv ) ;
32
29
33
30
var summary = BenchmarkRunner . Run < MySqlClient > ( customConfig ) ;
34
31
Console . WriteLine ( summary ) ;
@@ -37,10 +34,13 @@ static void Main()
37
34
38
35
public class MySqlClient
39
36
{
37
+ [ Params ( "MySql.Data" , "MySqlConnector" ) ]
38
+ public string Library { get ; set ; }
39
+
40
40
[ GlobalSetup ]
41
41
public void GlobalSetup ( )
42
42
{
43
- using ( var connection = new MySqlConnection ( s_connectionString ) )
43
+ using ( var connection = new MySqlConnector . MySqlConnection ( s_connectionString ) )
44
44
{
45
45
connection . Open ( ) ;
46
46
using ( var cmd = connection . CreateCommand ( ) )
@@ -69,16 +69,23 @@ create table benchmark.blobs(
69
69
70
70
s_connectionString += ";database=benchmark" ;
71
71
72
- m_connection = new MySqlConnection ( s_connectionString ) ;
73
- m_connection . Open ( ) ;
72
+ var mySqlData = new MySql . Data . MySqlClient . MySqlConnection ( s_connectionString ) ;
73
+ mySqlData . Open ( ) ;
74
+ m_connections . Add ( "MySql.Data" , mySqlData ) ;
75
+
76
+ var mySqlConnector = new MySqlConnector . MySqlConnection ( s_connectionString ) ;
77
+ mySqlConnector . Open ( ) ;
78
+ m_connections . Add ( "MySqlConnector" , mySqlConnector ) ;
74
79
}
75
80
76
81
[ GlobalCleanup ]
77
82
public void GlobalCleanup ( )
78
83
{
79
- m_connection . Dispose ( ) ;
80
- m_connection = null ;
81
- MySqlConnection . ClearAllPools ( ) ;
84
+ foreach ( var connection in m_connections . Values )
85
+ connection . Dispose ( ) ;
86
+ m_connections . Clear ( ) ;
87
+ MySqlConnector . MySqlConnection . ClearAllPools ( ) ;
88
+ MySql . Data . MySqlClient . MySqlConnection . ClearAllPools ( ) ;
82
89
}
83
90
84
91
private static void AddBlobParameter ( DbCommand command , string name , int size )
@@ -97,35 +104,31 @@ private static void AddBlobParameter(DbCommand command, string name, int size)
97
104
[ Benchmark ]
98
105
public async Task OpenFromPoolAsync ( )
99
106
{
100
- m_connection . Close ( ) ;
101
- await m_connection . OpenAsync ( ) ;
107
+ Connection . Close ( ) ;
108
+ await Connection . OpenAsync ( ) ;
102
109
}
103
110
104
111
[ Benchmark ]
105
112
public void OpenFromPoolSync ( )
106
113
{
107
- m_connection . Close ( ) ;
108
- m_connection . Open ( ) ;
114
+ Connection . Close ( ) ;
115
+ Connection . Open ( ) ;
109
116
}
110
117
111
118
[ Benchmark ]
112
119
public async Task ExecuteScalarAsync ( )
113
120
{
114
- using ( var cmd = m_connection . CreateCommand ( ) )
115
- {
116
- cmd . CommandText = c_executeScalarSql ;
117
- await cmd . ExecuteScalarAsync ( ) ;
118
- }
121
+ using var cmd = Connection . CreateCommand ( ) ;
122
+ cmd . CommandText = c_executeScalarSql ;
123
+ await cmd . ExecuteScalarAsync ( ) ;
119
124
}
120
125
121
126
[ Benchmark ]
122
127
public void ExecuteScalarSync ( )
123
128
{
124
- using ( var cmd = m_connection . CreateCommand ( ) )
125
- {
126
- cmd . CommandText = c_executeScalarSql ;
127
- cmd . ExecuteScalar ( ) ;
128
- }
129
+ using var cmd = Connection . CreateCommand ( ) ;
130
+ cmd . CommandText = c_executeScalarSql ;
131
+ cmd . ExecuteScalar ( ) ;
129
132
}
130
133
131
134
private const string c_executeScalarSql = "select max(value) from integers;" ;
@@ -143,48 +146,46 @@ public void ExecuteScalarSync()
143
146
private async Task < int > ReadAllRowsAsync ( string sql )
144
147
{
145
148
int total = 0 ;
146
- using ( var cmd = m_connection . CreateCommand ( ) )
149
+ using ( var cmd = Connection . CreateCommand ( ) )
147
150
{
148
151
cmd . CommandText = sql ;
149
- using ( var reader = await cmd . ExecuteReaderAsync ( ) )
152
+ using var reader = await cmd . ExecuteReaderAsync ( ) ;
153
+ do
150
154
{
151
- do
155
+ while ( await reader . ReadAsync ( ) )
152
156
{
153
- while ( await reader . ReadAsync ( ) )
154
- {
155
- if ( reader . FieldCount > 1 )
156
- total += reader . GetInt32 ( 1 ) ;
157
- }
158
- } while ( await reader . NextResultAsync ( ) ) ;
159
- }
157
+ if ( reader . FieldCount > 1 )
158
+ total += reader . GetInt32 ( 1 ) ;
159
+ }
160
+ } while ( await reader . NextResultAsync ( ) ) ;
160
161
}
161
162
return total ;
162
163
}
163
164
164
165
private int ReadAllRowsSync ( string sql )
165
166
{
166
167
int total = 0 ;
167
- using ( var cmd = m_connection . CreateCommand ( ) )
168
+ using ( var cmd = Connection . CreateCommand ( ) )
168
169
{
169
170
cmd . CommandText = sql ;
170
- using ( var reader = cmd . ExecuteReader ( ) )
171
+ using var reader = cmd . ExecuteReader ( ) ;
172
+ do
171
173
{
172
- do
174
+ while ( reader . Read ( ) )
173
175
{
174
- while ( reader . Read ( ) )
175
- {
176
- if ( reader . FieldCount > 1 )
177
- total += reader . GetInt32 ( 1 ) ;
178
- }
179
- } while ( reader . NextResult ( ) ) ;
180
- }
176
+ if ( reader . FieldCount > 1 )
177
+ total += reader . GetInt32 ( 1 ) ;
178
+ }
179
+ } while ( reader . NextResult ( ) ) ;
181
180
}
182
181
return total ;
183
182
}
184
183
184
+ private DbConnection Connection => m_connections [ Library ] ;
185
+
185
186
// TODO: move to config file
186
- static string s_connectionString = "server=127.0.0.1;user id=mysqltest ;password=test ;port=3306;ssl mode=none;Use Affected Rows=true;Connection Reset=false;Default Command Timeout=0;AutoEnlist=false;" ;
187
+ static string s_connectionString = "server=127.0.0.1;user id=root ;password=pass ;port=3306;ssl mode=none;Use Affected Rows=true;Connection Reset=false;Default Command Timeout=0;AutoEnlist=false;" ;
187
188
188
- MySqlConnection m_connection ;
189
+ Dictionary < string , DbConnection > m_connections = new Dictionary < string , DbConnection > ( ) ;
189
190
}
190
191
}
0 commit comments