This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReadingBenchmarks.cs
246 lines (208 loc) · 7.71 KB
/
ReadingBenchmarks.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Buffers;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipelines;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Nerdbank.Streams;
using TestFixtures;
using static System.IO.File;
namespace Pipelines.File.Unofficial.Benchmarks
{
[MemoryDiagnoser]
[RPlotExporter]
[CsvExporter()]
public class ReadingBenchmarks
{
//Asynchronous = 1073741824, // 0x40000000
//SequentialScan = 134217728, // 0x08000000
private const int AsyncRead = 0x48000000;
public ReadingBenchmarks()
{
}
[Params(
//1024,
2048,
4096,
//8192,
16384,
//32768,
65536, // NT Async read calls are only actually async after this buffer size
//131072,
262144
//524288
)]
public int BufferSize { get; set; }
[Params("Async|Sequential", "Sequential")]
public string FileOptionsString { get; set; }
// this is some bug with bencmarkdotnet's templater that requires this hack or else we get a CS1513
public FileOptions FileOptions => FileOptionsString == "Sequential"
? FileOptions.SequentialScan
: FileOptions.SequentialScan | FileOptions.Asynchronous;
// public field
[ParamsSource(nameof(FilesToTest))]
public FriendlyPath File { get; set; }
// public property
public IEnumerable<FriendlyPath> FilesToTest => new[] { Fixtures.IncrementingInt64_4, Fixtures.IncrementingInt64_128}.Select(x => new FriendlyPath(x));
[Benchmark(Baseline = true)]
public ulong FileStreamSync()
{
ulong last = 0;
using (var fileStream = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.None, BufferSize, FileOptions))
{
var buffer = new byte[BufferSize];
int bytesRead;
do
{
bytesRead = fileStream.Read(buffer, 0, BufferSize);
ParseLastNumber(buffer.AsSpan(), ref last);
} while (bytesRead != 0);
return last;
}
}
//[Benchmark()]
public ulong FileStreamSyncMemoryPool()
{
ulong last = 0;
using (var fileStream = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.None, BufferSize, FileOptions))
{
var memoryOwner = MemoryPool<byte>.Shared.Rent(BufferSize);
var buffer = memoryOwner.Memory.Span;
int bytesRead;
do
{
bytesRead = fileStream.Read(buffer);
ParseLastNumber(buffer, ref last);
} while (bytesRead != 0);
return last;
}
}
[Benchmark]
public async Task<ulong> FileStreamAsync()
{
ulong last = 0;
var bufferSize = BufferSize == 1 ? 4096 : BufferSize;
using (var fileStream = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize, FileOptions))
{
var buffer = new byte[bufferSize];
int bytesRead;
do
{
bytesRead = await fileStream.ReadAsync(buffer, 0, bufferSize);
ParseLastNumber(buffer.AsSpan(), ref last);
} while (bytesRead != 0);
return last;
}
}
//[Benchmark]
public async Task<ulong> FileStreamAsyncMemoryPool()
{
ulong last = 0;
using (var fileStream = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.None, BufferSize, FileOptions))
{
var memoryOwner = MemoryPool<byte>.Shared.Rent(BufferSize);
var buffer = memoryOwner.Memory;
int bytesRead;
do
{
bytesRead = await fileStream.ReadAsync(buffer);
ParseLastNumber(buffer.Span, ref last);
} while (bytesRead != 0);
return last;
}
}
[Benchmark()]
public async Task<ulong> PipelineNative()
{
ulong last = 0;
var filePipeline = FileReader.ReadFile(File, BufferSize);
ReadResult result = default;
while(!result.IsCompleted)
{
result = await filePipeline.ReadAsync();
ParseLastNumber(result.Buffer, ref last);
filePipeline.AdvanceTo(result.Buffer.End);
}
return last;
}
[Benchmark()]
public async Task<ulong> PipelineAdapter()
{
ulong last = 0;
using (var reader = FileStreamReader.ReadFile(File, BufferSize, FileOptions))
{
var filePipeline = reader.Reader;
ReadResult result = default;
while (!result.IsCompleted)
{
result = await filePipeline.ReadAsync();
ParseLastNumber(result.Buffer, ref last);
filePipeline.AdvanceTo(result.Buffer.End);
}
return last;
}
}
[Benchmark()]
public async Task<ulong> PipelineAdapter2()
{
ulong last = 0;
using (var reader = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.None, BufferSize, FileOptions))
{
var filePipeline = Sockets.Unofficial.StreamConnection.GetReader(reader, new PipeOptions(minimumSegmentSize: BufferSize));
ReadResult result = default;
while (!result.IsCompleted)
{
result = await filePipeline.ReadAsync();
ParseLastNumber(result.Buffer, ref last);
filePipeline.AdvanceTo(result.Buffer.End);
}
return last;
}
}
[Benchmark()]
public async Task<ulong> PipelineAdapter3()
{
ulong last = 0;
using (var reader = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.None, BufferSize, FileOptions))
{
var filePipeline = reader.UsePipeReader(sizeHint: BufferSize);
ReadResult result = default;
while (!result.IsCompleted)
{
result = await filePipeline.ReadAsync();
ParseLastNumber(result.Buffer, ref last);
filePipeline.AdvanceTo(result.Buffer.End);
}
return last;
}
}
private void ParseLastNumber(ReadOnlySpan<byte> packet, ref ulong value)
{
if (packet.Length < 8)
{
return;
}
// assumes packets are 8 byte aligned...
// dodgy but good enough for tests
var last8Bytes = packet.Slice(packet.Length - 8, 8);
value = BinaryPrimitives
.ReadUInt64LittleEndian(last8Bytes);
}
private void ParseLastNumber(ReadOnlySequence<byte> packet, ref ulong value)
{
if (packet.Length < 8)
{
return;
}
// assumes packets are 8 byte aligned...
// dodgy but good enough for tests
var last8Bytes = packet.Slice(packet.Length - 8, 8);
value = BinaryPrimitives
.ReadUInt64LittleEndian(last8Bytes.First.Span);
}
}
}