Skip to content

Commit

Permalink
refactor: fix sonar issues in Benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Mar 1, 2025
1 parent 111b3ab commit b01f2de
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.IO.Abstractions.Benchmarks.Support;
using System.IO.Abstractions.Benchmarks.Support;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;

Expand All @@ -13,36 +11,34 @@ namespace System.IO.Abstractions.Benchmarks;
[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
private readonly FileSupport _fileSupport;
private readonly DirectorySupport _directorySupport;

private readonly string _path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

#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));
public void FileExists_DotNet() => FileSupportStatic.IsFile(_path);

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

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

[Benchmark]
public void DirectoryExists_Abstraction() => _directorySupport.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
public void DirectoryExists_Abstraction() => _directorySupport.Exists(_path);
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ private static Dictionary<string, MockFileData> CreateTestData()
}

[Benchmark]
public MockFileSystem MockFileSystem_Constructor() => new MockFileSystem(testData);
public MockFileSystem MockFileSystem_Constructor() => new(testData);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace System.IO.Abstractions.Benchmarks;

using BenchmarkDotNet.Running;
using System.Reflection;

class Program
static class Program
{
public static void Main(string[] args)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;

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

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

#region CTOR's
public DirectorySupport(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
Expand All @@ -20,22 +13,20 @@ public DirectorySupport() : this(new FileSystem())
{
// Default implementation for FileSystem
}
#endregion

#region Methods
public bool IsDirectory(string path)
{
return _fileSystem.Directory.Exists(path);
}

private string GetRandomTempDirectory()
private static string GetRandomTempDirectory()
{
return Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
}

public string CreateRandomDirectory()
{
var randomPath = this.GetRandomTempDirectory();
var randomPath = GetRandomTempDirectory();
_fileSystem.Directory.CreateDirectory(randomPath);
return randomPath;
}
Expand Down Expand Up @@ -89,5 +80,4 @@ public bool Exists(string directory)
{
return _fileSystem.Directory.Exists(directory);
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public static class DirectorySupportStatic
{
#region Methods
public static bool IsDirectory(string path)
{
return Directory.Exists(path);
Expand Down Expand Up @@ -67,5 +66,4 @@ public static void CreateIfNotExists(string directory)
}

public static bool Exists(string directory) => Directory.Exists(directory);
#endregion
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;

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

public class FileSupport
{
#region Members
private IFileSystem _fileSystem;
#endregion
private readonly IFileSystem _fileSystem;

#region CTOR's
public FileSupport(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
Expand All @@ -20,10 +13,8 @@ public FileSupport() : this(new FileSystem())
{
// Default implementation for FileSystem
}
#endregion

#region Methods
public string GetRandomTempFile()
private static string GetRandomTempFile()
{
return Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
}
Expand All @@ -44,5 +35,4 @@ public void DeleteIfExists(string filePath)
_fileSystem.File.Delete(filePath);
}
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.IO;

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

public static class FileSupportStatic
{
Expand Down

0 comments on commit b01f2de

Please sign in to comment.