-
Notifications
You must be signed in to change notification settings - Fork 272
/
Perf.File.cs
276 lines (240 loc) · 10.4 KB
/
Perf.File.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
namespace System.IO.Tests
{
[BenchmarkCategory(Categories.Libraries)]
public class Perf_File
{
private const int OneKibibyte = 1 << 10; // 1024
private const int HalfKibibyte = OneKibibyte >> 1;
private const int FourKibibytes = OneKibibyte << 2; // default Stream buffer size
private const int SixteenKibibytes = FourKibibytes << 2; // default Stream buffer size * 4
private const int OneMibibyte = OneKibibyte << 10;
private const int HundredMibibytes = OneMibibyte * 100;
private const int DeleteteInnerIterations = 10;
private string _testFilePath;
private string[] _filesToRemove;
private Dictionary<int, byte[]> _userBuffers;
private Dictionary<int, string> _filesToRead;
private string[] _linesToAppend;
private Dictionary<int, string> _textToAppend;
[GlobalSetup(Target = nameof(Exists))]
public void SetupExists()
{
_testFilePath = FileUtils.GetTestFilePath();
File.Create(_testFilePath).Dispose();
}
[Benchmark]
public void Exists() => File.Exists(_testFilePath);
[GlobalCleanup]
public void Cleanup()
{
if (File.Exists(_testFilePath))
{
File.Delete(_testFilePath);
}
}
[IterationSetup(Target = nameof(Delete))]
public void SetupDeleteIteration()
{
var testFile = FileUtils.GetTestFilePath();
_filesToRemove = Enumerable.Range(1, DeleteteInnerIterations).Select(index => testFile + index).ToArray();
foreach (var file in _filesToRemove)
File.Create(file).Dispose();
}
[Benchmark(OperationsPerInvoke = DeleteteInnerIterations)]
public void Delete()
{
var filesToRemove = _filesToRemove;
foreach (var file in filesToRemove)
File.Delete(file);
}
[GlobalSetup(Targets = new[] { nameof(WriteAllBytes), "WriteAllBytesAsync" })]
public void SetupWriteAllBytes()
{
_testFilePath = FileUtils.GetTestFilePath();
_userBuffers = new Dictionary<int, byte[]>()
{
{ HalfKibibyte, ValuesGenerator.Array<byte>(HalfKibibyte) },
{ FourKibibytes, ValuesGenerator.Array<byte>(FourKibibytes) },
{ SixteenKibibytes, ValuesGenerator.Array<byte>(SixteenKibibytes) },
{ OneMibibyte, ValuesGenerator.Array<byte>(OneMibibyte) },
{ HundredMibibytes, ValuesGenerator.Array<byte>(HundredMibibytes) },
};
}
[Benchmark]
[Arguments(HalfKibibyte)]
[Arguments(FourKibibytes)]
[Arguments(SixteenKibibytes)]
[Arguments(OneMibibyte)]
[Arguments(HundredMibibytes)]
public void WriteAllBytes(int size) => File.WriteAllBytes(_testFilePath, _userBuffers[size]);
[GlobalSetup(Targets = new[] { nameof(ReadAllBytes), "ReadAllBytesAsync", nameof(CopyTo), nameof(CopyToOverwrite) })]
public void SetupReadAllBytes()
{
// use non-temp file path to ensure that we don't test some unusal File System on Unix
string baseDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments, Environment.SpecialFolderOption.Create);
File.WriteAllBytes(_testFilePath = Path.Combine(baseDir, Path.GetRandomFileName()), Array.Empty<byte>());
_filesToRead = new Dictionary<int, string>()
{
{ HalfKibibyte, WriteBytes(HalfKibibyte) },
{ FourKibibytes, WriteBytes(FourKibibytes) },
{ SixteenKibibytes, WriteBytes(SixteenKibibytes) },
{ OneMibibyte, WriteBytes(OneMibibyte) },
{ HundredMibibytes, WriteBytes(HundredMibibytes) },
};
string WriteBytes(int fileSize)
{
string filePath = Path.Combine(baseDir, Path.GetRandomFileName());
File.WriteAllBytes(filePath, ValuesGenerator.Array<byte>(fileSize));
return filePath;
}
}
[GlobalCleanup(Targets = new[] { nameof(ReadAllBytes), "ReadAllBytesAsync", nameof(CopyTo), nameof(CopyToOverwrite) })]
public void CleanupReadAllBytes()
{
foreach (string filePath in _filesToRead.Values)
File.Delete(filePath);
File.Delete(_testFilePath);
}
[Benchmark]
[Arguments(HalfKibibyte)]
[Arguments(FourKibibytes)]
[Arguments(SixteenKibibytes)]
[Arguments(OneMibibyte)]
[Arguments(HundredMibibytes)]
[MemoryRandomization]
public byte[] ReadAllBytes(int size) => File.ReadAllBytes(_filesToRead[size]);
#if !NETFRAMEWORK
[BenchmarkCategory(Categories.NoWASM)]
[Benchmark]
[Arguments(HalfKibibyte)]
[Arguments(FourKibibytes)]
[Arguments(SixteenKibibytes)]
[Arguments(OneMibibyte)]
[Arguments(HundredMibibytes)]
public Task WriteAllBytesAsync(int size) => File.WriteAllBytesAsync(_testFilePath, _userBuffers[size]);
[BenchmarkCategory(Categories.NoWASM)]
[Benchmark]
[Arguments(HalfKibibyte)]
[Arguments(FourKibibytes)]
[Arguments(SixteenKibibytes)]
[Arguments(OneMibibyte)]
[Arguments(HundredMibibytes)]
[MemoryRandomization]
public Task<byte[]> ReadAllBytesAsync(int size) => File.ReadAllBytesAsync(_filesToRead[size]);
#endif
[GlobalSetup(Targets = new[] { nameof(ReadAllLines), "ReadAllLinesAsync" })]
public void SetupReadAllLines()
=> File.WriteAllLines(_testFilePath = FileUtils.GetTestFilePath(), ValuesGenerator.ArrayOfStrings(count: 100, minLength: 20, maxLength: 80));
[Benchmark]
public string[] ReadAllLines() => File.ReadAllLines(_testFilePath);
#if !NETFRAMEWORK
[BenchmarkCategory(Categories.NoWASM)]
[Benchmark]
public Task<string[]> ReadAllLinesAsync() => File.ReadAllLinesAsync(_testFilePath);
#endif
[GlobalSetup(Targets = new[] { nameof(AppendAllLines), "AppendAllLinesAsync" })]
public void SetupAppendAllLines()
{
_testFilePath = FileUtils.GetTestFilePath();
_linesToAppend = ValuesGenerator.ArrayOfStrings(count: 20, minLength: 20, maxLength: 80);
}
[Benchmark(OperationsPerInvoke = 1000)]
public void AppendAllLines()
{
for (int i = 0; i < 1000; i++)
{
File.AppendAllLines(_testFilePath, _linesToAppend);
}
// We can't use:
// - [GlobalCleanup] because it could be invoked after millions of invocations of this benchmark, which could consume entire disk space
// - [IterationSetup] because it would invoke the benchmark once per iteration: https://github.com/dotnet/performance/blob/main/docs/microbenchmark-design-guidelines.md#IterationSetup
// This is why we Delete the file from the benchmark itself, but add plenty of OperationsPerInvoke so it's amortized
File.Delete(_testFilePath);
}
[GlobalSetup(Targets = new[] { nameof(AppendAllText), "AppendAllTextAsync", nameof(WriteAllText), "WriteAllTextAsync" })]
public void SetupAppendAllText()
{
_testFilePath = FileUtils.GetTestFilePath();
_textToAppend = new Dictionary<int, string>()
{
{ 10, new string('a', 10) },
{ 100, new string('a', 100) },
{ 10_000, new string('a', 10_000) },
{ 100_000, new string('a', 100_000) },
};
}
[Benchmark(OperationsPerInvoke = 1000)]
[Arguments(100)]
[Arguments(10_000)]
public void AppendAllText(int size)
{
string content = _textToAppend[size];
for (int i = 0; i < 1000; i++)
{
File.AppendAllText(_testFilePath, content);
}
File.Delete(_testFilePath); // see the comment in AppendAllLines
}
[Benchmark]
[Arguments(100)]
[Arguments(10_000)]
[Arguments(100_000)]
public void WriteAllText(int size) => File.WriteAllText(_testFilePath, _textToAppend[size]);
#if !NETFRAMEWORK
[BenchmarkCategory(Categories.NoWASM)]
[Benchmark(OperationsPerInvoke = 1000)]
public async Task AppendAllLinesAsync()
{
for (int i = 0; i < 1000; i++)
{
await File.AppendAllLinesAsync(_testFilePath, _linesToAppend);
}
File.Delete(_testFilePath); // see the comment in AppendAllLines
}
[BenchmarkCategory(Categories.NoWASM)]
[Benchmark(OperationsPerInvoke = 1000)]
[Arguments(100)]
[Arguments(10_000)]
public async Task AppendAllTextAsync(int size)
{
string content = _textToAppend[size];
for (int i = 0; i < 1000; i++)
{
await File.AppendAllTextAsync(_testFilePath, content);
}
File.Delete(_testFilePath); // see the comment in AppendAllLines
}
[BenchmarkCategory(Categories.NoWASM)]
[Benchmark]
[Arguments(100)]
[Arguments(10_000)]
[Arguments(100_000)]
public Task WriteAllTextAsync(int size) => File.WriteAllTextAsync(_testFilePath, _textToAppend[size]);
#endif
[Benchmark]
[Arguments(HalfKibibyte)]
[Arguments(FourKibibytes)]
[Arguments(OneMibibyte)]
[Arguments(HundredMibibytes)]
public void CopyTo(int size)
{
File.Delete(_testFilePath);
File.Copy(_filesToRead[size], _testFilePath); // overwrite defaults to false
}
[Benchmark]
[Arguments(HalfKibibyte)]
[Arguments(FourKibibytes)]
[Arguments(OneMibibyte)]
[Arguments(HundredMibibytes)]
public void CopyToOverwrite(int size) => File.Copy(_filesToRead[size], _testFilePath, overwrite: true);
}
}