diff --git a/src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs b/src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs index 9ea4b7a616d..859da2eb2d2 100644 --- a/src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs +++ b/src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs @@ -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()); _filesToRead = new Dictionary() { { HalfKibibyte, WriteBytes(HalfKibibyte) }, @@ -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(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] @@ -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); } }