-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
216 lines (187 loc) · 6.13 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Exporters.Json;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Validators;
using MySqlConnector;
using MySql.Data.MySqlClient;
namespace Benchmark;
class Program
{
static void Main()
{
var customConfig = ManualConfig
.Create(DefaultConfig.Instance)
.AddValidator(JitOptimizationsValidator.FailOnError)
.AddDiagnoser(MemoryDiagnoser.Default)
.AddColumn(StatisticColumn.AllStatistics)
.AddJob(Job.Default.WithRuntime(CoreRuntime.Core70))
.AddExporter(JsonExporter.Brief);
var summary = BenchmarkRunner.Run<MySqlClient>(customConfig);
Console.WriteLine(summary);
}
}
public class MySqlClient
{
[Params("MySql.Data", "MySqlConnector")]
public string Library { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
var mySqlData = new MySql.Data.MySqlClient.MySqlConnection(s_connectionString);
mySqlData.Open();
m_connections.Add("MySql.Data", mySqlData);
var mySqlConnector = new MySqlConnector.MySqlConnection(s_connectionString);
mySqlConnector.Open();
m_connections.Add("MySqlConnector", mySqlConnector);
Connection = m_connections[Library];
}
[GlobalCleanup]
public void GlobalCleanup()
{
foreach (var connection in m_connections.Values)
connection.Dispose();
m_connections.Clear();
MySqlConnector.MySqlConnection.ClearAllPools();
MySql.Data.MySqlClient.MySqlConnection.ClearAllPools();
}
private static String do1000Param;
static MySqlClient()
{
StringBuilder sb = new StringBuilder("do ?");
for (int i = 1; i < 1000; i++) {
sb.Append(",?");
}
do1000Param = sb.ToString();
}
[Benchmark]
public async Task<int> ExecuteDo1()
{
using var cmd = Connection.CreateCommand();
cmd.CommandText = "do 1";
return await cmd.ExecuteNonQueryAsync();
}
[Benchmark]
public async Task<int> Select1()
{
using var cmd = Connection.CreateCommand();
cmd.CommandText = "Select 1";
return (int) await cmd.ExecuteScalarAsync();
}
[Benchmark]
public async Task<int[]> Select100ColText()
{
using var cmd = Connection.CreateCommand();
cmd.CommandText = "select * FROM test100";
using var reader = await cmd.ExecuteReaderAsync();
int[] val = new int[100];
await reader.ReadAsync();
for (int i = 0; i < 100; i++) {
val[i] = reader.GetInt32(i);
}
return val;
}
[Benchmark]
public async Task<int[]> Select100ColBinary()
{
using var cmd = Connection.CreateCommand();
cmd.CommandText = "select * FROM test100";
cmd.Prepare();
using var reader = await cmd.ExecuteReaderAsync();
int[] val = new int[100];
await reader.ReadAsync();
for (int i = 0; i < 100; i++) {
val[i] = reader.GetInt32(i);
}
return val;
}
[Benchmark]
public async Task<int> Select1000rowsText()
{
using var cmd = Connection.CreateCommand();
cmd.CommandText = "select * from 1000rows";
using var reader = await cmd.ExecuteReaderAsync();
int val = 0;
while (await reader.ReadAsync())
{
val = reader.GetInt32(0);
reader.GetString(1);
}
return val;
}
[Benchmark]
public async Task<int> Select1000rowsBinary()
{
using var cmd = Connection.CreateCommand();
cmd.CommandText = "select * from 1000rows";
cmd.Prepare();
using var reader = await cmd.ExecuteReaderAsync();
int val = 0;
while (await reader.ReadAsync())
{
val = reader.GetInt32(0);
reader.GetString(1);
}
return val;
}
[Benchmark]
public async Task<int> ExecuteDo1000PrepareParamMySql()
{
using var cmd = Connection.CreateCommand();
cmd.CommandText = do1000Param;
cmd.Prepare();
for (int i = 1; i <= 1000; i++) {
var param = new MySql.Data.MySqlClient.MySqlParameter();
param.Value = i;
cmd.Parameters.Add(param);
}
return await cmd.ExecuteNonQueryAsync();
}
[Benchmark]
public async Task<int> ExecuteDo1000ParamMySql()
{
using var cmd = Connection.CreateCommand();
cmd.CommandText = do1000Param;
for (int i = 1; i <= 1000; i++) {
var param = new MySql.Data.MySqlClient.MySqlParameter();
param.Value = i;
cmd.Parameters.Add(param);
}
return await cmd.ExecuteNonQueryAsync();
}
[Benchmark]
public async Task<int> ExecuteDo1000ParamComm()
{
using var cmd = Connection.CreateCommand();
cmd.CommandText = do1000Param;
for (int i = 1; i <= 1000; i++) {
cmd.Parameters.Add(new MySqlConnector.MySqlParameter() { Value = 100 });
}
return await cmd.ExecuteNonQueryAsync();
}
[Benchmark]
public async Task<int> ExecuteDo1000PrepareParamCommPrepare()
{
using var cmd = Connection.CreateCommand();
cmd.CommandText = do1000Param;
cmd.Prepare();
for (int i = 1; i <= 1000; i++) {
cmd.Parameters.Add(new MySqlConnector.MySqlParameter() { Value = 100 });
}
return await cmd.ExecuteNonQueryAsync();
}
private DbConnection Connection { get; set; }
// TODO: move to config file
static string s_connectionString = "server=127.0.0.1;user id=root;port=3306;ssl mode=none;Use Affected Rows=true;Connection Reset=false;Default Command Timeout=0;AutoEnlist=false;database=bench;";
Dictionary<string, DbConnection> m_connections = new Dictionary<string, DbConnection>();
}