11using System ;
2+ using System . Collections . Generic ;
23using System . Data . Common ;
34using System . Threading . Tasks ;
45using BenchmarkDotNet . Attributes ;
910using BenchmarkDotNet . Exporters ;
1011using BenchmarkDotNet . Jobs ;
1112using BenchmarkDotNet . Running ;
12- using BenchmarkDotNet . Toolchains . CsProj ;
1313using BenchmarkDotNet . Validators ;
14- using MySql . Data . MySqlClient ;
1514
1615namespace Benchmark
1716{
@@ -21,14 +20,12 @@ static void Main()
2120 {
2221 var customConfig = ManualConfig
2322 . 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 ) ;
3229
3330 var summary = BenchmarkRunner . Run < MySqlClient > ( customConfig ) ;
3431 Console . WriteLine ( summary ) ;
@@ -37,10 +34,13 @@ static void Main()
3734
3835 public class MySqlClient
3936 {
37+ [ Params ( "MySql.Data" , "MySqlConnector" ) ]
38+ public string Library { get ; set ; }
39+
4040 [ GlobalSetup ]
4141 public void GlobalSetup ( )
4242 {
43- using ( var connection = new MySqlConnection ( s_connectionString ) )
43+ using ( var connection = new MySqlConnector . MySqlConnection ( s_connectionString ) )
4444 {
4545 connection . Open ( ) ;
4646 using ( var cmd = connection . CreateCommand ( ) )
@@ -69,16 +69,23 @@ create table benchmark.blobs(
6969
7070 s_connectionString += ";database=benchmark" ;
7171
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 ) ;
7479 }
7580
7681 [ GlobalCleanup ]
7782 public void GlobalCleanup ( )
7883 {
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 ( ) ;
8289 }
8390
8491 private static void AddBlobParameter ( DbCommand command , string name , int size )
@@ -97,35 +104,31 @@ private static void AddBlobParameter(DbCommand command, string name, int size)
97104 [ Benchmark ]
98105 public async Task OpenFromPoolAsync ( )
99106 {
100- m_connection . Close ( ) ;
101- await m_connection . OpenAsync ( ) ;
107+ Connection . Close ( ) ;
108+ await Connection . OpenAsync ( ) ;
102109 }
103110
104111 [ Benchmark ]
105112 public void OpenFromPoolSync ( )
106113 {
107- m_connection . Close ( ) ;
108- m_connection . Open ( ) ;
114+ Connection . Close ( ) ;
115+ Connection . Open ( ) ;
109116 }
110117
111118 [ Benchmark ]
112119 public async Task ExecuteScalarAsync ( )
113120 {
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 ( ) ;
119124 }
120125
121126 [ Benchmark ]
122127 public void ExecuteScalarSync ( )
123128 {
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 ( ) ;
129132 }
130133
131134 private const string c_executeScalarSql = "select max(value) from integers;" ;
@@ -143,48 +146,46 @@ public void ExecuteScalarSync()
143146 private async Task < int > ReadAllRowsAsync ( string sql )
144147 {
145148 int total = 0 ;
146- using ( var cmd = m_connection . CreateCommand ( ) )
149+ using ( var cmd = Connection . CreateCommand ( ) )
147150 {
148151 cmd . CommandText = sql ;
149- using ( var reader = await cmd . ExecuteReaderAsync ( ) )
152+ using var reader = await cmd . ExecuteReaderAsync ( ) ;
153+ do
150154 {
151- do
155+ while ( await reader . ReadAsync ( ) )
152156 {
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 ( ) ) ;
160161 }
161162 return total ;
162163 }
163164
164165 private int ReadAllRowsSync ( string sql )
165166 {
166167 int total = 0 ;
167- using ( var cmd = m_connection . CreateCommand ( ) )
168+ using ( var cmd = Connection . CreateCommand ( ) )
168169 {
169170 cmd . CommandText = sql ;
170- using ( var reader = cmd . ExecuteReader ( ) )
171+ using var reader = cmd . ExecuteReader ( ) ;
172+ do
171173 {
172- do
174+ while ( reader . Read ( ) )
173175 {
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 ( ) ) ;
181180 }
182181 return total ;
183182 }
184183
184+ private DbConnection Connection => m_connections [ Library ] ;
185+
185186 // 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;" ;
187188
188- MySqlConnection m_connection ;
189+ Dictionary < string , DbConnection > m_connections = new Dictionary < string , DbConnection > ( ) ;
189190 }
190191}
0 commit comments