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

refactor: use file-scoped namespaces #1237

Merged
merged 2 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,45 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;

namespace System.IO.Abstractions.Benchmarks
namespace System.IO.Abstractions.Benchmarks;

//[SimpleJob(launchCount: 3, warmupCount: 10, targetCount: 30)]
[RPlotExporter]
[MemoryDiagnoser]
[Orderer(summaryOrderPolicy: SummaryOrderPolicy.FastestToSlowest)]
[RankColumn]
public class FileSystemAbstractionBenchmarks
{
//[SimpleJob(launchCount: 3, warmupCount: 10, targetCount: 30)]
[RPlotExporter]
[MemoryDiagnoser]
[Orderer(summaryOrderPolicy: SummaryOrderPolicy.FastestToSlowest)]
[RankColumn]
public class FileSystemAbstractionBenchmarks
{
#region Members
/// <summary>
/// FileSupport type to avoid counting object initialisation on the benchmark
/// </summary>
private FileSupport _fileSupport;
private DirectorySupport _directorySupport;
#endregion
#region Members
/// <summary>
/// FileSupport type to avoid counting object initialisation on the benchmark
/// </summary>
private FileSupport _fileSupport;
private DirectorySupport _directorySupport;
#endregion

#region CTOR's
public FileSystemAbstractionBenchmarks()
{
// Initialize file support
_fileSupport = new FileSupport();
_directorySupport = new DirectorySupport();
}
#endregion
#region CTOR's
public FileSystemAbstractionBenchmarks()
{
// Initialize file support
_fileSupport = new FileSupport();
_directorySupport = new DirectorySupport();
}
#endregion

#region File IsFile
[Benchmark]
public void FileExists_DotNet() => FileSupportStatic.IsFile(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
#region File IsFile
[Benchmark]
public void FileExists_DotNet() => FileSupportStatic.IsFile(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));

[Benchmark]
public void FileExists_Abstraction() => _fileSupport.IsFile(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
#endregion
[Benchmark]
public void FileExists_Abstraction() => _fileSupport.IsFile(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
#endregion

#region Directory Exists
[Benchmark]
public void DirectoryExists_DotNet() => DirectorySupportStatic.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
#region Directory Exists
[Benchmark]
public void DirectoryExists_DotNet() => DirectorySupportStatic.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));

[Benchmark]
public void DirectoryExists_Abstraction() => _directorySupport.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
#endregion
}
}
[Benchmark]
public void DirectoryExists_Abstraction() => _directorySupport.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
using System.Linq;
using XFS = System.IO.Abstractions.TestingHelpers.MockUnixSupport;

namespace System.IO.Abstractions.Benchmarks
{
[RPlotExporter]
[MemoryDiagnoser]
public class MockFileSystemBenchmarks
{
private readonly Dictionary<string, MockFileData> testData = CreateTestData();
namespace System.IO.Abstractions.Benchmarks;

private static Dictionary<string, MockFileData> CreateTestData()
{
var filesCount = 100000;
var maxDirectoryDepth = 8;
return Enumerable.Range(0, filesCount).ToDictionary(
i => XFS.Path(@$"C:\{string.Join(@"\", Enumerable.Range(0, i % maxDirectoryDepth + 1).Select(i => i.ToString()))}\{i}.bin"),
i => new MockFileData(i.ToString()));
}
[RPlotExporter]
[MemoryDiagnoser]
public class MockFileSystemBenchmarks
{
private readonly Dictionary<string, MockFileData> testData = CreateTestData();

[Benchmark]
public MockFileSystem MockFileSystem_Constructor() => new MockFileSystem(testData);
private static Dictionary<string, MockFileData> CreateTestData()
{
var filesCount = 100000;
var maxDirectoryDepth = 8;
return Enumerable.Range(0, filesCount).ToDictionary(
i => XFS.Path(@$"C:\{string.Join(@"\", Enumerable.Range(0, i % maxDirectoryDepth + 1).Select(i => i.ToString()))}\{i}.bin"),
i => new MockFileData(i.ToString()));
}
}

[Benchmark]
public MockFileSystem MockFileSystem_Constructor() => new MockFileSystem(testData);
}
19 changes: 9 additions & 10 deletions benchmarks/TestableIO.System.IO.Abstractions.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
namespace System.IO.Abstractions.Benchmarks
{
using BenchmarkDotNet.Running;
using System.Reflection;
namespace System.IO.Abstractions.Benchmarks;

using BenchmarkDotNet.Running;
using System.Reflection;

class Program
class Program
{
public static void Main(string[] args)
{
public static void Main(string[] args)
{
BenchmarkRunner.Run(typeof(Program).Assembly);
}
BenchmarkRunner.Run(typeof(Program).Assembly);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,92 @@
using System.Collections.Generic;
using System.Text;

namespace System.IO.Abstractions.Benchmarks.Support
namespace System.IO.Abstractions.Benchmarks.Support;

public class DirectorySupport
{
public class DirectorySupport
#region Members
private IFileSystem _fileSystem;
#endregion

#region CTOR's
public DirectorySupport(IFileSystem fileSystem)
{
#region Members
private IFileSystem _fileSystem;
#endregion
_fileSystem = fileSystem;
}

#region CTOR's
public DirectorySupport(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
public DirectorySupport() : this(new FileSystem())
{
// Default implementation for FileSystem
}
#endregion

public DirectorySupport() : this(new FileSystem())
{
// Default implementation for FileSystem
}
#endregion
#region Methods
public bool IsDirectory(string path)
{
return _fileSystem.Directory.Exists(path);
}

#region Methods
public bool IsDirectory(string path)
{
return _fileSystem.Directory.Exists(path);
}
private string GetRandomTempDirectory()
{
return Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
}

public string CreateRandomDirectory()
{
var randomPath = this.GetRandomTempDirectory();
_fileSystem.Directory.CreateDirectory(randomPath);
return randomPath;
}

private string GetRandomTempDirectory()
private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs = true, bool overwrite = true)
{
// Get the subdirectories for the specified directory.
var dir = _fileSystem.DirectoryInfo.New(sourceDirName);
if (!dir.Exists)
{
return Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}

public string CreateRandomDirectory()
var dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!_fileSystem.Directory.Exists(destDirName))
{
var randomPath = this.GetRandomTempDirectory();
_fileSystem.Directory.CreateDirectory(randomPath);
return randomPath;
_fileSystem.Directory.CreateDirectory(destDirName);
}

private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs = true, bool overwrite = true)
// Get the files in the directory and copy them to the new location.
var files = dir.GetFiles();
foreach (var file in files)
{
// Get the subdirectories for the specified directory.
var dir = _fileSystem.DirectoryInfo.New(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}

var dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!_fileSystem.Directory.Exists(destDirName))
{
_fileSystem.Directory.CreateDirectory(destDirName);
}

// Get the files in the directory and copy them to the new location.
var files = dir.GetFiles();
foreach (var file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, overwrite);
}

// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (var subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, overwrite);
}

public void CreateIfNotExists(string directory)
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
if (!_fileSystem.Directory.Exists(directory))
foreach (var subdir in dirs)
{
_fileSystem.Directory.CreateDirectory(directory);
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}

public bool Exists(string directory)
public void CreateIfNotExists(string directory)
{
if (!_fileSystem.Directory.Exists(directory))
{
return _fileSystem.Directory.Exists(directory);
_fileSystem.Directory.CreateDirectory(directory);
}
#endregion
}
}

public bool Exists(string directory)
{
return _fileSystem.Directory.Exists(directory);
}
#endregion
}
Loading
Loading