Repository navigation
ci: add benchmarks for realistic file system usage #1247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Benchmarks/Testably.Abstractions.Benchmarks/DirectoryTreeBenchmarks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using System.IO.Abstractions; | ||
| using BenchmarkDotNet.Attributes; | ||
| using Testably.Abstractions.Testing; | ||
| using SioaTesting = System.IO.Abstractions.TestingHelpers; | ||
|
|
||
| namespace Testably.Abstractions.Benchmarks; | ||
|
|
||
| /// <summary> | ||
| /// Moves a directory with 10 subdirectories of 10 files each and moves it back, like code that archives a folder. | ||
| /// </summary> | ||
| [InvocationCount(512)] | ||
| public class DirectoryTreeBenchmarks : BenchmarksBase | ||
| { | ||
| private SioaTesting.MockFileSystem _systemIOAbstractions = null!; | ||
| private MockFileSystem _testably = null!; | ||
|
|
||
| [IterationSetup] | ||
| public void Setup() | ||
| { | ||
| _testably = new MockFileSystem(); | ||
| _systemIOAbstractions = new SioaTesting.MockFileSystem(); | ||
| foreach (IFileSystem fileSystem in new IFileSystem[] { _testably, _systemIOAbstractions, }) | ||
| { | ||
| for (int directory = 0; directory < 10; directory++) | ||
| { | ||
| string path = Path.Combine("source", $"d{directory}"); | ||
| fileSystem.Directory.CreateDirectory(path); | ||
| for (int file = 0; file < 10; file++) | ||
| { | ||
| fileSystem.File.WriteAllText(Path.Combine(path, $"f{file}.txt"), "content"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public void MoveTree_Testably() | ||
| { | ||
| _testably.Directory.Move("source", "archive"); | ||
| _testably.Directory.Move("archive", "source"); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public void MoveTree_SystemIOAbstractions() | ||
| { | ||
| _systemIOAbstractions.Directory.Move("source", "archive"); | ||
| _systemIOAbstractions.Directory.Move("archive", "source"); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
Benchmarks/Testably.Abstractions.Benchmarks/Enumeration1000Benchmarks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using BenchmarkDotNet.Attributes; | ||
|
|
||
| namespace Testably.Abstractions.Benchmarks; | ||
|
|
||
| [InvocationCount(512)] | ||
| public class Enumeration1000Benchmarks : EnumerationBenchmarks | ||
| { | ||
| [Params(1000)] public override int N { get; set; } | ||
| } |
9 changes: 9 additions & 0 deletions
9
Benchmarks/Testably.Abstractions.Benchmarks/Enumeration10Benchmarks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using BenchmarkDotNet.Attributes; | ||
|
|
||
| namespace Testably.Abstractions.Benchmarks; | ||
|
|
||
| [InvocationCount(16384)] | ||
| public class Enumeration10Benchmarks : EnumerationBenchmarks | ||
| { | ||
| [Params(10)] public override int N { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Benchmarks/Testably.Abstractions.Benchmarks/FileInfoBenchmarks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using System.IO.Abstractions; | ||
| using BenchmarkDotNet.Attributes; | ||
| using Testably.Abstractions.Testing; | ||
| using SioaTesting = System.IO.Abstractions.TestingHelpers; | ||
|
|
||
| namespace Testably.Abstractions.Benchmarks; | ||
|
|
||
| /// <summary> | ||
| /// Inspects the metadata of a file, like code that decides whether a file is new or large enough to process. | ||
| /// </summary> | ||
| [InvocationCount(65536)] | ||
| public class FileInfoBenchmarks : BenchmarksBase | ||
| { | ||
| private SioaTesting.MockFileSystem _systemIOAbstractions = null!; | ||
| private MockFileSystem _testably = null!; | ||
|
|
||
| [IterationSetup] | ||
| public void Setup() | ||
| { | ||
| _testably = new MockFileSystem(); | ||
| _testably.Directory.CreateDirectory("data"); | ||
| _testably.File.WriteAllText(Path.Combine("data", "report.csv"), "a,b,c"); | ||
| _systemIOAbstractions = new SioaTesting.MockFileSystem(); | ||
| _systemIOAbstractions.Directory.CreateDirectory("data"); | ||
| _systemIOAbstractions.File.WriteAllText(Path.Combine("data", "report.csv"), "a,b,c"); | ||
| } | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public (bool, long, DateTime, string, string?) Inspect_Testably() | ||
| => Inspect(_testably); | ||
|
|
||
| [Benchmark] | ||
| public (bool, long, DateTime, string, string?) Inspect_SystemIOAbstractions() | ||
| => Inspect(_systemIOAbstractions); | ||
|
|
||
| private static (bool, long, DateTime, string, string?) Inspect(IFileSystem fileSystem) | ||
| { | ||
| IFileInfo file = fileSystem.FileInfo.New(Path.Combine("data", "report.csv")); | ||
| return (file.Exists, file.Length, file.LastWriteTimeUtc, file.Name, file.DirectoryName); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
Benchmarks/Testably.Abstractions.Benchmarks/FileRoundTripBenchmarks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using BenchmarkDotNet.Attributes; | ||
| using Testably.Abstractions.Testing; | ||
| using SioaTesting = System.IO.Abstractions.TestingHelpers; | ||
|
|
||
| namespace Testably.Abstractions.Benchmarks; | ||
|
|
||
| /// <summary> | ||
| /// Writes a text file of <see cref="Size" /> characters and reads it back, like code that stores and loads a | ||
| /// configuration or a document. | ||
| /// </summary> | ||
| [InvocationCount(4096)] | ||
| public class FileRoundTripBenchmarks : BenchmarksBase | ||
| { | ||
| private string _content = null!; | ||
| private SioaTesting.MockFileSystem _systemIOAbstractions = null!; | ||
| private MockFileSystem _testably = null!; | ||
|
|
||
| [Params(1024, 65536)] public int Size { get; set; } | ||
|
|
||
| [GlobalSetup] | ||
| public void GlobalSetup() | ||
| => _content = new string('x', Size); | ||
|
|
||
| [IterationSetup] | ||
| public void Setup() | ||
| { | ||
| _testably = new MockFileSystem(); | ||
| _systemIOAbstractions = new SioaTesting.MockFileSystem(); | ||
| } | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public string RoundTrip_Testably() | ||
| { | ||
| _testably.File.WriteAllText("document.txt", _content); | ||
| return _testably.File.ReadAllText("document.txt"); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public string RoundTrip_SystemIOAbstractions() | ||
| { | ||
| _systemIOAbstractions.File.WriteAllText("document.txt", _content); | ||
| return _systemIOAbstractions.File.ReadAllText("document.txt"); | ||
| } | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
Benchmarks/Testably.Abstractions.Benchmarks/FileStreamBenchmarks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using System.IO.Abstractions; | ||
| using BenchmarkDotNet.Attributes; | ||
| using Testably.Abstractions.Testing; | ||
| using SioaTesting = System.IO.Abstractions.TestingHelpers; | ||
|
|
||
| namespace Testably.Abstractions.Benchmarks; | ||
|
|
||
| /// <summary> | ||
| /// Writes 64 KB through a file stream in chunks of 4 KB and reads it back the same way, like code that copies or | ||
| /// hashes a file. | ||
| /// </summary> | ||
| [InvocationCount(2048)] | ||
| public class FileStreamBenchmarks : BenchmarksBase | ||
| { | ||
| private const int ChunkSize = 4096; | ||
| private const int ChunkCount = 16; | ||
|
|
||
| private readonly byte[] _chunk = new byte[ChunkSize]; | ||
| private SioaTesting.MockFileSystem _systemIOAbstractions = null!; | ||
| private MockFileSystem _testably = null!; | ||
|
|
||
| [IterationSetup] | ||
| public void Setup() | ||
| { | ||
| _testably = new MockFileSystem(); | ||
| _systemIOAbstractions = new SioaTesting.MockFileSystem(); | ||
| } | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public long Chunks_Testably() | ||
| => WriteAndRead(_testably); | ||
|
|
||
| [Benchmark] | ||
| public long Chunks_SystemIOAbstractions() | ||
| => WriteAndRead(_systemIOAbstractions); | ||
|
|
||
| private long WriteAndRead(IFileSystem fileSystem) | ||
| { | ||
| using (Stream stream = fileSystem.FileStream.New("data.bin", FileMode.Create)) | ||
| { | ||
| for (int i = 0; i < ChunkCount; i++) | ||
| { | ||
| stream.Write(_chunk, 0, ChunkSize); | ||
| } | ||
| } | ||
|
|
||
| long total = 0; | ||
| using (Stream stream = fileSystem.FileStream.New("data.bin", FileMode.Open)) | ||
| { | ||
| int read; | ||
| while ((read = stream.Read(_chunk, 0, ChunkSize)) > 0) | ||
| { | ||
| total += read; | ||
| } | ||
| } | ||
|
|
||
| return total; | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
Benchmarks/Testably.Abstractions.Benchmarks/InitializationBenchmarks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| using BenchmarkDotNet.Attributes; | ||
| using Testably.Abstractions.Testing; | ||
| using SioaTesting = System.IO.Abstractions.TestingHelpers; | ||
|
|
||
| namespace Testably.Abstractions.Benchmarks; | ||
|
|
||
| /// <summary> | ||
| /// Seeds a test fixture with <see cref="N" /> files in one directory, each library the way its documentation | ||
| /// recommends. | ||
| /// </summary> | ||
| public class InitializationBenchmarks : BenchmarksBase | ||
| { | ||
| private string[] _contents = null!; | ||
| private string[] _fileNames = null!; | ||
| private string[] _paths = null!; | ||
|
|
||
| [Params(10, 100)] public int N { get; set; } | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| _fileNames = Enumerable.Range(0, N).Select(i => $"file{i}.txt").ToArray(); | ||
| _paths = _fileNames.Select(name => Path.Combine("project", name)).ToArray(); | ||
| _contents = Enumerable.Range(0, N).Select(i => $"content of file {i}").ToArray(); | ||
| } | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public object Initialize_Testably() | ||
| { | ||
| MockFileSystem fileSystem = new(); | ||
| fileSystem.Initialize().WithSubdirectory("project").Initialized(project => | ||
| { | ||
| for (int i = 0; i < _fileNames.Length; i++) | ||
| { | ||
| string content = _contents[i]; | ||
| project.WithFile(_fileNames[i]).Which(f => f.HasStringContent(content)); | ||
| } | ||
| }); | ||
| return fileSystem; | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public object Initialize_SystemIOAbstractions() | ||
| { | ||
| Dictionary<string, SioaTesting.MockFileData> files = new(_paths.Length); | ||
| for (int i = 0; i < _paths.Length; i++) | ||
| { | ||
| files[_paths[i]] = new SioaTesting.MockFileData(_contents[i]); | ||
| } | ||
|
|
||
| return new SioaTesting.MockFileSystem(files); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.