Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ public void SetupWriteAllBytes()
[Arguments(HundredMibibytes)]
public void WriteAllBytes(int size) => File.WriteAllBytes(_testFilePath, _userBuffers[size]);

[GlobalSetup(Targets = new[] { nameof(ReadAllBytes), "ReadAllBytesAsync" })]
[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);
File.WriteAllBytes(_testFilePath = Path.Combine(baseDir, Path.GetTempFileName()), Array.Empty<byte>());
_filesToRead = new Dictionary<int, string>()
{
{ HalfKibibyte, WriteBytes(HalfKibibyte) },
Expand All @@ -95,19 +98,21 @@ public void SetupReadAllBytes()
{ HundredMibibytes, WriteBytes(HundredMibibytes) },
};

static string WriteBytes(int fileSize)
string WriteBytes(int fileSize)
{
string filePath = FileUtils.GetTestFilePath();
string filePath = Path.Combine(baseDir, Path.GetTempFileName());
File.WriteAllBytes(filePath, ValuesGenerator.Array<byte>(fileSize));
return filePath;
}
}

[GlobalCleanup(Targets = new[] { nameof(ReadAllBytes), "ReadAllBytesAsync" })]
[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]
Expand Down Expand Up @@ -228,5 +233,23 @@ public async Task AppendAllTextAsync(int size)
[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);
}
}