about summary refs log tree commit diff
path: root/FilesystemBenchmark/Benchmarks.cs
blob: 9ab044a362f9ac69b2935597aa0455b2a697f19e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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;
        }
    }
}