From c5f233785f18c2f160f45b50f8909a04164812bc Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 27 Jul 2021 16:53:08 -0400 Subject: [PATCH] Revise the PipeStream perf tests --- .../Perf.AnonymousPipeStream.cs | 13 +---- .../System.IO.Pipes/Perf.NamedPipeStream.cs | 46 ++-------------- .../System.IO.Pipes/Perf.PipeTest.cs | 55 +++++++++++++++++-- 3 files changed, 56 insertions(+), 58 deletions(-) diff --git a/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.AnonymousPipeStream.cs b/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.AnonymousPipeStream.cs index 2591fb9abb4..b9b169779a8 100644 --- a/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.AnonymousPipeStream.cs +++ b/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.AnonymousPipeStream.cs @@ -4,7 +4,7 @@ namespace System.IO.Pipes.Tests { - public class Perf_AnonymousPipeStream_ServerIn_ClientOut : Perf_PipeTest + public class Perf_AnonymousPipeStream : Perf_PipeTest { protected override ServerClientPair CreateServerClientPair() { @@ -14,15 +14,4 @@ protected override ServerClientPair CreateServerClientPair() return ret; } } - - public class Perf_AnonymousPipeStream_ServerOut_ClientIn : Perf_PipeTest - { - protected override ServerClientPair CreateServerClientPair() - { - ServerClientPair ret = new ServerClientPair(); - ret.writeablePipe = new AnonymousPipeServerStream(PipeDirection.Out); - ret.readablePipe = new AnonymousPipeClientStream(PipeDirection.In, ((AnonymousPipeServerStream)ret.writeablePipe).ClientSafePipeHandle); - return ret; - } - } } diff --git a/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.NamedPipeStream.cs b/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.NamedPipeStream.cs index 3788dd0ee3a..db3029cbfb6 100644 --- a/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.NamedPipeStream.cs +++ b/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.NamedPipeStream.cs @@ -2,56 +2,22 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using BenchmarkDotNet.Attributes; using System.Threading.Tasks; namespace System.IO.Pipes.Tests { - public class Perf_NamedPipeStream_ServerOut_ClientIn : Perf_PipeTest + public class Perf_NamedPipeStream : Perf_PipeTest { - protected override ServerClientPair CreateServerClientPair() - { - ServerClientPair ret = new ServerClientPair(); - string pipeName = GetUniquePipeName(); - var writeablePipe = new NamedPipeServerStream(pipeName, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); - var readablePipe = new NamedPipeClientStream(".", pipeName, PipeDirection.In, PipeOptions.Asynchronous); + [Params(PipeOptions.None, PipeOptions.Asynchronous)] + public PipeOptions Options { get; set; } - Task clientConnect = readablePipe.ConnectAsync(); - writeablePipe.WaitForConnection(); - clientConnect.Wait(); - - ret.readablePipe = readablePipe; - ret.writeablePipe = writeablePipe; - return ret; - } - } - - public class Perf_NamedPipeStream_ServerIn_ClientOut : Perf_PipeTest - { - protected override ServerClientPair CreateServerClientPair() - { - ServerClientPair ret = new ServerClientPair(); - string pipeName = GetUniquePipeName(); - var readablePipe = new NamedPipeServerStream(pipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); - var writeablePipe = new NamedPipeClientStream(".", pipeName, PipeDirection.Out, PipeOptions.Asynchronous); - - Task clientConnect = writeablePipe.ConnectAsync(); - readablePipe.WaitForConnection(); - clientConnect.Wait(); - - ret.readablePipe = readablePipe; - ret.writeablePipe = writeablePipe; - return ret; - } - } - - public class Perf_NamedPipeStream_ServerInOut_ClientInOut : Perf_PipeTest - { protected override ServerClientPair CreateServerClientPair() { ServerClientPair ret = new ServerClientPair(); string pipeName = GetUniquePipeName(); - var readablePipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); - var writeablePipe = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous); + var readablePipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, Options); + var writeablePipe = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, Options); Task clientConnect = writeablePipe.ConnectAsync(); readablePipe.WaitForConnection(); diff --git a/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.PipeTest.cs b/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.PipeTest.cs index 3970d31abdc..cc8db724aaf 100644 --- a/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.PipeTest.cs +++ b/src/benchmarks/micro/libraries/System.IO.Pipes/Perf.PipeTest.cs @@ -11,6 +11,8 @@ namespace System.IO.Pipes.Tests [BenchmarkCategory(Categories.Libraries, Categories.NoWASM)] public abstract class Perf_PipeTest : PipeTestBase { + private const int Iterations = 1000; + [Params(1000000)] public int size; @@ -32,15 +34,56 @@ public void Setup() [GlobalCleanup] public void Cleanup() => _serverClientPair.Dispose(); - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public async Task ReadWrite() { - Task write = Task.Run(() => _serverClientPair.writeablePipe.Write(_sent, 0, _sent.Length)); - int totalReadLength = 0; - while (totalReadLength < _sent.Length) + Task write = Task.Run(delegate + { + for (int i = 0; i < Iterations; i++) + { + _serverClientPair.writeablePipe.Write(_sent, 0, _sent.Length); + } + }); + + for (int i = 0; i < Iterations; i++) + { + int totalReadLength = 0; + while (totalReadLength < _sent.Length) + { + totalReadLength += _serverClientPair.readablePipe.Read(_received, totalReadLength, size - totalReadLength); + } + } + + await write; + } + + [Benchmark(OperationsPerInvoke = Iterations)] + public async Task ReadWriteAsync() + { + Task write = Task.Run(async delegate + { + for (int i = 0; i < Iterations; i++) + { +#if !NETFRAMEWORK + await _serverClientPair.writeablePipe.WriteAsync(_sent); +#else + await _serverClientPair.writeablePipe.WriteAsync(_sent, 0, _sent.Length); +#endif + } + }); + + for (int i = 0; i < Iterations; i++) { - int readLength = _serverClientPair.readablePipe.Read(_received, totalReadLength, size - totalReadLength); - totalReadLength += readLength; + int totalReadLength = 0; + while (totalReadLength < _sent.Length) + { + totalReadLength += await +#if !NETFRAMEWORK + _serverClientPair.readablePipe.ReadAsync(_received.AsMemory(totalReadLength)); +#else + _serverClientPair.readablePipe.ReadAsync(_received, totalReadLength, size - totalReadLength); +#endif + } } await write;