From 23bbc17bde681271815a2406dbcff19ef04af1d2 Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Wed, 7 Sep 2022 19:28:13 +0000 Subject: [PATCH] Fix some randomly failing Perf.File tests on wasm/aot On wasm/aot, `System.IO.FileSystem/Perf.File`'s `CopyTo`, `CopyToOverride`, and `ReadAllBytes` benchmarks fail randomly with: ``` ---> System.IO.IOException: The file '/tmp/' already exists. at BenchmarkDotNet.Autogenerated.Runnable_48.Run(IHost host, String benchmarkName) at System.Reflection.MethodInvoker.InterpretedInvoke(Object , Span`1 , BindingFlags ) --- End of inner exception stack trace --- at BenchmarkDotNet.Autogenerated.UniqueProgramName.AfterAssemblyLoadingAttached(String[] args) ``` .. due to `Path.GetTempFileName()` being buggy - https://github.com/dotnet/runtime/issues/73721. In `SetupReadAllBytes`, we are building the test file path as: ```csharp _testFilePath = Path.Combine(baseDir, Path.GetTempFileName()) ``` .. but the `Path.GetTempFileName()` is not appropriate here, as it will return a full path, and it will create the file. Instead we can use `Path.GetRandomFileName()` which should avoid the underlying issue completely. Fixes https://github.com/dotnet/runtime/issues/74104 . --- .../micro/libraries/System.IO.FileSystem/Perf.File.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 e242f834bc8..ded2a0470b6 100644 --- a/src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs +++ b/src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs @@ -94,7 +94,7 @@ 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()); + File.WriteAllBytes(_testFilePath = Path.Combine(baseDir, Path.GetRandomFileName()), Array.Empty()); _filesToRead = new Dictionary() { { HalfKibibyte, WriteBytes(HalfKibibyte) }, @@ -106,7 +106,7 @@ public void SetupReadAllBytes() string WriteBytes(int fileSize) { - string filePath = Path.Combine(baseDir, Path.GetTempFileName()); + string filePath = Path.Combine(baseDir, Path.GetRandomFileName()); File.WriteAllBytes(filePath, ValuesGenerator.Array(fileSize)); return filePath; }