Skip to content

Commit 0a8856c

Browse files
0ghnyfgreinacher
authored andcommitted
System-IO-Abstractions/System.IO.Abstractions/issues#598 Added benchmarks to project
1 parent 4e93383 commit 0a8856c

10 files changed

Lines changed: 367 additions & 3 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ build/
4545
*.log
4646
*.scc
4747

48+
# Benchmarks results
49+
BenchmarkDotNet.Artifacts
50+
4851
# Visual C++ cache files
4952
ipch/
5053
*.aps
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.IO;
3+
using System.IO.Abstractions.Benchmarks.Support;
4+
using BenchmarkDotNet.Attributes;
5+
using BenchmarkDotNet.Order;
6+
7+
namespace System.IO.Abstractions.Benchmarks
8+
{
9+
//[SimpleJob(launchCount: 3, warmupCount: 10, targetCount: 30)]
10+
[RPlotExporter]
11+
[MemoryDiagnoser]
12+
[Orderer(summaryOrderPolicy: SummaryOrderPolicy.FastestToSlowest)]
13+
[RankColumn]
14+
public class FileSystemAbstractionBenchmarks
15+
{
16+
#region Members
17+
/// <summary>
18+
/// FileSupport type to avoid counting object initialisation on the benchmark
19+
/// </summary>
20+
private FileSupport _fileSupport;
21+
private DirectorySupport _directorySupport;
22+
#endregion
23+
24+
#region CTOR's
25+
public FileSystemAbstractionBenchmarks( )
26+
{
27+
// Initialize file support
28+
_fileSupport = new FileSupport();
29+
_directorySupport = new DirectorySupport();
30+
}
31+
#endregion
32+
33+
#region File IsFile
34+
[Benchmark]
35+
public void FileExists_DotNet() => FileSupportStatic.IsFile(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
36+
37+
[Benchmark]
38+
public void FileExists_Abstraction() => _fileSupport.IsFile(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
39+
#endregion
40+
41+
#region Directory Exists
42+
[Benchmark]
43+
public void DirectoryExists_DotNet() => DirectorySupportStatic.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
44+
45+
[Benchmark]
46+
public void DirectoryExists_Abstraction() => _directorySupport.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
47+
#endregion
48+
}
49+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace System.IO.Abstractions.Benchmarks
2+
{
3+
using BenchmarkDotNet.Running;
4+
5+
class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
BenchmarkRunner.Run<FileSystemAbstractionBenchmarks>();
10+
}
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"yapm": {
4+
"commandName": "Project",
5+
"commandLineArgs": "apply"
6+
}
7+
}
8+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace System.IO.Abstractions.Benchmarks.Support
6+
{
7+
public class DirectorySupport
8+
{
9+
#region Members
10+
private IFileSystem _fileSystem;
11+
#endregion
12+
13+
#region CTOR's
14+
public DirectorySupport(IFileSystem fileSystem)
15+
{
16+
_fileSystem = fileSystem;
17+
}
18+
19+
public DirectorySupport() : this(new FileSystem())
20+
{
21+
// Default implementation for FileSystem
22+
}
23+
#endregion
24+
25+
#region Methods
26+
public bool IsDirectory(string path)
27+
{
28+
return _fileSystem.Directory.Exists(path);
29+
}
30+
31+
private string GetRandomTempDirectory()
32+
{
33+
return Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
34+
}
35+
36+
public string CreateRandomDirectory()
37+
{
38+
var randomPath = this.GetRandomTempDirectory();
39+
try
40+
{
41+
_fileSystem.Directory.CreateDirectory(randomPath);
42+
return randomPath;
43+
}
44+
catch (Exception ex)
45+
{
46+
throw (ex);
47+
}
48+
}
49+
50+
private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs = true, bool overwrite = true)
51+
{
52+
// Get the subdirectories for the specified directory.
53+
var dir = _fileSystem.DirectoryInfo.FromDirectoryName(sourceDirName);
54+
if (!dir.Exists)
55+
{
56+
throw new DirectoryNotFoundException(
57+
"Source directory does not exist or could not be found: "
58+
+ sourceDirName);
59+
}
60+
61+
var dirs = dir.GetDirectories();
62+
// If the destination directory doesn't exist, create it.
63+
if (!_fileSystem.Directory.Exists(destDirName))
64+
{
65+
_fileSystem.Directory.CreateDirectory(destDirName);
66+
}
67+
68+
// Get the files in the directory and copy them to the new location.
69+
var files = dir.GetFiles();
70+
foreach (var file in files)
71+
{
72+
string temppath = Path.Combine(destDirName, file.Name);
73+
file.CopyTo(temppath, overwrite);
74+
}
75+
76+
// If copying subdirectories, copy them and their contents to new location.
77+
if (copySubDirs)
78+
{
79+
foreach (var subdir in dirs)
80+
{
81+
string temppath = Path.Combine(destDirName, subdir.Name);
82+
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
83+
}
84+
}
85+
}
86+
87+
public void CreateIfNotExists(string directory)
88+
{
89+
if (!_fileSystem.Directory.Exists(directory))
90+
{
91+
_fileSystem.Directory.CreateDirectory(directory);
92+
}
93+
}
94+
95+
public bool Exists(string directory)
96+
{
97+
return _fileSystem.Directory.Exists(directory);
98+
}
99+
#endregion
100+
}
101+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
namespace System.IO.Abstractions.Benchmarks.Support
2+
{
3+
public static class DirectorySupportStatic
4+
{
5+
#region Methods
6+
public static bool IsDirectory(string path)
7+
{
8+
return Directory.Exists(path);
9+
}
10+
11+
private static string GetRandomTempDirectory()
12+
{
13+
return Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
14+
}
15+
16+
public static string CreateDirectory()
17+
{
18+
var randomPath = GetRandomTempDirectory();
19+
try
20+
{
21+
Directory.CreateDirectory(randomPath);
22+
return randomPath;
23+
}
24+
catch (Exception ex)
25+
{
26+
throw (ex);
27+
}
28+
}
29+
30+
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs = true, bool overwrite = true)
31+
{
32+
// Get the subdirectories for the specified directory.
33+
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
34+
35+
if (!dir.Exists)
36+
{
37+
throw new DirectoryNotFoundException(
38+
"Source directory does not exist or could not be found: "
39+
+ sourceDirName);
40+
}
41+
42+
DirectoryInfo[] dirs = dir.GetDirectories();
43+
// If the destination directory doesn't exist, create it.
44+
if (!Directory.Exists(destDirName))
45+
{
46+
Directory.CreateDirectory(destDirName);
47+
}
48+
49+
// Get the files in the directory and copy them to the new location.
50+
FileInfo[] files = dir.GetFiles();
51+
foreach (FileInfo file in files)
52+
{
53+
string temppath = Path.Combine(destDirName, file.Name);
54+
file.CopyTo(temppath, overwrite);
55+
}
56+
57+
// If copying subdirectories, copy them and their contents to new location.
58+
if (copySubDirs)
59+
{
60+
foreach (DirectoryInfo subdir in dirs)
61+
{
62+
string temppath = Path.Combine(destDirName, subdir.Name);
63+
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
64+
}
65+
}
66+
}
67+
68+
public static void CreateIfNotExists(string directory)
69+
{
70+
if (!Directory.Exists(directory))
71+
{
72+
Directory.CreateDirectory(directory);
73+
}
74+
}
75+
76+
public static bool Exists(string directory) => Directory.Exists(directory);
77+
#endregion
78+
}
79+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace System.IO.Abstractions.Benchmarks.Support
6+
{
7+
public class FileSupport
8+
{
9+
#region Members
10+
private IFileSystem _fileSystem;
11+
#endregion
12+
13+
#region CTOR's
14+
public FileSupport(IFileSystem fileSystem)
15+
{
16+
_fileSystem = fileSystem;
17+
}
18+
19+
public FileSupport() : this(new FileSystem())
20+
{
21+
// Default implementation for FileSystem
22+
}
23+
#endregion
24+
25+
#region Methods
26+
public string GetRandomTempFile()
27+
{
28+
return Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
29+
}
30+
31+
public bool IsFile(string path)
32+
{
33+
return _fileSystem.File.Exists(path);
34+
}
35+
36+
/// <summary>
37+
/// Checks and deletes given file if it does exists.
38+
/// </summary>
39+
/// <param name="filePath">Path of the file</param>
40+
public void DeleteIfExists(string filePath)
41+
{
42+
if (_fileSystem.File.Exists(filePath))
43+
{
44+
_fileSystem.File.Delete(filePath);
45+
}
46+
}
47+
#endregion
48+
}
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.IO;
2+
3+
namespace System.IO.Abstractions.Benchmarks.Support
4+
{
5+
public static class FileSupportStatic
6+
{
7+
public static string GetRandomTempFile()
8+
{
9+
return Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
10+
}
11+
12+
public static bool IsFile(string path)
13+
{
14+
return File.Exists(path);
15+
}
16+
17+
/// <summary>
18+
/// Checks and deletes given file if it does exists.
19+
/// </summary>
20+
/// <param name="filePath">Path of the file</param>
21+
public static void DeleteIfExists(string filePath)
22+
{
23+
if (File.Exists(filePath))
24+
{
25+
File.Delete(filePath);
26+
}
27+
}
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<AssemblyName>System.IO.Abstractions.Benchmarks</AssemblyName>
4+
<RootNamespace>System.IO.Abstractions.Benchmarks</RootNamespace>
5+
<Description>Bencharmks comparisons.</Description>
6+
<TargetFrameworks>netcoreapp3.1;net461</TargetFrameworks>
7+
<PackageProjectUrl>https://github.com/System-IO-Abstractions/System.IO.Abstractions</PackageProjectUrl>
8+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
9+
<PackageTags>testing</PackageTags>
10+
<IsPackable>false</IsPackable>
11+
<IsTestable>false</IsTestable>
12+
<OutputType>Exe</OutputType>
13+
<OutputType>Exe</OutputType>
14+
</PropertyGroup>
15+
<ItemGroup>
16+
<ProjectReference Include="..\System.IO.Abstractions\System.IO.Abstractions.csproj" />
17+
</ItemGroup>
18+
<ItemGroup>
19+
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net461" Version="1.0.0">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
22+
</PackageReference>
23+
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
24+
</ItemGroup>
25+
</Project>

0 commit comments

Comments
 (0)