Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add File.CopyTo benchmarks #2134

Merged
merged 1 commit into from
Nov 16, 2021
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);
}
}