Skip to content

Commit d1f3aa5

Browse files
authored
refactor: fix sonar issues in Benchmarks (#1238)
1 parent 111b3ab commit d1f3aa5

File tree

7 files changed

+19
-48
lines changed

7 files changed

+19
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.IO;
3-
using System.IO.Abstractions.Benchmarks.Support;
1+
using System.IO.Abstractions.Benchmarks.Support;
42
using BenchmarkDotNet.Attributes;
53
using BenchmarkDotNet.Order;
64

@@ -13,36 +11,34 @@ namespace System.IO.Abstractions.Benchmarks;
1311
[RankColumn]
1412
public class FileSystemAbstractionBenchmarks
1513
{
16-
#region Members
1714
/// <summary>
1815
/// FileSupport type to avoid counting object initialisation on the benchmark
1916
/// </summary>
20-
private FileSupport _fileSupport;
21-
private DirectorySupport _directorySupport;
22-
#endregion
17+
private readonly FileSupport _fileSupport;
18+
private readonly DirectorySupport _directorySupport;
19+
20+
private readonly string _path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
2321

24-
#region CTOR's
2522
public FileSystemAbstractionBenchmarks()
2623
{
2724
// Initialize file support
2825
_fileSupport = new FileSupport();
2926
_directorySupport = new DirectorySupport();
3027
}
31-
#endregion
3228

3329
#region File IsFile
3430
[Benchmark]
35-
public void FileExists_DotNet() => FileSupportStatic.IsFile(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
31+
public void FileExists_DotNet() => FileSupportStatic.IsFile(_path);
3632

3733
[Benchmark]
38-
public void FileExists_Abstraction() => _fileSupport.IsFile(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
34+
public void FileExists_Abstraction() => _fileSupport.IsFile(_path);
3935
#endregion
4036

4137
#region Directory Exists
4238
[Benchmark]
43-
public void DirectoryExists_DotNet() => DirectorySupportStatic.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
39+
public void DirectoryExists_DotNet() => DirectorySupportStatic.Exists(_path);
4440

4541
[Benchmark]
46-
public void DirectoryExists_Abstraction() => _directorySupport.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
42+
public void DirectoryExists_Abstraction() => _directorySupport.Exists(_path);
4743
#endregion
4844
}

benchmarks/TestableIO.System.IO.Abstractions.Benchmarks/MockFileSystemBenchmarks.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ private static Dictionary<string, MockFileData> CreateTestData()
2222
}
2323

2424
[Benchmark]
25-
public MockFileSystem MockFileSystem_Constructor() => new MockFileSystem(testData);
25+
public MockFileSystem MockFileSystem_Constructor() => new(testData);
2626
}

benchmarks/TestableIO.System.IO.Abstractions.Benchmarks/Program.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
namespace System.IO.Abstractions.Benchmarks;
22

33
using BenchmarkDotNet.Running;
4-
using System.Reflection;
54

6-
class Program
5+
static class Program
76
{
87
public static void Main(string[] args)
98
{

benchmarks/TestableIO.System.IO.Abstractions.Benchmarks/Support/DirectorySupport.cs

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
5-
namespace System.IO.Abstractions.Benchmarks.Support;
1+
namespace System.IO.Abstractions.Benchmarks.Support;
62

73
public class DirectorySupport
84
{
9-
#region Members
10-
private IFileSystem _fileSystem;
11-
#endregion
5+
private readonly IFileSystem _fileSystem;
126

13-
#region CTOR's
147
public DirectorySupport(IFileSystem fileSystem)
158
{
169
_fileSystem = fileSystem;
@@ -20,22 +13,20 @@ public DirectorySupport() : this(new FileSystem())
2013
{
2114
// Default implementation for FileSystem
2215
}
23-
#endregion
2416

25-
#region Methods
2617
public bool IsDirectory(string path)
2718
{
2819
return _fileSystem.Directory.Exists(path);
2920
}
3021

31-
private string GetRandomTempDirectory()
22+
private static string GetRandomTempDirectory()
3223
{
3324
return Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
3425
}
3526

3627
public string CreateRandomDirectory()
3728
{
38-
var randomPath = this.GetRandomTempDirectory();
29+
var randomPath = GetRandomTempDirectory();
3930
_fileSystem.Directory.CreateDirectory(randomPath);
4031
return randomPath;
4132
}
@@ -89,5 +80,4 @@ public bool Exists(string directory)
8980
{
9081
return _fileSystem.Directory.Exists(directory);
9182
}
92-
#endregion
9383
}

benchmarks/TestableIO.System.IO.Abstractions.Benchmarks/Support/DirectorySupportStatic.cs

-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
public static class DirectorySupportStatic
44
{
5-
#region Methods
65
public static bool IsDirectory(string path)
76
{
87
return Directory.Exists(path);
@@ -67,5 +66,4 @@ public static void CreateIfNotExists(string directory)
6766
}
6867

6968
public static bool Exists(string directory) => Directory.Exists(directory);
70-
#endregion
7169
}

benchmarks/TestableIO.System.IO.Abstractions.Benchmarks/Support/FileSupport.cs

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
5-
namespace System.IO.Abstractions.Benchmarks.Support;
1+
namespace System.IO.Abstractions.Benchmarks.Support;
62

73
public class FileSupport
84
{
9-
#region Members
10-
private IFileSystem _fileSystem;
11-
#endregion
5+
private readonly IFileSystem _fileSystem;
126

13-
#region CTOR's
147
public FileSupport(IFileSystem fileSystem)
158
{
169
_fileSystem = fileSystem;
@@ -20,10 +13,8 @@ public FileSupport() : this(new FileSystem())
2013
{
2114
// Default implementation for FileSystem
2215
}
23-
#endregion
2416

25-
#region Methods
26-
public string GetRandomTempFile()
17+
private static string GetRandomTempFile()
2718
{
2819
return Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
2920
}
@@ -44,5 +35,4 @@ public void DeleteIfExists(string filePath)
4435
_fileSystem.File.Delete(filePath);
4536
}
4637
}
47-
#endregion
4838
}

benchmarks/TestableIO.System.IO.Abstractions.Benchmarks/Support/FileSupportStatic.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.IO;
2-
3-
namespace System.IO.Abstractions.Benchmarks.Support;
1+
namespace System.IO.Abstractions.Benchmarks.Support;
42

53
public static class FileSupportStatic
64
{

0 commit comments

Comments
 (0)