NetSharp

NetSharp.git
git clone git://git.lenczewski.org/NetSharp.git
Log | Files | Refs | README | LICENSE

commit da345df286b253f6fb2e8b369c210c1e979f542a
parent e5205a37ab7b580e45e1b8a9711cc08abb371b51
Author: Mikolaj Lenczewski <mikolaj.lenczewski308@gmail.com>
Date:   Thu, 18 Jun 2020 12:56:09 +0100

Moved NetSharp.Examples to correct directory, and started implementing new wave of benchmarks using BenchmarkDotNet

Diffstat:
MNetSharp/NetSharp.Benchmarks/BenchmarkHelper.cs | 8++++++++
MNetSharp/NetSharp.Benchmarks/Benchmarks/Datagram Network Connection Benchmarks/DatagramNetworkReaderBenchmark.cs | 195+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
MNetSharp/NetSharp.Benchmarks/Benchmarks/Datagram Network Connection Benchmarks/DatagramNetworkWriterAsyncBenchmark.cs | 14+++++---------
MNetSharp/NetSharp.Benchmarks/Benchmarks/Datagram Network Connection Benchmarks/DatagramNetworkWriterSyncBenchmark.cs | 14+++++---------
MNetSharp/NetSharp.Benchmarks/Benchmarks/Stream Network Connection Benchmarks/StreamNetworkReaderBenchmark.cs | 14+++++---------
MNetSharp/NetSharp.Benchmarks/Benchmarks/Stream Network Connection Benchmarks/StreamNetworkWriterAsyncBenchmark.cs | 16++++++----------
MNetSharp/NetSharp.Benchmarks/Benchmarks/Stream Network Connection Benchmarks/StreamNetworkWriterSyncBenchmark.cs | 16++++++----------
MNetSharp/NetSharp.Benchmarks/INetSharpBenchmark.cs | 9+--------
MNetSharp/NetSharp.Benchmarks/NetSharp.Benchmarks.csproj | 6+++++-
MNetSharp/NetSharp.Benchmarks/Program.cs | 45++++++++++++++++++++++-----------------------
RNetSharp/NetSharpExamples/Examples/Datagram Network Connection Examples/DatagramNetworkReaderExample.cs -> NetSharp/NetSharp.Examples/Examples/Datagram Network Connection Examples/DatagramNetworkReaderExample.cs | 0
RNetSharp/NetSharpExamples/Examples/Datagram Network Connection Examples/DatagramNetworkWriterAsyncExample.cs -> NetSharp/NetSharp.Examples/Examples/Datagram Network Connection Examples/DatagramNetworkWriterAsyncExample.cs | 0
RNetSharp/NetSharpExamples/Examples/Datagram Network Connection Examples/DatagramNetworkWriterSyncExample.cs -> NetSharp/NetSharp.Examples/Examples/Datagram Network Connection Examples/DatagramNetworkWriterSyncExample.cs | 0
RNetSharp/NetSharpExamples/Examples/Stream Network Connection Examples/RawStreamChatServer.cs -> NetSharp/NetSharp.Examples/Examples/Stream Network Connection Examples/RawStreamChatServer.cs | 0
RNetSharp/NetSharpExamples/Examples/Stream Network Connection Examples/StreamNetworkReaderExample.cs -> NetSharp/NetSharp.Examples/Examples/Stream Network Connection Examples/StreamNetworkReaderExample.cs | 0
RNetSharp/NetSharpExamples/Examples/Stream Network Connection Examples/StreamNetworkWriterAsyncExample.cs -> NetSharp/NetSharp.Examples/Examples/Stream Network Connection Examples/StreamNetworkWriterAsyncExample.cs | 0
RNetSharp/NetSharpExamples/Examples/Stream Network Connection Examples/StreamNetworkWriterSyncExample.cs -> NetSharp/NetSharp.Examples/Examples/Stream Network Connection Examples/StreamNetworkWriterSyncExample.cs | 0
RNetSharp/NetSharpExamples/INetSharpExample.cs -> NetSharp/NetSharp.Examples/INetSharpExample.cs | 0
ANetSharp/NetSharp.Examples/NetSharp.Examples.csproj | 12++++++++++++
RNetSharp/NetSharpExamples/NetSharpExamples.xml -> NetSharp/NetSharp.Examples/NetSharpExamples.xml | 0
RNetSharp/NetSharpExamples/Program.cs -> NetSharp/NetSharp.Examples/Program.cs | 0
MNetSharp/NetSharp.Tests/NetSharp.Tests.csproj | 4++++
MNetSharp/NetSharp.Tests/Program.cs | 2+-
MNetSharp/NetSharp.sln | 8++++----
MNetSharp/NetSharp/AssemblyAttributes.cs | 1+
DNetSharp/NetSharpExamples/NetSharp.Examples.csproj | 21---------------------
26 files changed, 221 insertions(+), 164 deletions(-)

