-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
NarrowRowWriteSyncBenchmark.cs
223 lines (171 loc) · 6.25 KB
/
NarrowRowWriteSyncBenchmark.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
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using BenchmarkDotNet.Attributes;
namespace Cesil.Benchmark
{
[BenchmarkCategory("Write", "ComparesLibraries")]
public class NarrowRowWriteSyncBenchmark
{
[ParamsSource(nameof(KnownTypes))]
public Type Type { get; set; }
[ParamsSource(nameof(KnownRowSet))]
public string RowSet { get; set; }
public IEnumerable<string> KnownRowSet =>
new[]
{
"ShallowRows",
"DeepRows"
};
public IEnumerable<Type> KnownTypes =>
new[]
{
typeof(byte),
typeof(sbyte),
typeof(byte?),
typeof(sbyte?),
typeof(short),
typeof(ushort),
typeof(short?),
typeof(ushort?),
typeof(int),
typeof(uint),
typeof(int?),
typeof(uint?),
typeof(long),
typeof(ulong),
typeof(long?),
typeof(ulong?),
typeof(float),
typeof(float?),
typeof(double),
typeof(double?),
typeof(decimal),
typeof(decimal?),
typeof(string),
typeof(char),
typeof(char?),
typeof(Guid),
typeof(Guid?),
typeof(DateTime),
typeof(DateTime?),
typeof(DateTimeOffset),
typeof(DateTimeOffset?),
typeof(Uri),
typeof(NarrowRowEnum),
typeof(NarrowRowEnum?),
typeof(NarrowRowFlagsEnum),
typeof(NarrowRowFlagsEnum?)
};
private Action<TextWriter> DoCsvHelper;
private Action<TextWriter> DoCesil;
[GlobalSetup]
public void Initialize()
{
DoCsvHelper = MakeRun(Type, RowSet, nameof(CsvHelper));
DoCesil = MakeRun(Type, RowSet, nameof(Cesil));
}
private static Action<TextWriter> MakeRun(Type type, string rowSet, string library)
{
var mtd = MakeRunGenericMtd.MakeGenericMethod(type);
var ret = mtd.Invoke(null, new[] { rowSet, library });
return (Action<TextWriter>)ret;
}
private static MethodInfo MakeRunGenericMtd = typeof(NarrowRowWriteSyncBenchmark).GetMethod(nameof(MakeRunGeneric), BindingFlags.NonPublic | BindingFlags.Static);
private static Action<TextWriter> MakeRunGeneric<T>(string rowSet, string library)
{
const int NUM_SHALLOW = 10;
const int NUM_DEEP = 10_000;
IEnumerable<NarrowRow<T>> rows;
var r = MakeRandom();
switch (rowSet)
{
case "ShallowRows": rows = Enumerable.Range(0, NUM_SHALLOW).Select(_ => NarrowRow<T>.Create(r)).ToArray(); break;
case "DeepRows": rows = Enumerable.Range(0, NUM_DEEP).Select(_ => NarrowRow<T>.Create(r)).ToArray(); break;
default: throw new Exception();
}
Action<IEnumerable<NarrowRow<T>>, TextWriter> writeDel;
switch (library)
{
case nameof(Cesil):
{
var opts = Options.CreateBuilder(Options.Default).WithWriteTrailingRowEnding(WriteTrailingRowEnding.Always).ToOptions();
var config = Configuration.For<NarrowRow<T>>(opts);
writeDel = MakeWriteWithCesilDel(config);
}
break;
case nameof(CsvHelper):
{
var config = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture);
config.RegisterClassMap<NarrowRowMapping<T>>();
writeDel = MakeWriteWithCsvHelper(config);
}
break;
default: throw new Exception();
}
return writer => writeDel(rows, writer);
static Random MakeRandom()
{
return new Random(2020_03_04);
}
static Action<IEnumerable<NarrowRow<T>>, TextWriter> MakeWriteWithCesilDel(IBoundConfiguration<NarrowRow<T>> config)
{
return
(IEnumerable<NarrowRow<T>> rows, TextWriter into) =>
{
using (var writer = config.CreateWriter(into))
{
writer.WriteAll(rows);
}
};
}
static Action<IEnumerable<NarrowRow<T>>, TextWriter> MakeWriteWithCsvHelper(CsvHelper.Configuration.CsvConfiguration config)
{
return
(IEnumerable<NarrowRow<T>> rows, TextWriter into) =>
{
using (var writer = new CsvHelper.CsvWriter(into, config))
{
writer.WriteRecords(rows);
}
};
}
}
public void InitializeAndTest()
{
foreach (var row in KnownRowSet)
{
foreach (var type in KnownTypes)
{
RowSet = row;
Type = type;
Initialize();
var csvHelper = GetText(DoCsvHelper);
var cesil = GetText(DoCesil);
if (csvHelper != cesil) throw new Exception();
}
}
static string GetText(Action<TextWriter> del)
{
using (var str = new StringWriter())
{
del(str);
return str.ToString();
}
}
}
[Benchmark(Baseline = true)]
public void CsvHelper()
{
DoCsvHelper(TextWriter.Null);
}
[Benchmark]
public void Cesil()
{
DoCesil(TextWriter.Null);
}
}
}