Benchmarks
These benchmarks compare Testably.Abstractions with System.IO.Abstractions for the same calls. The mock file system of Testably.Abstractions models more of the real behavior, such as file locks, timestamps and the rules of each operating system, so some of its extra time is the cost of behaving like the real file system.
- Complete test
- Initialize
- Create mock
- Read and write
- File info
- File stream
- Move directory
- Enumerate
A complete unit test: seed a small project, let the code under test read its configuration, write the sources into one output file, back it up and delete a source, then check the result. Both libraries run the same calls.
System.IO.Abstractions takes 2.6× as long.
Lower is better. Bars longer than four times the fastest library are cut so the others stay comparable. The line after each time shows the last runs compared to Testably.Abstractions. Hover a bar, a multiple or a line for the exact comparison.
MockFileSystem fileSystem = new();
fileSystem.Directory.CreateDirectory("project/src");
fileSystem.File.WriteAllText("project/config.json", "{\"output\":\"out/bundle.txt\"}");
for (int i = 0; i < 5; i++)
{
fileSystem.File.WriteAllText($"project/src/file{i}.cs", $"class C{i} {{ }}");
}
string config = fileSystem.File.ReadAllText("project/config.json");
fileSystem.Directory.CreateDirectory("project/out");
using (StreamWriter writer = fileSystem.File.CreateText("project/out/bundle.txt"))
{
foreach (string file in fileSystem.Directory.EnumerateFiles("project/src", "*.cs"))
{
writer.WriteLine(fileSystem.File.ReadAllText(file));
}
}
fileSystem.File.Copy("project/out/bundle.txt", "project/out/bundle.bak");
fileSystem.File.Delete("project/src/file0.cs");
Seed a test fixture with N files in one directory, each library the way its documentation recommends: Testably.Abstractions with its initializer, System.IO.Abstractions with a dictionary of files.
System.IO.Abstractions is faster here at 23.55 µs; Testably.Abstractions takes 1.0× as long.
Lower is better. Bars longer than four times the fastest library are cut so the others stay comparable. The line after each time shows the last runs compared to Testably.Abstractions. Hover a bar, a multiple or a line for the exact comparison.
MockFileSystem fileSystem = new();
fileSystem.Initialize().WithSubdirectory("project").Initialized(project =>
{
for (int i = 0; i < N; i++)
{
project.WithFile($"file{i}.txt").Which(f => f.HasStringContent($"content of file {i}"));
}
});
The cost of creating an empty mock file system, which every test pays.
System.IO.Abstractions takes 3.7× as long.
Lower is better. Bars longer than four times the fastest library are cut so the others stay comparable. The line after each time shows the last runs compared to Testably.Abstractions. Hover a bar, a multiple or a line for the exact comparison.
new MockFileSystem();
Write a text file and read it back, like code that stores and loads a configuration or a document.
System.IO.Abstractions is faster here at 7.93 µs; Testably.Abstractions takes 1.1× as long.
Lower is better. Bars longer than four times the fastest library are cut so the others stay comparable. The line after each time shows the last runs compared to Testably.Abstractions. Hover a bar, a multiple or a line for the exact comparison.
fileSystem.File.WriteAllText("document.txt", content);
fileSystem.File.ReadAllText("document.txt");
Inspect the metadata of a file, like code that decides whether a file is new or large enough to process.
System.IO.Abstractions is faster here at 1.36 µs; Testably.Abstractions takes 1.1× as long.
Lower is better. Bars longer than four times the fastest library are cut so the others stay comparable. The line after each time shows the last runs compared to Testably.Abstractions. Hover a bar, a multiple or a line for the exact comparison.
IFileInfo file = fileSystem.FileInfo.New("data/report.csv");
_ = (file.Exists, file.Length, file.LastWriteTimeUtc, file.Name, file.DirectoryName);
Write 64 KB through a file stream in chunks of 4 KB and read it back the same way, like code that copies or hashes a file.
System.IO.Abstractions takes 1.9× as long.
Lower is better. Bars longer than four times the fastest library are cut so the others stay comparable. The line after each time shows the last runs compared to Testably.Abstractions. Hover a bar, a multiple or a line for the exact comparison.
using (Stream stream = fileSystem.FileStream.New("data.bin", FileMode.Create))
{
for (int i = 0; i < 16; i++)
{
stream.Write(chunk, 0, 4096);
}
}
using (Stream stream = fileSystem.FileStream.New("data.bin", FileMode.Open))
{
while (stream.Read(chunk, 0, 4096) > 0)
{
}
}
Move a directory with 10 subdirectories of 10 files each and move it back, like code that archives a folder.
System.IO.Abstractions is faster here at 61.92 µs; Testably.Abstractions takes 4.2× as long.
Lower is better. Bars longer than four times the fastest library are cut so the others stay comparable. The line after each time shows the last runs compared to Testably.Abstractions. Hover a bar, a multiple or a line for the exact comparison.
fileSystem.Directory.Move("source", "archive");
fileSystem.Directory.Move("archive", "source");
Recursively enumerate a mock file system with N files in folders of ten, either all files (*) or only the .txt files, which are half of them.
System.IO.Abstractions takes 1.2× as long.
Lower is better. Bars longer than four times the fastest library are cut so the others stay comparable. The line after each time shows the last runs compared to Testably.Abstractions. Hover a bar, a multiple or a line for the exact comparison.
fileSystem.Directory.EnumerateFiles("root", Pattern, SearchOption.AllDirectories).Count();
How these numbers are measured
- Suite:
Testably.Abstractions.Benchmarksdriven by BenchmarkDotNet. - Job:
MediumRunwith the in-process toolchain on .NET 10. - Runner:
ubuntu-latestGitHub Actions agent, refreshed on every push tomain. - Parameters: the initialization benchmark runs with 10 and 100 files, the read and write benchmark with 1 KB and 64 KB of text, and the enumeration benchmark with 10 and 1,000 files and with the patterns
*and*.txt; each value can be selected in the chart. - Charts: every multiple is relative to Testably.Abstractions, and bars longer than four times the fastest library are cut so the others stay comparable. Lower is better.