diff --git a/NetSharp/NetSharp.Benchmarks/BenchmarkHelper.cs b/NetSharp/NetSharp.Benchmarks/BenchmarkHelper.cs @@ -19,6 +19,14 @@ namespace NetSharp.Benchmarks public long RttTicks => stopwatch.ElapsedTicks; + public static double CalcBandwidth(long elapsedMilliseconds, long sentPacketCount, long sentPacketSize, long bandwidthDownscalingFactor = 1_000_000) + { + double bytes = sentPacketCount * sentPacketSize / bandwidthDownscalingFactor; + double bandwidth = bytes / (elapsedMilliseconds / 1000.0); + + return bandwidth; + } + public double CalcBandwidth(long sentPacketCount, long packetSize) { long millis = stopwatch.ElapsedMilliseconds; diff --git a/NetSharp/NetSharp.Benchmarks/Benchmarks/Datagram Network Connection Benchmarks/DatagramNetworkReaderBenchmark.cs b/NetSharp/NetSharp.Benchmarks/Benchmarks/Datagram Network Connection Benchmarks/DatagramNetworkReaderBenchmark.cs @@ -1,23 +1,36 @@ using System; +using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Net; using System.Net.Sockets; -using System.Text; using System.Threading; using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; + using NetSharp.Raw.Datagram; namespace NetSharp.Benchmarks.Benchmarks.Datagram_Network_Connection_Benchmarks { - internal class DatagramNetworkReaderBenchmark : INetSharpBenchmark + public class DatagramNetworkReaderBenchmark : INetSharpBenchmark { - private const int PacketSize = 8192, PacketCount = 1_000_000, ClientCount = 12; - private static readonly EndPoint ClientEndPoint = Program.DefaultClientEndPoint; - private static readonly Encoding ServerEncoding = Program.DefaultEncoding; - private static readonly EndPoint ServerEndPoint = Program.DefaultServerEndPoint; - private static readonly ManualResetEventSlim ServerReadyEvent = new ManualResetEventSlim(); - private double[] ClientBandwidths; + private const int ClientCount = 12; + private const int MaxPacketCount = 1_000_000, MinPacketCount = 100_000, PacketCountInterval = 100_000; + private const int MaxPacketSize = 65535 / 2, MinPacketSize = 8192; + private static readonly EndPoint ServerDefaultListenEndpoint = new IPEndPoint(IPAddress.Any, 0); + private static readonly ManualResetEventSlim ServerReadyEvent = new ManualResetEventSlim(false); + private Task[] _clientTasks; + private Socket _serverSocket; + private double[] ClientMegabyteBandwidths; + + public ushort BenchmarkClientCount { get; set; } = ClientCount; + + [ParamsSource(nameof(PacketCountParamsSource))] + public int BenchmarkPacketCount { get; set; } + + [ParamsSource(nameof(PacketSizeParamsSource))] + public int BenchmarkPacketSize { get; set; } /// <inheritdoc /> public string Name { get; } = "Raw Datagram Network Reader Benchmark"; @@ -32,87 +45,151 @@ namespace NetSharp.Benchmarks.Benchmarks.Datagram_Network_Connection_Benchmarks private Task BenchmarkClientTask(object idObj) { - try - { - int id = (int) idObj; + int id = (int) idObj; - BenchmarkHelper benchmarkHelper = new BenchmarkHelper(); + Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + clientSocket.Bind(Program.Constants.ClientEndPoint); - Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - clientSocket.Bind(ClientEndPoint); + ServerReadyEvent.Wait(); - ServerReadyEvent.Wait(); + byte[] transmissionBuffer = new byte[BenchmarkPacketSize]; - byte[] sendBuffer = new byte[PacketSize]; - byte[] receiveBuffer = new byte[PacketSize]; + EndPoint remoteEndPoint = Program.Constants.ServerEndPoint; + Stopwatch bandwidthStopwatch = new Stopwatch(); + bandwidthStopwatch.Reset(); - EndPoint remoteEndPoint = ServerEndPoint; + for (int i = 0; i < BenchmarkPacketCount; i++) + { + byte[] packetBuffer = Program.Constants.ServerEncoding.GetBytes($"[Client {id}] Hello World! (Packet {i})"); + packetBuffer.CopyTo(transmissionBuffer, 0); - lock (typeof(Console)) - { - Console.WriteLine($"[Client {id}] Starting client; sending messages to {remoteEndPoint}"); - } + bandwidthStopwatch.Start(); + int sentBytes = clientSocket.SendTo(transmissionBuffer, remoteEndPoint); - for (int i = 0; i < PacketCount; i++) - { - byte[] packetBuffer = ServerEncoding.GetBytes($"[Client {id}] Hello World! (Packet {i})"); - packetBuffer.CopyTo(sendBuffer, 0); + Array.Clear(transmissionBuffer, 0, BenchmarkPacketSize); - benchmarkHelper.StartStopwatch(); - int sentBytes = clientSocket.SendTo(sendBuffer, remoteEndPoint); + int receivedBytes = clientSocket.ReceiveFrom(transmissionBuffer, ref remoteEndPoint); + bandwidthStopwatch.Stop(); + } - int receivedBytes = clientSocket.ReceiveFrom(receiveBuffer, ref remoteEndPoint); - benchmarkHelper.StopStopwatch(); + ClientMegabyteBandwidths[id] = BenchmarkHelper.CalcBandwidth(bandwidthStopwatch.ElapsedMilliseconds, BenchmarkPacketCount, BenchmarkPacketSize); - benchmarkHelper.SnapshotRttStats(); - } + clientSocket.Close(); + clientSocket.Dispose(); - benchmarkHelper.PrintBandwidthStats(id, PacketCount, PacketSize); - benchmarkHelper.PrintRttStats(id); + return Task.CompletedTask; + } - ClientBandwidths[id] = benchmarkHelper.CalcBandwidth(PacketCount, PacketSize); - } - catch (Exception ex) + public static IEnumerable<int> PacketCountParamsSource() + { + for (int i = MinPacketCount; i <= MaxPacketCount; i += PacketCountInterval) { - Console.WriteLine(ex); + yield return i; } - - return Task.CompletedTask; } - /// <inheritdoc /> - public async Task RunAsync() + public static IEnumerable<int> PacketSizeParamsSource() { - if (PacketCount > 10_000) + for (int i = MinPacketSize; i < MaxPacketSize; i *= 2) { - Console.WriteLine($"{PacketCount} packets will be sent per client. This could take a long time (maybe more than a minute)!"); + yield return i; } + } + + [Benchmark(Description = "Measures performance of a Datagram Network Reader with multiple clients")] + public void MultipleClientPerformance() + { + using RawDatagramNetworkReader reader = new RawDatagramNetworkReader(ref _serverSocket, RequestHandler, ServerDefaultListenEndpoint, BenchmarkPacketSize); + reader.Start(BenchmarkClientCount); + + ServerReadyEvent.Set(); + + Task.WhenAll(_clientTasks).GetAwaiter().GetResult(); + + reader.Shutdown(); + } + + [IterationCleanup(Target = nameof(MultipleClientPerformance))] + public void MultipleClientPerformanceCleanup() + { + Console.WriteLine($"Total estimated bandwidth: {ClientMegabyteBandwidths.Sum():F3}"); - ClientBandwidths = new double[ClientCount]; - Task[] clientTasks = new Task[ClientCount]; - for (int i = 0; i < clientTasks.Length; i++) + _serverSocket.Close(); + _serverSocket.Dispose(); + } + + [GlobalCleanup(Target = nameof(MultipleClientPerformance))] + public void MultipleClientPerformanceGlobalCleanup() + { + } + + [GlobalSetup(Target = nameof(MultipleClientPerformance))] + public void MultipleClientPerformanceGlobalSetup() + { + } + + [IterationSetup(Target = nameof(MultipleClientPerformance))] + public void MultipleClientPerformanceSetup() + { + Console.WriteLine($"Packet Size: {BenchmarkPacketSize}, Packet Count: {BenchmarkPacketCount}, Client Count: {BenchmarkClientCount}"); + + ServerReadyEvent.Reset(); + + ClientMegabyteBandwidths = new double[BenchmarkClientCount]; + _clientTasks = new Task[BenchmarkClientCount]; + for (int i = 0; i < _clientTasks.Length; i++) { - clientTasks[i] = Task.Factory.StartNew(BenchmarkClientTask, i, TaskCreationOptions.LongRunning); + _clientTasks[i] = Task.Factory.StartNew(BenchmarkClientTask, i, TaskCreationOptions.LongRunning); } - EndPoint defaultRemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); - - Socket rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - rawSocket.Bind(ServerEndPoint); + _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + _serverSocket.Bind(Program.Constants.ServerEndPoint); + } - using RawDatagramNetworkReader reader = new RawDatagramNetworkReader(ref rawSocket, RequestHandler, defaultRemoteEndPoint, PacketSize); - reader.Start(ClientCount); + [Benchmark(Description = "Measures performance of a Datagram Network Reader with a single client")] + public void SingleClientPerformance() + { + using RawDatagramNetworkReader reader = new RawDatagramNetworkReader(ref _serverSocket, RequestHandler, ServerDefaultListenEndpoint, BenchmarkPacketSize); + reader.Start(BenchmarkClientCount); ServerReadyEvent.Set(); - await Task.WhenAll(clientTasks); - - Console.WriteLine($"Total estimated bandwidth: {ClientBandwidths.Sum():F3}"); + _clientTasks[0].GetAwaiter().GetResult(); reader.Shutdown(); + } + + [IterationCleanup(Target = nameof(SingleClientPerformance))] + public void SingleClientPerformanceCleanup() + { + Console.WriteLine($"Total estimated bandwidth: {ClientMegabyteBandwidths.Sum():F3}"); + + _serverSocket.Close(); + _serverSocket.Dispose(); + } + + [GlobalCleanup(Target = nameof(SingleClientPerformance))] + public void SingleClientPerformanceGlobalCleanup() + { + } + + [GlobalSetup(Target = nameof(SingleClientPerformance))] + public void SingleClientPerformanceGlobalSetup() + { + } + + [IterationSetup(Target = nameof(SingleClientPerformance))] + public void SingleClientPerformanceSetup() + { + Console.WriteLine($"Packet Size: {BenchmarkPacketSize}, Packet Count: {BenchmarkPacketCount}, Client Count: {BenchmarkClientCount}"); + + ServerReadyEvent.Reset(); + + ClientMegabyteBandwidths = new double[BenchmarkClientCount]; + _clientTasks = new Task[] { Task.Factory.StartNew(BenchmarkClientTask, 0, TaskCreationOptions.LongRunning) }; - rawSocket.Close(); - rawSocket.Dispose(); + _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + _serverSocket.Bind(Program.Constants.ServerEndPoint); } } } \ No newline at end of file diff --git a/NetSharp/NetSharp.Benchmarks/Benchmarks/Datagram Network Connection Benchmarks/DatagramNetworkWriterAsyncBenchmark.cs b/NetSharp/NetSharp.Benchmarks/Benchmarks/Datagram Network Connection Benchmarks/DatagramNetworkWriterAsyncBenchmark.cs @@ -1,7 +1,6 @@ using System; using System.Net; using System.Net.Sockets; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -12,9 +11,6 @@ namespace NetSharp.Benchmarks.Benchmarks.Datagram_Network_Connection_Benchmarks internal class DatagramNetworkWriterAsyncBenchmark : INetSharpBenchmark { private const int PacketSize = 8192, PacketCount = 1_000_000; - private static readonly EndPoint ClientEndPoint = Program.DefaultClientEndPoint; - private static readonly Encoding ServerEncoding = Program.DefaultEncoding; - private static readonly EndPoint ServerEndPoint = Program.DefaultServerEndPoint; private static readonly ManualResetEventSlim ServerReadyEvent = new ManualResetEventSlim(); /// <inheritdoc /> @@ -24,7 +20,7 @@ namespace NetSharp.Benchmarks.Benchmarks.Datagram_Network_Connection_Benchmarks { using Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - server.Bind(ServerEndPoint); + server.Bind(Program.Constants.ServerEndPoint); ServerReadyEvent.Set(); byte[] transmissionBuffer = new byte[PacketSize]; @@ -54,7 +50,7 @@ namespace NetSharp.Benchmarks.Benchmarks.Datagram_Network_Connection_Benchmarks EndPoint defaultRemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); Socket rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - rawSocket.Bind(ClientEndPoint); + rawSocket.Bind(Program.Constants.ClientEndPoint); using RawDatagramNetworkWriter writer = new RawDatagramNetworkWriter(ref rawSocket, defaultRemoteEndPoint, PacketSize); @@ -70,13 +66,13 @@ namespace NetSharp.Benchmarks.Benchmarks.Datagram_Network_Connection_Benchmarks for (int i = 0; i < PacketCount; i++) { - byte[] packetBuffer = ServerEncoding.GetBytes($"[Client 0] Hello World! (Packet {i})"); + byte[] packetBuffer = Program.Constants.ServerEncoding.GetBytes($"[Client 0] Hello World! (Packet {i})"); packetBuffer.CopyTo(sendBuffer, 0); benchmarkHelper.StartStopwatch(); - int sendResult = await writer.WriteAsync(ServerEndPoint, sendBuffer); + int sendResult = await writer.WriteAsync(Program.Constants.ServerEndPoint, sendBuffer); - int receiveResult = await writer.ReadAsync(ServerEndPoint, receiveBuffer); + int receiveResult = await writer.ReadAsync(Program.Constants.ServerEndPoint, receiveBuffer); benchmarkHelper.StopStopwatch(); benchmarkHelper.SnapshotRttStats(); diff --git a/NetSharp/NetSharp.Benchmarks/Benchmarks/Datagram Network Connection Benchmarks/DatagramNetworkWriterSyncBenchmark.cs b/NetSharp/NetSharp.Benchmarks/Benchmarks/Datagram Network Connection Benchmarks/DatagramNetworkWriterSyncBenchmark.cs @@ -1,7 +1,6 @@ using System; using System.Net; using System.Net.Sockets; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -12,9 +11,6 @@ namespace NetSharp.Benchmarks.Benchmarks.Datagram_Network_Connection_Benchmarks internal class DatagramNetworkWriterSyncBenchmark : INetSharpBenchmark { private const int PacketSize = 8192, PacketCount = 1_000_000; - private static readonly EndPoint ClientEndPoint = Program.DefaultClientEndPoint; - private static readonly Encoding ServerEncoding = Program.DefaultEncoding; - private static readonly EndPoint ServerEndPoint = Program.DefaultServerEndPoint; private static readonly ManualResetEventSlim ServerReadyEvent = new ManualResetEventSlim(); /// <inheritdoc /> @@ -24,7 +20,7 @@ namespace NetSharp.Benchmarks.Benchmarks.Datagram_Network_Connection_Benchmarks { using Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - server.Bind(ServerEndPoint); + server.Bind(Program.Constants.ServerEndPoint); ServerReadyEvent.Set(); byte[] transmissionBuffer = new byte[PacketSize]; @@ -54,7 +50,7 @@ namespace NetSharp.Benchmarks.Benchmarks.Datagram_Network_Connection_Benchmarks EndPoint defaultRemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); Socket rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - rawSocket.Bind(ClientEndPoint); + rawSocket.Bind(Program.Constants.ClientEndPoint); using RawDatagramNetworkWriter writer = new RawDatagramNetworkWriter(ref rawSocket, defaultRemoteEndPoint, PacketSize); @@ -68,15 +64,15 @@ namespace NetSharp.Benchmarks.Benchmarks.Datagram_Network_Connection_Benchmarks byte[] sendBuffer = new byte[PacketSize]; byte[] receiveBuffer = new byte[PacketSize]; - EndPoint remoteEndPoint = ServerEndPoint; + EndPoint remoteEndPoint = Program.Constants.ServerEndPoint; for (int i = 0; i < PacketCount; i++) { - byte[] packetBuffer = ServerEncoding.GetBytes($"[Client 0] Hello World! (Packet {i})"); + byte[] packetBuffer = Program.Constants.ServerEncoding.GetBytes($"[Client 0] Hello World! (Packet {i})"); packetBuffer.CopyTo(sendBuffer, 0); benchmarkHelper.StartStopwatch(); - int sendResult = writer.Write(ServerEndPoint, sendBuffer); + int sendResult = writer.Write(Program.Constants.ServerEndPoint, sendBuffer); int receiveResult = writer.Read(ref remoteEndPoint, receiveBuffer); benchmarkHelper.StopStopwatch(); diff --git a/NetSharp/NetSharp.Benchmarks/Benchmarks/Stream Network Connection Benchmarks/StreamNetworkReaderBenchmark.cs b/NetSharp/NetSharp.Benchmarks/Benchmarks/Stream Network Connection Benchmarks/StreamNetworkReaderBenchmark.cs @@ -2,7 +2,6 @@ using System.Linq; using System.Net; using System.Net.Sockets; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -14,9 +13,6 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks internal class StreamNetworkReaderBenchmark : INetSharpBenchmark { private const int PacketSize = 8192, PacketCount = 1_000_000, ClientCount = 12; - private static readonly EndPoint ClientEndPoint = Program.DefaultClientEndPoint; - private static readonly Encoding ServerEncoding = Program.DefaultEncoding; - private static readonly EndPoint ServerEndPoint = Program.DefaultServerEndPoint; private static readonly ManualResetEventSlim ServerReadyEvent = new ManualResetEventSlim(); private double[] ClientBandwidths; @@ -38,16 +34,16 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks BenchmarkHelper benchmarkHelper = new BenchmarkHelper(); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - clientSocket.Bind(ClientEndPoint); + clientSocket.Bind(Program.Constants.ClientEndPoint); ServerReadyEvent.Wait(); - clientSocket.Connect(ServerEndPoint); + clientSocket.Connect(Program.Constants.ServerEndPoint); byte[] sendBuffer = new byte[PacketSize + RawStreamPacketHeader.TotalSize]; byte[] receiveBuffer = new byte[PacketSize + RawStreamPacketHeader.TotalSize]; byte[] packetBuffer = new byte[PacketSize]; - EndPoint remoteEndPoint = ServerEndPoint; + EndPoint remoteEndPoint = Program.Constants.ServerEndPoint; lock (typeof(Console)) { @@ -56,7 +52,7 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks for (int i = 0; i < PacketCount; i++) { - ServerEncoding.GetBytes($"[Client {id}] Hello World! (Packet {i})").CopyTo(packetBuffer, 0); + Program.Constants.ServerEncoding.GetBytes($"[Client {id}] Hello World! (Packet {i})").CopyTo(packetBuffer, 0); RawStreamPacketHeader streamPacketHeader = new RawStreamPacketHeader(PacketSize); RawStreamPacket.Serialise(sendBuffer, in streamPacketHeader, packetBuffer); @@ -121,7 +117,7 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks EndPoint defaultEndPoint = new IPEndPoint(IPAddress.Any, 0); Socket rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - rawSocket.Bind(ServerEndPoint); + rawSocket.Bind(Program.Constants.ServerEndPoint); rawSocket.Listen(ClientCount); using RawStreamNetworkReader reader = new RawStreamNetworkReader(ref rawSocket, RequestHandler, defaultEndPoint, PacketSize); diff --git a/NetSharp/NetSharp.Benchmarks/Benchmarks/Stream Network Connection Benchmarks/StreamNetworkWriterAsyncBenchmark.cs b/NetSharp/NetSharp.Benchmarks/Benchmarks/Stream Network Connection Benchmarks/StreamNetworkWriterAsyncBenchmark.cs @@ -1,7 +1,6 @@ using System; using System.Net; using System.Net.Sockets; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -13,9 +12,6 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks internal class StreamNetworkWriterAsyncBenchmark : INetSharpBenchmark { private const int PacketSize = 8192, PacketCount = 1_000_000; - private static readonly EndPoint ClientEndPoint = Program.DefaultClientEndPoint; - private static readonly Encoding ServerEncoding = Program.DefaultEncoding; - private static readonly EndPoint ServerEndPoint = Program.DefaultServerEndPoint; private static readonly ManualResetEventSlim ServerReadyEvent = new ManualResetEventSlim(); /// <inheritdoc /> @@ -25,7 +21,7 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks { using Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - server.Bind(ServerEndPoint); + server.Bind(Program.Constants.ServerEndPoint); ServerReadyEvent.Set(); RawStreamPacketHeader archetypalHeader = new RawStreamPacketHeader(PacketSize); @@ -78,7 +74,7 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks EndPoint defaultRemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); Socket rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - rawSocket.Bind(ClientEndPoint); + rawSocket.Bind(Program.Constants.ClientEndPoint); using RawStreamNetworkWriter writer = new RawStreamNetworkWriter(ref rawSocket, defaultRemoteEndPoint, PacketSize); @@ -86,7 +82,7 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks Task serverTask = Task.Factory.StartNew(state => ServerTask((CancellationToken) state), serverCts.Token, TaskCreationOptions.LongRunning); ServerReadyEvent.Wait(); - rawSocket.Connect(ServerEndPoint); + rawSocket.Connect(Program.Constants.ServerEndPoint); BenchmarkHelper benchmarkHelper = new BenchmarkHelper(); @@ -95,13 +91,13 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks for (int i = 0; i < PacketCount; i++) { - byte[] packetBuffer = ServerEncoding.GetBytes($"[Client 0] Hello World! (Packet {i})"); + byte[] packetBuffer = Program.Constants.ServerEncoding.GetBytes($"[Client 0] Hello World! (Packet {i})"); packetBuffer.CopyTo(sendBuffer, 0); benchmarkHelper.StartStopwatch(); - int sendResult = await writer.WriteAsync(ServerEndPoint, sendBuffer); + int sendResult = await writer.WriteAsync(Program.Constants.ServerEndPoint, sendBuffer); - int receiveResult = await writer.ReadAsync(ServerEndPoint, receiveBuffer); + int receiveResult = await writer.ReadAsync(Program.Constants.ServerEndPoint, receiveBuffer); benchmarkHelper.StopStopwatch(); benchmarkHelper.SnapshotRttStats(); diff --git a/NetSharp/NetSharp.Benchmarks/Benchmarks/Stream Network Connection Benchmarks/StreamNetworkWriterSyncBenchmark.cs b/NetSharp/NetSharp.Benchmarks/Benchmarks/Stream Network Connection Benchmarks/StreamNetworkWriterSyncBenchmark.cs @@ -1,7 +1,6 @@ using System; using System.Net; using System.Net.Sockets; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -13,9 +12,6 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks internal class StreamNetworkWriterSyncBenchmark : INetSharpBenchmark { private const int PacketSize = 8192, PacketCount = 1_000_000; - private static readonly EndPoint ClientEndPoint = Program.DefaultClientEndPoint; - private static readonly Encoding ServerEncoding = Program.DefaultEncoding; - private static readonly EndPoint ServerEndPoint = Program.DefaultServerEndPoint; private static readonly ManualResetEventSlim ServerReadyEvent = new ManualResetEventSlim(); /// <inheritdoc /> @@ -25,7 +21,7 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks { using Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - server.Bind(ServerEndPoint); + server.Bind(Program.Constants.ServerEndPoint); ServerReadyEvent.Set(); // all the headers should have the same packet size, so will fit in the transmission buffer @@ -79,7 +75,7 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks EndPoint defaultRemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); Socket rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - rawSocket.Bind(ClientEndPoint); + rawSocket.Bind(Program.Constants.ClientEndPoint); using RawStreamNetworkWriter writer = new RawStreamNetworkWriter(ref rawSocket, defaultRemoteEndPoint, PacketSize); @@ -87,22 +83,22 @@ namespace NetSharp.Benchmarks.Benchmarks.Stream_Network_Connection_Benchmarks Task serverTask = Task.Factory.StartNew(state => ServerTask((CancellationToken) state), serverCts.Token, TaskCreationOptions.LongRunning); ServerReadyEvent.Wait(); - rawSocket.Connect(ServerEndPoint); + rawSocket.Connect(Program.Constants.ServerEndPoint); BenchmarkHelper benchmarkHelper = new BenchmarkHelper(); byte[] sendBuffer = new byte[PacketSize]; byte[] receiveBuffer = new byte[PacketSize]; - EndPoint remoteEndPoint = ServerEndPoint; + EndPoint remoteEndPoint = Program.Constants.ServerEndPoint; for (int i = 0; i < PacketCount; i++) { - byte[] packetBuffer = ServerEncoding.GetBytes($"[Client 0] Hello World! (Packet {i})"); + byte[] packetBuffer = Program.Constants.ServerEncoding.GetBytes($"[Client 0] Hello World! (Packet {i})"); packetBuffer.CopyTo(sendBuffer, 0); benchmarkHelper.StartStopwatch(); - int sendResult = writer.Write(ServerEndPoint, sendBuffer); + int sendResult = writer.Write(Program.Constants.ServerEndPoint, sendBuffer); int receiveResult = writer.Read(ref remoteEndPoint, receiveBuffer); benchmarkHelper.StopStopwatch(); diff --git a/NetSharp/NetSharp.Benchmarks/INetSharpBenchmark.cs b/NetSharp/NetSharp.Benchmarks/INetSharpBenchmark.cs @@ -1,6 +1,4 @@ -using System.Threading.Tasks; - -namespace NetSharp.Benchmarks +namespace NetSharp.Benchmarks { /// <summary> /// Defines a benchmark program. @@ -11,10 +9,5 @@ namespace NetSharp.Benchmarks /// The name of the benchmark. /// </summary> string Name { get; } - - /// <summary> - /// Runs the benchmark asynchronously. - /// </summary> - Task RunAsync(); } } \ No newline at end of file diff --git a/NetSharp/NetSharp.Benchmarks/NetSharp.Benchmarks.csproj b/NetSharp/NetSharp.Benchmarks/NetSharp.Benchmarks.csproj @@ -1,4 +1,4 @@ -<Project Sdk="Microsoft.NET.Sdk"> +<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> @@ -6,6 +6,10 @@ </PropertyGroup> <ItemGroup> + <PackageReference Include="BenchmarkDotNet" Version="0.12.1" /> + </ItemGroup> + + <ItemGroup> <ProjectReference Include="..\NetSharp\NetSharp.csproj" /> </ItemGroup> diff --git a/NetSharp/NetSharp.Benchmarks/Program.cs b/NetSharp/NetSharp.Benchmarks/Program.cs @@ -5,22 +5,19 @@ using System.Reflection; using System.Resources; using System.Text; +using BenchmarkDotNet.Running; + [assembly: NeutralResourcesLanguage("en")] namespace NetSharp.Benchmarks { internal class Program { - private const int DefaultExamplePort = 44231; - private static readonly List<INetSharpBenchmark> Benchmarks; - private static readonly IPAddress DefaultExampleAddress = IPAddress.Loopback; - public static readonly EndPoint DefaultClientEndPoint = new IPEndPoint(DefaultExampleAddress, 0); - public static readonly Encoding DefaultEncoding = Encoding.UTF8; - public static readonly EndPoint DefaultServerEndPoint = new IPEndPoint(DefaultExampleAddress, DefaultExamplePort); + private static readonly List<Type> Benchmarks; static Program() { - Benchmarks = new List<INetSharpBenchmark>(); + Benchmarks = new List<Type>(); foreach (Type type in Assembly.GetCallingAssembly().GetTypes()) { @@ -30,16 +27,15 @@ namespace NetSharp.Benchmarks } Type[] interfaces = type.GetInterfaces(); - object instance = Activator.CreateInstance(type); if (type.GetInterface(nameof(INetSharpBenchmark)) == typeof(INetSharpBenchmark)) { - Benchmarks.Add((INetSharpBenchmark) instance); + Benchmarks.Add(type); } } } - private static void Main() + private static void Main(string[] args) { RunAllBenchmarks(); @@ -53,7 +49,7 @@ namespace NetSharp.Benchmarks private static void PickBenchmark() { - Console.WriteLine("Available Examples:"); + Console.WriteLine("Available Benchmarks:"); for (int i = 0; i < Benchmarks.Count; i++) { Console.WriteLine($"\t{i:D2} - {Benchmarks[i].Name}"); @@ -74,11 +70,12 @@ namespace NetSharp.Benchmarks continue; } - INetSharpBenchmark selectedExample = Benchmarks[choice]; + Type selectedBenchmark = Benchmarks[choice]; + string selectedBenchmarkName = ((INetSharpBenchmark) Activator.CreateInstance(selectedBenchmark)).Name; - Console.WriteLine($"Starting \'{selectedExample.Name}\'..."); - selectedExample.RunAsync().GetAwaiter().GetResult(); - Console.WriteLine($"Finished \'{selectedExample.Name}\'!"); + Console.WriteLine($"Starting \'{selectedBenchmarkName}\'..."); + BenchmarkRunner.Run(selectedBenchmark); + Console.WriteLine($"Finished \'{selectedBenchmarkName}\'!"); break; } @@ -102,15 +99,17 @@ namespace NetSharp.Benchmarks if (input == "y") { - foreach (INetSharpBenchmark benchmark in Benchmarks) - { - Console.WriteLine($"Starting \'{benchmark.Name}\'..."); - benchmark.RunAsync().GetAwaiter().GetResult(); - Console.WriteLine($"Finished \'{benchmark.Name}\'!"); - - Console.WriteLine(); - } + BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(); } } + + public static class Constants + { + private const int DefaultExamplePort = 44231; + private static readonly IPAddress DefaultExampleAddress = IPAddress.Loopback; + public static readonly EndPoint ClientEndPoint = new IPEndPoint(DefaultExampleAddress, 0); + public static readonly Encoding ServerEncoding = Encoding.UTF8; + public static readonly EndPoint ServerEndPoint = new IPEndPoint(DefaultExampleAddress, DefaultExamplePort); + } } } \ No newline at end of file diff --git a/NetSharp/NetSharpExamples/Examples/Datagram Network Connection Examples/DatagramNetworkReaderExample.cs b/NetSharp/NetSharp.Examples/Examples/Datagram Network Connection Examples/DatagramNetworkReaderExample.cs diff --git a/NetSharp/NetSharpExamples/Examples/Datagram Network Connection Examples/DatagramNetworkWriterAsyncExample.cs b/NetSharp/NetSharp.Examples/Examples/Datagram Network Connection Examples/DatagramNetworkWriterAsyncExample.cs diff --git a/NetSharp/NetSharpExamples/Examples/Datagram Network Connection Examples/DatagramNetworkWriterSyncExample.cs b/NetSharp/NetSharp.Examples/Examples/Datagram Network Connection Examples/DatagramNetworkWriterSyncExample.cs diff --git a/NetSharp/NetSharpExamples/Examples/Stream Network Connection Examples/RawStreamChatServer.cs b/NetSharp/NetSharp.Examples/Examples/Stream Network Connection Examples/RawStreamChatServer.cs diff --git a/NetSharp/NetSharpExamples/Examples/Stream Network Connection Examples/StreamNetworkReaderExample.cs b/NetSharp/NetSharp.Examples/Examples/Stream Network Connection Examples/StreamNetworkReaderExample.cs diff --git a/NetSharp/NetSharpExamples/Examples/Stream Network Connection Examples/StreamNetworkWriterAsyncExample.cs b/NetSharp/NetSharp.Examples/Examples/Stream Network Connection Examples/StreamNetworkWriterAsyncExample.cs diff --git a/NetSharp/NetSharpExamples/Examples/Stream Network Connection Examples/StreamNetworkWriterSyncExample.cs b/NetSharp/NetSharp.Examples/Examples/Stream Network Connection Examples/StreamNetworkWriterSyncExample.cs diff --git a/NetSharp/NetSharpExamples/INetSharpExample.cs b/NetSharp/NetSharp.Examples/INetSharpExample.cs diff --git a/NetSharp/NetSharp.Examples/NetSharp.Examples.csproj b/NetSharp/NetSharp.Examples/NetSharp.Examples.csproj @@ -0,0 +1,11 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\NetSharp\NetSharp.csproj" /> + </ItemGroup> +</Project> +\ No newline at end of file diff --git a/NetSharp/NetSharpExamples/NetSharpExamples.xml b/NetSharp/NetSharp.Examples/NetSharpExamples.xml diff --git a/NetSharp/NetSharpExamples/Program.cs b/NetSharp/NetSharp.Examples/Program.cs diff --git a/NetSharp/NetSharp.Tests/NetSharp.Tests.csproj b/NetSharp/NetSharp.Tests/NetSharp.Tests.csproj @@ -6,6 +6,10 @@ </PropertyGroup> <ItemGroup> + <PackageReference Include="xunit" Version="2.4.1" /> + </ItemGroup> + + <ItemGroup> <ProjectReference Include="..\NetSharp\NetSharp.csproj" /> </ItemGroup> diff --git a/NetSharp/NetSharp.Tests/Program.cs b/NetSharp/NetSharp.Tests/Program.cs @@ -15,7 +15,7 @@ namespace NetSharp.Tests public static readonly Encoding DefaultEncoding = Encoding.UTF8; public static readonly EndPoint DefaultServerEndPoint = new IPEndPoint(DefaultExampleAddress, DefaultExamplePort); - private static void Main(string[] args) + private static void Main() { Console.WriteLine("Hello World!"); } diff --git a/NetSharp/NetSharp.sln b/NetSharp/NetSharp.sln @@ -3,16 +3,16 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.29324.140 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetSharp", "NetSharp\NetSharp.csproj", "{424A38DB-7CA0-4C94-90B4-EF246CB4BDC4}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetSharp.Examples", "NetSharpExamples\NetSharp.Examples.csproj", "{5861E518-E712-4031-9AA1-0313736FF957}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DD66A23B-CB45-4AAA-818A-4081DA9C875B}" ProjectSection(SolutionItems) = preProject ..\..\..\..\_Visual Studio Config\.editorconfig = ..\..\..\..\_Visual Studio Config\.editorconfig nuget.config = nuget.config EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetSharp", "NetSharp\NetSharp.csproj", "{424A38DB-7CA0-4C94-90B4-EF246CB4BDC4}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetSharp.Examples", "NetSharp.Examples\NetSharp.Examples.csproj", "{5861E518-E712-4031-9AA1-0313736FF957}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetSharp.Tests", "NetSharp.Tests\NetSharp.Tests.csproj", "{20FCC003-4342-49E3-9F66-640458C74B02}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetSharp.Benchmarks", "NetSharp.Benchmarks\NetSharp.Benchmarks.csproj", "{1C01A0C1-49C9-4B27-A4C8-7C66081982DC}" diff --git a/NetSharp/NetSharp/AssemblyAttributes.cs b/NetSharp/NetSharp/AssemblyAttributes.cs @@ -2,4 +2,5 @@ using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("NetSharp.Benchmarks", AllInternalsVisible = true)] +[assembly: InternalsVisibleTo("NetSharp.Tests", AllInternalsVisible = true)] [assembly: NeutralResourcesLanguage("en")] \ No newline at end of file diff --git a/NetSharp/NetSharpExamples/NetSharp.Examples.csproj b/NetSharp/NetSharpExamples/NetSharp.Examples.csproj @@ -1,20 +0,0 @@ -<Project Sdk="Microsoft.NET.Sdk"> - - <PropertyGroup> - <OutputType>Exe</OutputType> - <TargetFramework>netcoreapp3.1</TargetFramework> - <Configurations>Debug;Release</Configurations> - </PropertyGroup> - - <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> - <DocumentationFile>NetSharpExamples.xml</DocumentationFile> - </PropertyGroup> - - <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> - <DocumentationFile>NetSharpExamples.xml</DocumentationFile> - </PropertyGroup> - - <ItemGroup> - <ProjectReference Include="..\NetSharp\NetSharp.csproj" /> - </ItemGroup> -</Project> -\ No newline at end of file