diff options
Diffstat (limited to '')
-rw-r--r-- | FilesystemBenchmark/Benchmarks.cs | 134 | ||||
-rw-r--r-- | FilesystemBenchmark/FilesystemBenchmark.csproj | 19 | ||||
-rw-r--r-- | FilesystemBenchmark/Program.cs | 8 |
3 files changed, 161 insertions, 0 deletions
diff --git a/FilesystemBenchmark/Benchmarks.cs b/FilesystemBenchmark/Benchmarks.cs new file mode 100644 index 0000000..9ab044a --- /dev/null +++ b/FilesystemBenchmark/Benchmarks.cs @@ -0,0 +1,134 @@ +using System.Collections.Concurrent; +using System.Security.Cryptography; +using ArcaneLibs.Extensions; +using BenchmarkDotNet.Attributes; + +namespace FilesystemBenchmark; + +public class Benchmarks { + private string testPath = "/home/Rory/.local/share/ModerationClient/default/syncCache"; + + private EnumerationOptions enumOpts = new EnumerationOptions() { + MatchType = MatchType.Simple, + AttributesToSkip = FileAttributes.None, + IgnoreInaccessible = false, + RecurseSubdirectories = true + }; + + [Benchmark] + public void GetFilesMatching() { + _ = Directory.GetFiles(testPath, "*.*", SearchOption.AllDirectories).Count(); + } + + [Benchmark] + public void EnumerateFilesMatching() { + _ = Directory.EnumerateFiles(testPath, "*.*", SearchOption.AllDirectories).Count(); + } + + [Benchmark] + public void GetFilesMatchingSingleStar() { + _ = Directory.GetFiles(testPath, "*", SearchOption.AllDirectories).Count(); + } + + [Benchmark] + public void EnumerateFilesMatchingSingleStar() { + _ = Directory.EnumerateFiles(testPath, "*", SearchOption.AllDirectories).Count(); + } + + [Benchmark] + public void GetFilesMatchingSingleStarSimple() { + _ = Directory.GetFiles(testPath, "*", new EnumerationOptions() { + MatchType = MatchType.Simple, + AttributesToSkip = FileAttributes.None, + IgnoreInaccessible = false, + RecurseSubdirectories = true + }).Count(); + } + + [Benchmark] + public void EnumerateFilesMatchingSingleStarSimple() { + _ = Directory.EnumerateFiles(testPath, "*", new EnumerationOptions() { + MatchType = MatchType.Simple, + AttributesToSkip = FileAttributes.None, + IgnoreInaccessible = false, + RecurseSubdirectories = true + }).Count(); + } + + [Benchmark] + public void GetFilesMatchingSingleStarSimpleCached() { + _ = Directory.GetFiles(testPath, "*", enumOpts).Count(); + } + + [Benchmark] + public void EnumerateFilesMatchingSingleStarSimpleCached() { + _ = Directory.EnumerateFiles(testPath, "*", enumOpts).Count(); + } + + // [Benchmark] + // public void GetFilesRecursiveFunc() { + // GetFilesRecursive(testPath); + // } + // + // [Benchmark] + // public void GetFilesRecursiveParallelFunc() { + // GetFilesRecursiveParallel(testPath); + // } + // + // [Benchmark] + // public void GetFilesRecursiveEntriesFunc() { + // GetFilesRecursiveEntries(testPath); + // } + // + // [Benchmark] + // public void GetFilesRecursiveAsyncFunc() { + // GetFilesRecursiveAsync(testPath).ToBlockingEnumerable(); + // } + + + private List<string> GetFilesRecursive(string path) { + var result = new List<string>(); + foreach (var dir in Directory.GetDirectories(path)) { + result.AddRange(GetFilesRecursive(dir)); + } + + result.AddRange(Directory.GetFiles(path)); + return result; + } + + private List<string> GetFilesRecursiveEntries(string path) { + var result = new List<string>(); + foreach (var entry in Directory.EnumerateFileSystemEntries(path)) { + if (Directory.Exists(entry)) { + result.AddRange(GetFilesRecursiveEntries(entry)); + } + else { + result.Add(entry); + } + } + + return result; + } + + private List<string> GetFilesRecursiveParallel(string path) { + var result = new ConcurrentBag<string>(); + Directory.GetDirectories(path).AsParallel().ForAll(dir => { + GetFilesRecursiveParallel(dir).ForEach(result.Add); + }); + + Directory.GetFiles(path).AsParallel().ForAll(result.Add); + return result.ToList(); + } + + private async IAsyncEnumerable<string> GetFilesRecursiveAsync(string path) { + foreach (var dir in Directory.GetDirectories(path)) { + foreach (var file in GetFilesRecursiveAsync(dir).ToBlockingEnumerable()) { + yield return file; + } + } + + foreach (var file in Directory.GetFiles(path)) { + yield return file; + } + } +} \ No newline at end of file diff --git a/FilesystemBenchmark/FilesystemBenchmark.csproj b/FilesystemBenchmark/FilesystemBenchmark.csproj new file mode 100644 index 0000000..bb0af83 --- /dev/null +++ b/FilesystemBenchmark/FilesystemBenchmark.csproj @@ -0,0 +1,19 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <LangVersion>preview</LangVersion> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="BenchmarkDotNet" Version="0.14.0" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\LibMatrix\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj" /> + </ItemGroup> + +</Project> diff --git a/FilesystemBenchmark/Program.cs b/FilesystemBenchmark/Program.cs new file mode 100644 index 0000000..9454263 --- /dev/null +++ b/FilesystemBenchmark/Program.cs @@ -0,0 +1,8 @@ +// See https://aka.ms/new-console-template for more information + +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Configs; +using BenchmarkDotNet.Running; +using FilesystemBenchmark; + +BenchmarkRunner.Run<Benchmarks>(); \ No newline at end of file |