Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ jobs:
matrix:
benchmark:
- CompleteTestBenchmarks
- EnumerationBenchmarks
- DirectoryTreeBenchmarks
- Enumeration10Benchmarks
- Enumeration1000Benchmarks
- FileInfoBenchmarks
- FileRoundTripBenchmarks
- FileStreamBenchmarks
- InitializationBenchmarks
- MockCreationBenchmarks
runs-on: ubuntu-latest
env:
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ jobs:
matrix:
benchmark:
- CompleteTestBenchmarks
- EnumerationBenchmarks
- DirectoryTreeBenchmarks
- Enumeration10Benchmarks
- Enumeration1000Benchmarks
- FileInfoBenchmarks
- FileRoundTripBenchmarks
- FileStreamBenchmarks
- InitializationBenchmarks
- MockCreationBenchmarks
runs-on: ubuntu-latest
env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Testably.Abstractions.Benchmarks;

/// <summary>
/// The method names follow <c>{Scenario}_{Library}</c>, which the benchmark comment and the docs page rely on.
/// Benchmarks that reuse a mock create it in an <c>[IterationSetup]</c> with a fixed <c>[InvocationCount]</c>, because the mock keeps a record of every call in its statistics.
/// </summary>
[Config(typeof(Config))]
[MarkdownExporterAttribute.GitHub]
Expand Down
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");
}
}
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; }
}
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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ namespace Testably.Abstractions.Benchmarks;
/// <summary>
/// Recursively enumerates a file system with <see cref="N" /> files in folders of ten, half of them <c>.txt</c>.
/// </summary>
public class EnumerationBenchmarks : BenchmarksBase
/// <remarks>
/// Each <see cref="N" /> has its own class, because the <c>[InvocationCount]</c> depends on it.
/// </remarks>
public abstract class EnumerationBenchmarks : BenchmarksBase
{
private const int FilesPerFolder = 10;

private SioaTesting.MockFileSystem _systemIOAbstractions = null!;
private MockFileSystem _testably = null!;

[Params(10, 1000)] public int N { get; set; }
public abstract int N { get; set; }

[Params("*", "*.txt")] public string Pattern { get; set; } = "*";

[GlobalSetup]
[IterationSetup]
public void Setup()
{
_testably = new MockFileSystem();
Expand Down
41 changes: 41 additions & 0 deletions Benchmarks/Testably.Abstractions.Benchmarks/FileInfoBenchmarks.cs
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);
}
}
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();
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

[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");
}
}
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;
}
}
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);
}
}
Loading
Loading