Skip to content
Merged
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
46 changes: 46 additions & 0 deletions src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;

namespace System.IO.Tests
{
[BenchmarkCategory(Categories.Libraries)]
public class Perf_File
{
private const int OneKibibyte = 1 << 10; // 1024
private const int HalfKibibyte = OneKibibyte >> 1;
private const int FourKibibytes = OneKibibyte << 2; // default Stream buffer size
private const int SixteenKibibytes = FourKibibytes << 2; // default Stream buffer size * 4
private const int OneMibibyte = OneKibibyte << 10;
private const int HundredMibibytes = OneMibibyte * 100;

private const int DeleteteInnerIterations = 10;

private string _testFilePath;
private string[] _filesToRemove;
private Dictionary<int, byte[]> _userBuffers;

[GlobalSetup(Target = nameof(Exists))]
public void SetupExists()
Expand Down Expand Up @@ -46,5 +57,40 @@ public void Delete()
foreach (var file in filesToRemove)
File.Delete(file);
}

[GlobalSetup(Targets = new[] { nameof(WriteAllBytes), "WriteAllBytesAsync" })]
public void SetupWriteAllBytes()
{
_testFilePath = FileUtils.GetTestFilePath();
_userBuffers = new Dictionary<int, byte[]>()
{
{ HalfKibibyte, ValuesGenerator.Array<byte>(HalfKibibyte) },
{ FourKibibytes, ValuesGenerator.Array<byte>(FourKibibytes) },
{ SixteenKibibytes, ValuesGenerator.Array<byte>(SixteenKibibytes) },
{ OneMibibyte, ValuesGenerator.Array<byte>(OneMibibyte) },
{ HundredMibibytes, ValuesGenerator.Array<byte>(HundredMibibytes) },
};
}

[Benchmark]
[Arguments(HalfKibibyte)]
[Arguments(FourKibibytes)]
[Arguments(SixteenKibibytes)]
[Arguments(OneMibibyte)]
[Arguments(HundredMibibytes)]
public void WriteAllBytes(int size) => File.WriteAllBytes(_testFilePath, _userBuffers[size]);

#if !NETFRAMEWORK
[Benchmark]
[Arguments(HalfKibibyte)]
[Arguments(FourKibibytes)]
[Arguments(SixteenKibibytes)]
[Arguments(OneMibibyte)]
[Arguments(HundredMibibytes)]
public Task WriteAllBytesAsync(int size) => File.WriteAllBytesAsync(_testFilePath, _userBuffers[size]);
#endif

[GlobalCleanup(Targets = new[] { nameof(WriteAllBytes), "WriteAllBytesAsync" })]
public void CleanupWriteAllBytes() => File.Delete(_testFilePath);
}
}