diff --git a/src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs b/src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs index f1430ee8535..631903b8f2f 100644 --- a/src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs +++ b/src/benchmarks/micro/libraries/System.IO.FileSystem/Perf.File.cs @@ -2,8 +2,11 @@ // 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 @@ -11,10 +14,18 @@ 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 _userBuffers; [GlobalSetup(Target = nameof(Exists))] public void SetupExists() @@ -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() + { + { HalfKibibyte, ValuesGenerator.Array(HalfKibibyte) }, + { FourKibibytes, ValuesGenerator.Array(FourKibibytes) }, + { SixteenKibibytes, ValuesGenerator.Array(SixteenKibibytes) }, + { OneMibibyte, ValuesGenerator.Array(OneMibibyte) }, + { HundredMibibytes, ValuesGenerator.Array(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); } }